Ubuntu下实现回射服务器(一)

一、服务器端

 1 #include<stdio.h>
 2 #include <sys/types.h>          /* See NOTES */
 3 #include <sys/socket.h>
 4 #include <netinet/ip.h>
 5 #include <errno.h>
 6 #include <stdlib.h>
 7 #include <netinet/in.h>
 8 #include <arpa/inet.h>
 9 #include <unistd.h>
10 #include <string.h>
11 
12 int main(){
13     int socketfd;
14     socketfd=socket(PF_INET,SOCK_STREAM,0);
15     if(socketfd<0){
16         perror("socket");
17         exit(EXIT_FAILURE);
18     }
19 
20     struct sockaddr_in sockaddress;
21     sockaddress.sin_family=AF_INET;
22     sockaddress.sin_port=htons(5188);
23     sockaddress.sin_addr.s_addr=htonl(INADDR_ANY);//INADDR_ANY接收任何地址的连接
24     //sockaddress.sin_addr.s_addr=inet_addr("127.0.0.1");//也可以是这种方式
25     if(bind(socketfd,(struct sockaddr*)&sockaddress,sizeof(sockaddress))<0){
26         perror("bind");
27         exit(EXIT_FAILURE);
28     }
29 
30     if ( listen(socketfd,SOMAXCONN)<0){
31         perror("listen");
32         exit(EXIT_FAILURE);
33     }
34 
35     int conn;
36     struct sockaddr_in peeraddr;
37     socklen_t len=sizeof(struct sockaddr_in);
38     conn=accept(socketfd,(struct sockaddr*)&peeraddr,&len);
39         if (conn<0){
40       perror("accept");
41       exit(EXIT_FAILURE);
42     }
43     printf("client ip:%s port:%d\n",inet_ntoa(peeraddr.sin_addr),ntohs(peeraddr.sin_port));
44 
45     char recvbuffer[1024];
46        while(1){
47      memset(recvbuffer,0,sizeof(recvbuffer));
48          read(conn,&recvbuffer,sizeof(recvbuffer));
49      fputs(recvbuffer,stdout);
50      write(conn,recvbuffer,strlen(recvbuffer));
51        }
52        close(socketfd);
53        close(conn);
54 
55        return 0;
56 
57 }
1、accept(socketfd,(struct sockaddr*)&peeraddr,&len)第二个参数是一个输出参数,第三个参数是一个输入输出参数一定要初始化

 

二、客户端

 1 #include<stdio.h>
 2 #include <sys/types.h>          /* See NOTES */
 3 #include <sys/socket.h>
 4 #include <netinet/ip.h>
 5 #include <errno.h>
 6 #include <stdlib.h>
 7 #include <netinet/in.h>
 8 #include <arpa/inet.h>
 9 #include <unistd.h>
10 #include <string.h>
11 
12 int main(){
13     int socketfd;
14     socketfd=socket(PF_INET,SOCK_STREAM,0);
15     if(socketfd<0){
16         perror("socket");
17         exit(EXIT_FAILURE);
18     }
19 
20     struct sockaddr_in sockaddress;
21     sockaddress.sin_family=AF_INET;
22     sockaddress.sin_port=htons(5188);
23     sockaddress.sin_addr.s_addr=inet_addr("127.0.0.1");
24 
25     if(connect(socketfd,(struct sockaddr*)&sockaddress,sizeof(sockaddress))<0){
26       perror("connect");
27       exit(EXIT_FAILURE);
28     }
29 
30     char recvbuffer[1024]={0};
31     char sendbuffer[1024]={0};
32 
33     while(fgets(sendbuffer,sizeof(sendbuffer),stdin)!=NULL){
34       write(socketfd,sendbuffer,strlen(sendbuffer));
35       read(socketfd,recvbuffer,sizeof(recvbuffer));
36       fputs(recvbuffer,stdout);
37       memset(sendbuffer,0,sizeof(sendbuffer));
38       memset(recvbuffer,0,sizeof(recvbuffer));
39     }
40 
41     close(socketfd);
42        return 0;
43 
44 }

注意客户端第37、38行,清空缓存的原因是因为服务器端回射回来的数据可能覆盖不全原来缓存内容。

 

 

posted @ 2021-05-28 12:05  疯狗强尼  阅读(81)  评论(0编辑  收藏  举报