TCP协议的服务器、客户端代码,UDP的服务器、客户端代码

TCP:服务器

  1 #include <stdio.h>
  2 #include <sys/types.h>
  3 #include <sys/socket.h>
  4 #include <unistd.h>
  5 #include <arpa/inet.h>
  6 #include <netinet/in.h>
  7 #include <string.h>
  8 
  9 #define IP "192.168.124.47"     //ifconfig查看
 10 #define PORT 8888              //端口号的网络字节序(在1014-49151之间)
 11 #define ERR_MSG(msg) do{\
 12     fprintf(stderr,"line=%d \n",__LINE__);\
 13     perror(msg);\
 14 }while(0)
 15 
 16 int main(int argc, const char *argv[])
 17 {
 18     //创建流式套接字
 19     int sfd = socket(AF_INET,SOCK_STREAM,0);
 20     if (sfd < 0)
 21     {
 22         ERR_MSG("socket");
 23         return -1;
 24     }
 25     printf("流式套接字创建成功\n");
 26     //允许端口被快速使用                                                                                      
 27     int reuse = 1;
 28     if(setsockopt(sfd,SOL_SOCKET,SO_REUSEADDR,&reuse,sizeof(reuse))<0)
 29     {
 30         ERR_MSG("setsockopt");
 31         return -1;
 32     }
 33     printf("允许端口快速重用\n");
 34     //填充服务器的地址信息结构体,给bind函数使用
 35     //真实的地址信息结构体根据地址族制定,AF_INET-->main 7 ip
 36     struct sockaddr_in sin;
 37     sin.sin_family      = AF_INET;//必须填AF_INET(代表是流式套接字)
 38     sin.sin_port        = htons(PORT);//端口号的网络字节序
 39     //(htons代表是将主机字节序转换为网络字节序)
 40     sin.sin_addr.s_addr = inet_addr(IP);//本机IP,ifconfig查看,
 41     //inet_addr是.十进制转换为网络字节序
 42 
 43     //绑定服务器的地址信息,必须绑定
 44     if(bind(sfd,(struct sockaddr*)&sin,sizeof(sin)) < 0)
 45     {
 46         ERR_MSG("bind");
 47         return -1;
 48     }
 49     printf("绑定成功\n");
 50     //将套接字设置为被监听状态
 51     if (listen(sfd,128)< 0)
 52     {
 53         ERR_MSG("listen");
 54         return -1;
 55     }
 56     //获取连接成功的客户端信息,生成一个新的套接字文件描述符
 57     struct sockaddr_in cin;
 58     socklen_t addrlen = sizeof(cin);
 59     int newsfd = accept(sfd,(struct sockaddr * )&cin,&addrlen);
 60     if(newsfd < 0)
 61     {
 62         ERR_MSG("accept");
 63         return -1;
 64     }
 65     printf("[%s %d] newsfd=%d 客户端连接成功\n",inet_ntoa(cin.sin_addr),\
 66             ntohs(cin.sin_port),newsfd);
 67     //接收
 68     char buf[128]="";
 69     ssize_t res;
 70     while(1)
 71     {
 72         bzero(buf,sizeof(buf));
 73         res = recv(newsfd,buf,sizeof(buf),0);
 74         if(res < 0)
 75         {
 76             ERR_MSG("recv");
 77             return -1;
 78         }
 79         else if(res == 0)
 80         {
 81             printf("[%s %d] newsfd=%d 客户端下线\n",inet_ntoa(cin.sin_addr),\
 82                     ntohs(cin.sin_port),newsfd);
 83             break;
 84         }
 85         printf("[%s %d] newsfd = %d ; %s\n",inet_ntoa(cin.sin_addr),\
 86                 ntohs(cin.sin_port),newsfd,buf);
 87 
 88     //发送
 89     strcat(buf,"*__*");
 90     if(send(newsfd,buf,sizeof(buf),0)<0)
 91     {
 92         ERR_MSG("send");
 93         return -1;
 94         }
 95     printf("发送成功\n");
 96     }
 97 
 98     //关闭文件描述符
 99     if(close(sfd)< 0)
100     {
101         ERR_MSG("close");
102         return -1;
103     }
104     printf("sfd关闭成功 \n");
105     if(close(newsfd)<0)
106     {
107         ERR_MSG("close");
108         return -1;
109     }
110     printf("newsfd关闭成功\n");
111 
112     return 0;
113 }
~                                                                                                                 

