TCP通信实现(服务器端)
案例
// TCP 通信的服务器端 #include#include #include #include #include int main() { // 1.创建socket(用于监听的套接字) int lfd = socket(AF_INET, SOCK_STREAM, 0); if(lfd == -1) { perror("socket"); exit(-1); } // 2.绑定 #include // struct sockaddr_in // { // sa_family_t sin_family; /* __SOCKADDR_COMMON(sin_) */ // in_port_t sin_port; /* Port number. */ // struct in_addr sin_addr; /* Internet address. */ // /* Pad to size of `struct sockaddr'. */ // unsigned char sin_zero[sizeof (struct sockaddr) - __SOCKADDR_COMMON_SIZE - // sizeof (in_port_t) - sizeof (struct in_addr)]; // }; // struct in_addr // { // in_addr_t s_addr; // }; struct sockaddr_in saddr; saddr.sin_family = AF_INET; // inet_pton(AF_INET, "192.168.193.128", saddr.sin_addr.s_addr); saddr.sin_addr.s_addr = INADDR_ANY; // 0.0.0.0 表示任意地址,计算机有多个网卡,每个网卡ip地址可能都不一样。只有服务端可以这样写 saddr.sin_port = htons(9999);//将一个短整型数从主机字节序转换到网络字节序。 int ret = bind(lfd, (struct sockaddr *)&saddr, sizeof(saddr)); if(ret == -1) { perror("bind"); exit(-1); } // 3.监听 ret = listen(lfd, 8); if(ret == -1) { perror("listen"); exit(-1); } // 4.接收客户端连接 struct sockaddr_in clientaddr; int len = sizeof(clientaddr); //是一个阻塞的函数 int cfd = accept(lfd, (struct sockaddr *)&clientaddr, &len); if(cfd == -1) { perror("accept"); exit(-1); } // 输出客户端的信息 char clientIP[16]; inet_ntop(AF_INET, &clientaddr.sin_addr.s_addr, clientIP, sizeof(clientIP));//网络字节序转主机字节序。ip地址转换 unsigned short clientPort = ntohs(clientaddr.sin_port);//网络字节序转主机字节序,端口号转换 printf("client ip is %s, port is %d\n", clientIP, clientPort); // 5.通信 char recvBuf[1024] = {0}; while(1) { // 获取客户端的数据 int num = read(cfd, recvBuf, sizeof(recvBuf)); if(num == -1) { perror("read"); exit(-1); } else if(num > 0) { printf("recv client data : %s\n", recvBuf); } else if(num == 0) { // 表示客户端断开连接 printf("clinet closed..."); break; } char * data = "hello,i am server"; // 给客户端发送数据 write(cfd, data, strlen(data)); } // 关闭文件描述符 close(cfd); close(lfd); return 0; }
TCP通信实现(客户端)
案例
// TCP通信的客户端 #include#include #include #include #include int main() { // 1.创建套接字 int fd = socket(AF_INET, SOCK_STREAM, 0); if(fd == -1) { perror("socket"); exit(-1); } // 2.连接服务器端 struct sockaddr_in serveraddr; serveraddr.sin_family = AF_INET; inet_pton(AF_INET, "192.168.128.128", &serveraddr.sin_addr.s_addr); serveraddr.sin_port = htons(9999); int ret = connect(fd, (struct sockaddr *)&serveraddr, sizeof(serveraddr)); if(ret == -1) { perror("connect"); exit(-1); } // 3. 通信 char recvBuf[1024] = {0}; while(1) { char * data = "hello,i am client"; // 给客户端发送数据 write(fd, data , strlen(data)); sleep(1); int len = read(fd, recvBuf, sizeof(recvBuf)); if(len == -1) { perror("read"); exit(-1); } else if(len > 0) { printf("recv server data : %s\n", recvBuf); } else if(len == 0) { // 表示服务器端断开连接 printf("server closed..."); break; } } // 关闭连接 close(fd); return 0; }
daic@daic:~/Linux/linuxwebserver/part04networkProgramming/lesson32$ ./client recv server data : hello,i am server recv server data : hello,i am server recv server data : hello,i am server recv server data : hello,i am server recv server data : hello,i am server recv server data : hello,i am server
daic@daic:~/Linux/linuxwebserver/part04networkProgramming/lesson32$ ./server client ip is 192.168.128.128, port is 54410 recv client data : hello,i am client recv client data : hello,i am client recv client data : hello,i am client recv client data : hello,i am client recv client data : hello,i am client recv client data : hello,i am client recv client data : hello,i am client recv client data : hello,i am client recv client data : hello,i am client
还没有评论,来说两句吧...