c语言的tcp和udp客户端和服务器
都是最简单的用来记忆。
this is my 的git地址:https://github.com/yanjinyun/cLanguageTcpUdp
tcp最简单的服务器:
int main(int argc, const char *argv[]) { int listenfd, acceptfd; struct sockaddr_in sin, cin; socklen_t clen; char buf[1024]; signal(SIGCHLD, SIG_IGN); listenfd = socket(PF_INET, SOCK_STREAM, 0); sin.sin_family = PF_INET; sin.sin_port = htons(atoi(argv[2])); sin.sin_addr.s_addr = inet_addr(argv[1]); bind(listenfd, (struct sockaddr *)&sin, sizeof(sin)); listen(listenfd, 5); while(1) { clen = sizeof(cin); printf("listen ......\n"); acceptfd = accept(listenfd, (struct sockaddr *)&cin, &clen); printf("client %s %d connect\n", inet_ntoa(cin.sin_addr), ntohs(cin.sin_port)); if(fork() == 0) { close(listenfd); while(1) { bzero(buf, sizeof(buf)); if(recv(acceptfd, buf, sizeof(buf), 0) == 0) { printf("client %s %d exit\n", inet_ntoa(cin.sin_addr), ntohs(cin.sin_port)); exit(0); } printf("recv(%s %d): %s\n", inet_ntoa(cin.sin_addr), ntohs(cin.sin_port), buf); } } close(acceptfd); } return 0; }
tcp最简单的客户端:
int main(int argc, const char *argv[]) { int cfd = socket(PF_INET, SOCK_STREAM, 0); struct sockaddr_in cin; char buf[1024]; cin.sin_family = PF_INET; cin.sin_port = htons(atoi(argv[2])); cin.sin_addr.s_addr = inet_addr(argv[1]); connect(cfd, (struct sockaddr *)&cin, sizeof(cin)); printf("connect ip(%s) port(%s) server ok\n", argv[1], argv[2]); if(fork() == 0) { while(1) { bzero(buf, sizeof(buf)); recv(cfd, buf, sizeof(buf), 0); printf("recv: %s\n", buf); } } while(1) { fgets(buf, sizeof(buf), stdin); buf[strlen(buf)-1] = '\0'; send(cfd, buf, strlen(buf)+1, 0); } return 0; }
udp的最简单的服务器:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | int main( int argc, const char *argv[]) { int sfd; struct sockaddr_in sin, cin; char buf1[1024], buf2[1024], buf3[1024]; socklen_t clen; sfd = socket(PF_INET, SOCK_DGRAM, 0); sin.sin_family = PF_INET; sin.sin_port = htons(atoi(argv[2])); sin.sin_addr.s_addr = inet_addr(argv[1]); bind(sfd, ( struct sockaddr *)&sin, sizeof (sin)); if (fork() == 0) { while (1) { clen = sizeof (cin); bzero(buf1, sizeof (buf1)); recvfrom(sfd, buf1, sizeof (buf1), 0, ( struct sockaddr *)&cin, &clen); printf( "recvfrom: %s %d %s\n" , inet_ntoa(cin.sin_addr), ntohs(cin.sin_port), buf1); } } while (1) { fscanf(stdin, "%s%s%s" , buf1, buf2, buf3); cin.sin_family = PF_INET; cin.sin_port = htons(atoi(buf2)); cin.sin_addr.s_addr = inet_addr(buf1); sendto(sfd, buf3, strlen(buf3)+1, 0, ( struct sockaddr *)&cin, sizeof (cin)); } return 0; } |
udp最简单的客户端:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | int main( int argc, const char *argv[]) { int sfd; struct sockaddr_in cin; char buf1[1024], buf2[1024], buf3[1024]; socklen_t clen; sfd = socket(PF_INET, SOCK_DGRAM, 0); if (fork() == 0) { while (1) { clen = sizeof (cin); bzero(buf1, sizeof (buf1)); recvfrom(sfd, buf1, sizeof (buf1), 0, ( struct sockaddr *)&cin, &clen); printf( "recvfrom: %s %d %s\n" , inet_ntoa(cin.sin_addr), ntohs(cin.sin_port), buf1); } } while (1) { fscanf(stdin, "%s%s%s" , buf1, buf2, buf3); cin.sin_family = PF_INET; cin.sin_port = htons(atoi(buf2)); cin.sin_addr.s_addr = inet_addr(buf1); sendto(sfd, buf3, strlen(buf3)+1, 0, ( struct sockaddr *)&cin, sizeof (cin)); } return 0; } |
ftp最简单的服务器器:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 | #define N 256 void list( int connectfd) { DIR *dir; struct dirent *d; char buf[N]; if ((dir=opendir(( const char *)get_current_dir_name())) == NULL) { perror( "opendir" ); exit(1); } while ((d=readdir(dir)) != NULL) { if (d->d_name[0] == '.' ) { continue ; } strcpy(buf, d->d_name); send(connectfd, buf, N, 0); } closedir(dir); } void get ( int connectfd, char *filename) { char buf[N]; FILE *fp; int n; if ((fp=fopen(filename, "r" )) == NULL) { strcpy(buf, "no" ); send(connectfd, buf, sizeof (buf), 0); } strcpy(buf, "yes" ); send(connectfd, buf, sizeof (buf), 0); while ((n=fread(buf, 1, sizeof (buf), fp)) > 0) { send(connectfd, buf, n, 0); } fclose(fp); } void put( int connectfd, char *filename) { char buf[N]; FILE *fp; int n; if ((fp=fopen(filename, "r" )) == NULL) { strcpy(buf, "no" ); send(connectfd, buf, sizeof (buf), 0); fp = fopen(filename, "w" ); while ((n=recv(connectfd, buf, sizeof (buf), 0)) > 0) { fwrite(buf, 1, n, fp); } fclose(fp); return ; } strcpy(buf, "yes" ); send(connectfd, buf, sizeof (buf), 0); } int main( int argc, const char *argv[]) { FILE *fp; int n, listenfd, connectfd, i; struct sockaddr_in s, c; struct servent *sport; socklen_t clen; char *linep = NULL, buf[N], *arg[32]; signal(SIGCHLD, SIG_IGN); if ((fp=fopen( "/etc/ftp.conf" , "r" )) == NULL) { perror( "fopen" ); exit(1); } if (getline(&linep, &n, fp) == -1) { perror( "getline" ); exit(0); } linep[strlen(linep)-1] = '\0' ; chdir(linep); if ((sport=getservbyname( "myftp" , "tcp" )) == NULL) { perror( "getservbyname" ); exit(1); } #ifdef DEBUG printf( "%d %s\n" , n, linep); #endif if ((listenfd=socket(PF_INET, SOCK_STREAM, 0)) == -1) { perror( "socket" ); exit(1); } s.sin_family = PF_INET; s.sin_port = sport->s_port; s.sin_addr.s_addr = htonl(INADDR_ANY); if (bind(listenfd, ( struct sockaddr *)&s, sizeof (s)) == -1) { perror( "bind" ); exit(1); } listen(listenfd, 5); while (1) { clen = sizeof (c); if ((connectfd=accept(listenfd, ( struct sockaddr *)&c, &clen)) == -1) { perror( "accept" ); exit(1); } if (fork() == 0) { close(listenfd); recv(connectfd, buf, sizeof (buf), 0); arg[0] = strtok(buf, " " ); for (i=1; arg[i-1]!=NULL; i++) { arg[i]=strtok(NULL, " " ); } if (strcmp(arg[0], "list" ) == 0) { list(connectfd); } if (strcmp(arg[0], "get" ) == 0) { get (connectfd, arg[1]); } if (strcmp(arg[0], "put" ) == 0) { put(connectfd, arg[1]); } close(connectfd); exit(0); } close(connectfd); } } |
ftp最简单的客户端器:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 | #define N 256 void list( int connectfd) { DIR *dir; struct dirent *d; char buf[N]; if ((dir=opendir(( const char *)get_current_dir_name())) == NULL) { perror( "opendir" ); exit(1); } while ((d=readdir(dir)) != NULL) { if (d->d_name[0] == '.' ) { continue ; } strcpy(buf, d->d_name); send(connectfd, buf, N, 0); } closedir(dir); } void get ( int connectfd, char *filename) { char buf[N]; FILE *fp; int n; if ((fp=fopen(filename, "r" )) == NULL) { strcpy(buf, "no" ); send(connectfd, buf, sizeof (buf), 0); } strcpy(buf, "yes" ); send(connectfd, buf, sizeof (buf), 0); while ((n=fread(buf, 1, sizeof (buf), fp)) > 0) { send(connectfd, buf, n, 0); } fclose(fp); } void put( int connectfd, char *filename) { char buf[N]; FILE *fp; int n; if ((fp=fopen(filename, "r" )) == NULL) { strcpy(buf, "no" ); send(connectfd, buf, sizeof (buf), 0); fp = fopen(filename, "w" ); while ((n=recv(connectfd, buf, sizeof (buf), 0)) > 0) { fwrite(buf, 1, n, fp); } fclose(fp); return ; } strcpy(buf, "yes" ); send(connectfd, buf, sizeof (buf), 0); } int main( int argc, const char *argv[]) { FILE *fp; int n, listenfd, connectfd, i; struct sockaddr_in s, c; struct servent *sport; socklen_t clen; char *linep = NULL, buf[N], *arg[32]; signal(SIGCHLD, SIG_IGN); if ((fp=fopen( "/etc/ftp.conf" , "r" )) == NULL) { perror( "fopen" ); exit(1); } if (getline(&linep, &n, fp) == -1) { perror( "getline" ); exit(0); } linep[strlen(linep)-1] = '\0' ; chdir(linep); if ((sport=getservbyname( "myftp" , "tcp" )) == NULL) { perror( "getservbyname" ); exit(1); } #ifdef DEBUG printf( "%d %s\n" , n, linep); #endif if ((listenfd=socket(PF_INET, SOCK_STREAM, 0)) == -1) { perror( "socket" ); exit(1); } s.sin_family = PF_INET; s.sin_port = sport->s_port; s.sin_addr.s_addr = htonl(INADDR_ANY); if (bind(listenfd, ( struct sockaddr *)&s, sizeof (s)) == -1) { perror( "bind" ); exit(1); } listen(listenfd, 5); while (1) { clen = sizeof (c); if ((connectfd=accept(listenfd, ( struct sockaddr *)&c, &clen)) == -1) { perror( "accept" ); exit(1); } if (fork() == 0) { close(listenfd); recv(connectfd, buf, sizeof (buf), 0); arg[0] = strtok(buf, " " ); for (i=1; arg[i-1]!=NULL; i++) { arg[i]=strtok(NULL, " " ); } if (strcmp(arg[0], "list" ) == 0) { list(connectfd); } if (strcmp(arg[0], "get" ) == 0) { get (connectfd, arg[1]); } if (strcmp(arg[0], "put" ) == 0) { put(connectfd, arg[1]); } close(connectfd); exit(0); } close(connectfd); } } |
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步