TCP客户端

  1 #include <stdio.h>
  2 #include <sys/types.h>
  3 #include <sys/socket.h>
  4 #include <unistd.h>
  5 #include <arpa/inet.h>
  6 #include <netinet/in.h>
  7 #include <string.h>
  8 
  9 #define IP "192.168.124.47"     //ifconfig查看
 10 #define PORT 8888              //端口号的网络字节序(在1014-49151之间)
 11 #define ERR_MSG(msg) do{\
 12     fprintf(stderr,"line=%d \n",__LINE__);\
 13     perror(msg);\
 14 }while(0);
 15 
 16 int main(int argc, const char *argv[])
 17 {
 18     //创建流式套接字
 19     int cfd = socket(AF_INET,SOCK_STREAM,0);
 20     if (cfd < 0)
 21     {
 22         ERR_MSG("socket");
 23         return -1;
 24     }
 25     printf("流式套接字创建成功\n");
 26     /*
 27     //允许端口被快速使用
 28     int reuse = 1;
 29     if(setsockopt(sfd,SOL_SOCKET,SO_REUSEADDR,&reuse,sizeof(reuse))<0)
 30     {
 31         ERR_MSG("setsockopt");
 32         return -1;
 33     }
 34     printf("允许端口快速重用\n");
 35     */
 36     //填充服务器的地址信息结构体,给connect函数使用
 37     //代表需要连接到哪个服务器上,服务器绑定的是什么IP和PORT,这里就应该填哪个服务器的IP
 38     struct sockaddr_in sin;
 39     sin.sin_family      = AF_INET;//必须填AF_INET(代表是流式套接字)
 40     sin.sin_port        = htons(PORT);//端口号的网络字节序,服务器绑定的PORT
 41                                        //(htons代表是将主机字节序转换为网络字节序)
 42     sin.sin_addr.s_addr = inet_addr(IP);//服务器绑定的IP,ifconfig查看,
 43                                         //inet_addr是.十进制转换为网络字节序
 44     //连接指定的服务器
 45     if(connect(cfd,(struct sockaddr *)&sin,sizeof(sin))<0)
 46     {
 47         ERR_MSG("connect");
 48         return -1;
 49     }
 50     printf("连接服务器[%s : %d]成功\n",IP,PORT);
 51     char buf[128]="";
 52     ssize_t res;
 53     while(1)
 54     {
 55         bzero(buf,sizeof(buf));
 56         //发送
 57         printf("请输入>>>");
 58         fgets(buf,sizeof(buf),stdin);
 59         buf[strlen(buf)-1] = '\0';
 60         if(send(cfd,buf,sizeof(buf),0)<0)
 61         {
 62             ERR_MSG("send");
 63             return -1;
 64         }
 65         printf("发送成功\n");
 66 
 67         bzero(buf,sizeof(buf));
 68         res = recv(cfd,buf,sizeof(buf),0);
 69         if(res < 0)
 70         {
 71             ERR_MSG("recv");
 72             return -1;
 73         }
 74         else if(res == 0)
 75         {
 76             printf("[%s %d] cfd=%d 服务器下线\n",IP,PORT,cfd);
 77             break;
 78         }
 79         printf("[%s %d] cfd = %d ; %s\n",IP,PORT,cfd,buf);
 80     }
 81 
 82     //关闭文件描述符
 83     if(close(cfd)<0)                                                                                
 84     {
 85         ERR_MSG("close");
 86         return -1;
 87     }
 88     printf("newsfd关闭成功\n");
 89 
 90     return 0;
 91 }
~                                                                                                       

 UDP服务器

  1 #include <stdio.h>
  2 #include <sys/types.h>
  3 #include <sys/socket.h>
  4 #include <unistd.h>
  5 #include <arpa/inet.h>
  6 #include <netinet/in.h>
  7 #include <string.h>
  8 
  9 #define IP "192.168.124.47"     //ifconfig查看
 10 #define PORT 8888              //端口号的网络字节序(在1014-49
 11 #define ERR_MSG(msg) do{\
 12     fprintf(stderr,"line=%d \n",__LINE__);\
 13     perror(msg);\
 14 }while(0)
 15 int main(int argc, const char *argv[])
 16 {
 17     //创建报式套接字
 18     int sfd = socket(AF_INET,SOCK_DGRAM,0);
 19     if (sfd < 0)
 20     {
 21         ERR_MSG("socket");
 22         return -1;
 23     }
 24     printf("流式套接字创建成功\n");
 25     //允许端口被快速使用
 26     int reuse = 1;
 27     if(setsockopt(sfd,SOL_SOCKET,SO_REUSEADDR,&reuse,sizeof(reuse))<0)
 28     {
 29         ERR_MSG("setsockopt");
 30         return -1;
 31     }
 32     printf("允许端口快速重用\n");
 33     //填充服务器的地址信息结构体,给bind函数使用
 34     //真实的地址信息结构体根据地址族制定,AF_INET-->main 7 ip
 35     struct sockaddr_in sin;
 36     sin.sin_family      = AF_INET;//必须填AF_INET(代表是流式套接字)
 37     sin.sin_port        = htons(PORT);//端口号的网络字节序
 38                                         //(htons代表是将主机字节序转换为网络字节序)
 39     sin.sin_addr.s_addr = inet_addr(IP);//本机IP,ifconfig查看,
 40                                         //inet_addr是.十进制转换为网络字节序
 41 
 42     //绑定服务器的地址信息,必须绑定
 43     if(bind(sfd,(struct sockaddr*)&sin,sizeof(sin)) < 0)
 44     {
 45         ERR_MSG("bind");
 46         return -1;
 47     }
 48     printf("绑定成功\n");
 49     char buf[128]="";
 50     //获取连接成功的客户端信息,生成一个新的套接字文件描述符
 51     struct sockaddr_in cin;
 52     socklen_t addrlen = sizeof(cin);
 53 
 54     while(1)
 55     {
 56         //接收数据
 57         bzero(buf,sizeof(buf));
 58         if(recvfrom(sfd,buf,sizeof(buf),0,(struct sockaddr*)&cin,&addrlen)<0);
 59         {
 60             ERR_MSG("recvform");
 61             return -1;
 62         }
 63 
 64         //发送
 65         strcat(buf,"*__*");
 66         if(sendto(sfd,buf,sizeof(buf),0,(struct sockaddr*)&cin,sizeof(cin))<0)
 67         {
 68             ERR_MSG("sendto");
 69             return -1;
 70         }
 71         printf("发送成功\n");
 72     }
 73 
 74     //关闭文件描述符
 75     if(close(sfd)< 0)
 76     {
 77         ERR_MSG("close");
 78         return -1;
 79     }
 80     printf("sfd关闭成功 \n");
 81 
 82 
 83 
 84     return 0;
 85 }
 86                                                                                                  

