ubuntu@ubuntu:~/yuyu/yu/11$ cat Tcp_Cli.c 客户端 #include <stdio.h> #include <sys/types.h> #include <sys/socket.h> #include <arpa/inet.h> #include <netinet/in.h> #include <string.h> #include <unistd.h> #define ERR_MSG(msg) do{\ fprintf(stderr,"__%d__:", __LINE__);\ perror(msg);\ }while(0) #define PORT 8888 #define IP "192.168.8.187" int main(int argc, const char *argv[]) { //创建流式套接字 int sfd = socket(AF_INET, SOCK_STREAM, 0); if(sfd < 0) { ERR_MSG("socket"); return -1; } //下面这段代码允许端口快速被重用,不加只能用一次 int reuse = 1; if(setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)
22.11.22 TCP与UDP 客户端与服务器 协议搭建
最新推荐文章于 2024-05-28 09:54:01 发布