-
服务端
/* * @Author: hohj * @Date: 2020-12-13 14:14:49 * @Last Modified by: hohj * @Last Modified time: 2021-01-13 10:25:31 */ #include <unistd.h> #include <stdio.h> #include <string.h> #include <sys/socket.h> #include <sys/types.h> #include <stdlib.h> #include <arpa/inet.h> #include "../color.h" int main(int argc, char const *argv[]) { int sockfd = socket(AF_INET, SOCK_STREAM,0 ); struct sockaddr_in server; //设置服务端地址信息 server.sin_family = AF_INET; server.sin_addr.s_addr = inet_addr("192.168.33.1"); server.sin_port = htons(8888); //绑定本地地址信息 if(bind(sockfd, (struct sockaddr *)&server, sizeof(server))==-1){ perror("bind()"); exit(1); } //进入等待连接请求状态 listen(sockfd, 5); struct sockaddr_in client;//创建sockaddr_in用于存储客户端地址信息 socklen_t len = sizeof(client); int newfd = accept(sockfd, (struct sockaddr *)&client, &len);//接受客户端连接请求 printf("%d, %d", sockfd, newfd); printf("before listen\n"); char buff[10] = {0};//此处设置的buff大小只有10字节 while(1) { bzero(buff, sizeof(buff)); int ret = read(newfd, buff, sizeof(buff));//读取buff大小(10)的数据, if(ret==0) exit(0); //recv(newfd, buff, sizeof(buff), 0); printf(RED"%d: %s(%d): %s\n", newfd, inet_ntoa(client.sin_addr), ntohs(client.sin_port), buff); } return 0; }
-
客户端
/* * @Author: hohj * @Date: 2020-12-13 14:14:39 * @Last Modified by: hohj * @Last Modified time: 2021-02-01 15:24:30 */ #include <unistd.h> #include <stdio.h> #include <string.h> #include <sys/socket.h> #include <sys/types.h> #include <stdlib.h> #include <arpa/inet.h> int main(int argc, char const *argv[]) { int sockfd = socket(AF_INET, SOCK_STREAM, 0);//创建TCP套接字 struct sockaddr_in server; //创建sockaddr_in结构体(保存地址信息) server.sin_family = AF_INET; //使用IPv4地址 server.sin_addr.s_addr = inet_addr("192.168.33.20"); //服务器地址 server.sin_port = htons(6666); //服务器端口 /*struct sockaddr_in client; client.sin_family = AF_INET; client.sin_addr.s_addr = inet_addr("127.0.0.1"); client.sin_port = htons(6666);*/ /*if(bind(sockfd, (struct sockaddr *)&client, sizeof(client))==-1){ perror("bind()"); exit(1); }*/ //请求连接 if(connect(sockfd, (struct sockaddr *)&server, sizeof(server))==-1){ perror("connect()"); exit(1); } printf("after connect\n"); char buff[512] = {0}; while(1) { bzero(buff, sizeof(buff)); printf(">"); scanf("%[^\n]s", buff); getchar(); write(sockfd, buff, strlen(buff));//发送数据 //send(sockfd, buff, strlen(buff), 0); } return 0; }
-
编译
gcc client.c -o client
gcc server.c -o server
-
运行
服务端:./server
hohj@ubuntu20-10:~/Documents/C/tcp$ gcc server.c -o server hohj@ubuntu20-10:~/Documents/C/tcp$ ./server 3, 4before listen 4: 127.0.0.1(40258): this is th 4: 127.0.0.1(40258): e TCP conn 4: 127.0.0.1(40258): ect servic 4: 127.0.0.1(40258): es
客户端:
./client
hohj@ubuntu20-10:~/Documents/C/tcp$ gcc server.c -o server hohj@ubuntu20-10:~/Documents/C/tcp$ ./server 3, 4before listen 4: 127.0.0.1(40258): this is th 4: 127.0.0.1(40258): e TCP conn 4: 127.0.0.1(40258): ect servic 4: 127.0.0.1(40258): es
相关文章
- 03-24网络套接字 你必须给我把博客写好,这是重点的重点,后续也得写上,不能断
- 03-24网络安全传输系统-sprint1传输子系统
- 03-24如何用C语言写一个基于服务器和客户端(TCP)
- 03-24网络套接字函数
- 03-24TCP/UDP套接字网络协议
- 03-24TCP网络编程
- 03-24初夏小谈:基于TCP协议的网络通信(线程池实现)
- 03-24VC++多线程tcp connect扫描
- 03-24TCP/UDP
- 03-24LTE--MR开启异频测量对网络性能的影响