UDP客户端

 1 #include <stdio.h>
 2 #include <sys/types.h>
 3 #include <sys/socket.h>
 4 #include <unistd.h>
 5 #include <arpa/inet.h>
 6 #include <netinet/in.h>
 7 #include <string.h>
 8 
 9 #define IP "192.168.124.47"     //ifconfig查看
10 #define PORT 8888              //端口号的网络字节序(在1014-49
11 #define ERR_MSG(msg) do{\
12     fprintf(stderr,"line=%d \n",__LINE__);\
13     perror(msg);\
14 }while(0)
15 int main(int argc, const char *argv[])
16 {
17     //创建报式套接字
18     int sfd = socket(AF_INET,SOCK_DGRAM,0);
19     if (sfd < 0)
20     {
21         ERR_MSG("socket");
22         return -1;
23     }
24     printf("流式套接字创建成功\n");
25     /*
26     //允许端口被快速使用
27     int reuse = 1;
28     if(setsockopt(sfd,SOL_SOCKET,SO_REUSEADDR,&reuse,sizeof(reuse))<0)
29     {
30         ERR_MSG("setsockopt");
31         return -1;
32     }
33     printf("允许端口快速重用\n"); 
34     */
35     //填充服务器的地址信息结构体,给sendto函数使用
36     //代表将数据发送给指定的对端
37     struct sockaddr_in sin;
38     sin.sin_family      = AF_INET;//必须填AF_INET(代表是流式套接字)
39     sin.sin_port        = htons(PORT);//端口号的网络字节序,对端绑定的端口
40                                         //(htons代表是将主机字节序转换为网络字节序)
41     sin.sin_addr.s_addr = inet_addr(IP);//对端的IP,ifconfig查看,
42                                         //inet_addr是.十进制转换为网络字节序
43     /*
44     //绑定服务器的地址信息,必须绑定
45     if(bind(sfd,(struct sockaddr*)&sin,sizeof(sin)) < 0)
46     {
47         ERR_MSG("bind");
48         return -1;
49     }
50     printf("绑定成功\n");
51     */
52     char buf[128]="";
53     //获取连接成功的客户端信息,生成一个新的套接字文件描述符
54     struct sockaddr_in cin;
55     socklen_t addrlen = sizeof(cin);
56 
57     while(1)
58     {
59         bzero(buf,sizeof(cin));
60         //发送
61         printf("请输入>>>");
62         fgets(buf,sizeof(buf),stdin);
63         buf[strlen(buf)-1] = '\0';
64         if(sendto(sfd,buf,sizeof(buf),0,(struct sockaddr*)&sin,sizeof(sin))<0)
65         {
66             ERR_MSG("sendto");
67             return -1;
68         }
69         printf("发送成功\n");
70 
71         //接收数据
72 
73         bzero(buf,sizeof(buf));
74         if(recvfrom(sfd,buf,sizeof(buf),0,(struct sockaddr*)&sin,&addrlen)<0);
75         {
76             ERR_MSG("recvform");
77             return -1;
78         }
79         printf("[%s  %d]  %s\n",inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),buf);
80     }
81                                                                                                
82     //关闭文件描述符
83     if(close(sfd)< 0)
84     {
85         ERR_MSG("close");
86         return -1;
87     }
88     printf("sfd关闭成功 \n");
89 
90 
91 
92     return 0;
93 }
94 

 

 

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值