客户端代码: /*client.c*/ #include #include #include #include #include #include #define MAXBUF 256 int main(int argc, char const *argv[]) { int s = 0; int n = 0; int reuse = 1; int port = 1987; struct sockaddr_in srv; char buf[MAXBUF] = {0}; /*解析参数*/ if (argc != 2) { printf("Usage:%s ServerIP
", argv[0]); return -1; } bzero(&srv, sizeof(srv)); srv.sin_family = PF_INET; srv.sin_addr.s_addr = inet_addr(argv[1]); srv.sin_port = htons(port); /*创建 UDP 套节字*/ s = socket(AF_INET, SOCK_DGRAM, 0); if(s<0){ perror("socket"); return -1; } while(1){ memset(buf, 0, MAXBUF); /*读取用户输入到buf中*/ fgets(buf, MAXBUF, stdin); /*通过套节字 s 向服务器发送数据*/ if ((n = sendto(s, buf, strlen(buf), 0, (struct sockaddr *) &srv, sizeof(struct sockaddr))) < 0) { perror("sendto"); return -1; }else{ printf("send to %s(port=%d) len %d:%s
", argv[1], port, n, buf); } } }
服务器端代码:
/*server.c*/ #include #include #include #include #include #include #define MAXBUF 256 int main(int argc, char const *argv[]) { int s = 0; int n = 0; int reuse = 1; int cli_len = sizeof(struct sockaddr); int port = 1987; char buf[MAXBUF] = {0}; struct sockaddr_in addr, cli; /*初始化本地监听端口信息*/ bzero(&addr, sizeof(addr)); addr.sin_family = PF_INET; addr.sin_addr.s_addr = INADDR_ANY; addr.sin_port = htons(port); /*创建UDP套节字*/ s = socket(AF_INET, SOCK_DGRAM, 0); if (s<0) { perror("socket"); return -1; } /*允许端口复用*/ setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)); /*绑定指定端口*/ if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) { perror("bind"); return -1; } while(1){ memset(buf, 0, MAXBUF); /*从套节字s中读取数据*/ n = recvfrom(s, buf, MAXBUF, 0, (struct sockaddr *)&cli, &cli_len); if(n<0){ perror("recvfrom"); return -1; }else{ printf("receive msg from %s(port=%d) len %d: %s
",inet_ntoa(cli.sin_addr), port, n, buf); } } return 0; }编译命令:
客户端:
gcc client.c -o client ./client 127.0.0.1
服务端:
gcc server.c -o server ./server