复制代码
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#include<stdio.h> #include<stdlib.h> #include<unistd.h> #include<string.h> #include<assert.h> #include<netinet/in.h> #include<sys/types.h> #include<sys/socket.h> #include<arpa/inet.h> int main() { int sockfd=socket(AF_INET,SOCK_STREAM,0); //建立一个面向连接的套接字 struct sockaddr_in ser,cli; memset(&ser,0,sizeof(ser)); //将“0”复制到参数 ser 所指向的字符串 ser.sin_family=AF_INET; ser.sin_port=htons(80); //打开服务端的端口号 ser.sin_addr.s_addr=inet_addr("192.168.1.109"); //选择所连接的服务器的ip int res=bind(sockfd,(struct sockaddr*)&ser,sizeof(ser)); listen(sockfd,5); while(1) { int len=sizeof(cli); int c=accept(sockfd,(struct sockaddr*)&cli,&len); if(c<0) { perror("errorn"); continue; } while(1) { char recvbuf[128]={0}; int n=recv(c,recvbuf,127,0); if(n<=0) { printf("one client overn"); break; } printf("%d:%sn",c,recvbuf); //打印客户端发送的信息 send(c,"OK",2,0); //回显OK给客户端 } close(c); } close(sockfd); }
最后
以上就是鲤鱼小海豚最近收集整理的关于建立简单的tcp回显服务端的全部内容,更多相关建立简单内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复