• 网络编程:基于TCP和UDP的服务器、客户端


    1.基于TCP通信服务器

    程序代码:

    1. 1 #include
    2. 2 #define SER_IP "192.168.126.121"//服务器IP
    3. 3 #define SER_PORT 8888//服务器端口号
    4. 4 int main(int argc, const char *argv[])
    5. 5 {
    6. 6 //1.创建用于监听的套接字
    7. 7 int sfd=-1;
    8. 8 sfd=socket(AF_INET,SOCK_STREAM,0);
    9. 9 //参数1:IPv4的通信
    10. 10 //参数2:使用TCP通信类型
    11. 11 //参数3:参数2以确定通信类型,0
    12. 12 if(sfd==-1)
    13. 13 {
    14. 14 perror("socket error");
    15. 15 return -1;
    16. 16 }
    17. 17 printf("sfd=%d\n",sfd);//3 返回文件描述符,最小分配原则
    18. 18 //2.绑定IP地址和端口号
    19. 19 //2.1填充地址信息结构体
    20. 20 struct sockaddr_in sin;
    21. 21 sin.sin_family=AF_INET;//地址族
    22. 22 sin.sin_port=htons(SER_PORT);//要2字节无符号整数端口号(网络字节序)
    23. 23 //将主机字节序转换为网络字节序
    24. 24 sin.sin_addr.s_addr=inet_addr(SER_IP);//IP地址(sin的sin_addr的s_addr)
    25. 25 //要IP地址的网络字节序,将点分十进制数据转换为4字节无符号整数的网络字节序
    26. 26 //2.2绑定
    27. 27 if(bind(sfd,(struct sockaddr *)&sin,sizeof(sin))==-1)
    28. 28 //要绑定的套接字文件描述符,通用地址信号结构体,结构体大小
    29. 29 {
    30. 30 perror("bind error");
    31. 31 return -1;
    32. 32 }
    33. 33 printf("bind success\n");
    34. 34 //3.启动监听,允许客户端连接
    35. 35 if(listen(sfd,128)==-1)
    36. 36 {
    37. 37 perror("listen error");
    38. 38 return -1;
    39. 39 }
    40. 40 printf("listen success\n");
    41. 41 //4.客户端发来连接请求后,创建新的用于通信的套接字
    42. 42 //不想接收客户端地址信息结构体,则无需传参2,参3
    43. 43 //想要获取客户端地址信息结构体,要传入相关参数
    44. 44 struct sockaddr_in cin;//用于接收客户端地址信息结构体
    45. 45 socklen_t socklen=sizeof(cin);//用于接收客户端地址信息大小
    46. 46 int newfd=accept(sfd,(struct sockaddr*)&cin,&socklen);
    47. 47 //阻塞等待客户端请求,客户端发来连接请求后,创建新的套接字,返回套接字文件描述符
    48. 48 //参数1:服务器套接字文件描述符
    49. 49 //参数2:通用地址信号结构体,接收最新连接的客户端地址信息
    50. 50 //参数3:客户端套接字大小,地址传递
    51. 51 if(newfd==-1)//成功返回套接字文件描述符,失败-1
    52. 52 {
    53. 53 perror("accept error");
    54. 54 return -1;
    55. 55 }
    56. 56 printf("newfd=%d您有新的客户已经上线\n",newfd);
    57. 57 printf("客户端IP:%s,端口号:%d\n",inet_ntoa(cin.sin_addr),ntohs(cin.sin_port));
    58. 58 //将IP地址的网络字节序(4字节无符号整数)转换为点分十进制字符串(IP地址的网络字节序)
    59. 59 //将端口的网络字节序(2字节无符号整数)转换为主机字节序(端口的网络字节序)
    60. 60 //5.通信套接字与客户端进行数据收发
    61. 61 char rbuf[128]="";
    62. 62 while(1)
    63. 63 {
    64. 64 //清空容器
    65. 65 bzero(rbuf,sizeof(rbuf));
    66. 66 //从套接字中读取数据
    67. 67 int res=read(newfd,rbuf,sizeof(rbuf));
    68. 68 if(res==0)
    69. 69 {
    70. 70 printf("客户端已下线\n");
    71. 71 break;
    72. 72 }
    73. 73 printf("[%s:%d]:%s\n",inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),rbuf);
    74. 74 //将收到的消息加上其他字符回过去
    75. 75 strcat(rbuf,"*_*");
    76. 76 write(newfd,rbuf,strlen(rbuf));
    77. 77 }
    78. 78 //6.关闭套接字
    79. 79 close(newfd);
    80. 80 close(sfd);
    81. 81 return 0;
    82. 82 }

    运行结果:

    2. 基于TCP通信客户端

    程序代码:

    1. 1 #include
    2. 2 #define SER_IP "192.168.126.121"//服务器IP
    3. 3 #define SER_PORT 8888//服务器端口号
    4. 4 int main(int argc, const char *argv[])
    5. 5 {
    6. 6 //1.创建用于通信的套接字文件描述符
    7. 7 int cfd=socket(AF_INET,SOCK_STREAM,0);
    8. 8 //使用IPv4通信,使用TCP通信类型,已确定通信类型0
    9. 9 if(cfd==-1)
    10. 10 {
    11. 11 perror("socket error");
    12. 12 return -1;
    13. 13 }
    14. 14 printf("cfd=%d\n",cfd);//返回文件描述符,最小分配3
    15. 15 //2.绑定(不写系统默认绑定)
    16. 16 //3.连接服务器
    17. 17 //3.1填充要连接的服务器地址信息结构体
    18. 18 struct sockaddr_in sin;
    19. 19 sin.sin_family=AF_INET;//地址族
    20. 20 sin.sin_port=htons(SER_PORT);//端口号
    21. 21 sin.sin_addr.s_addr=inet_addr(SER_IP);//IP地址
    22. 22 //3.2连接服务器
    23. 23 if(connect(cfd,(struct sockaddr*)&sin,sizeof(sin))==-1)
    24. 24 {
    25. 25 perror("connect error");
    26. 26 return -1;
    27. 27 }
    28. 28 printf("connect success\n");
    29. 29 //4数据收发
    30. 30 char wbuf[128]="";
    31. 31 while(1)
    32. 32 {
    33. 33 //清空数据
    34. 34 bzero(wbuf,sizeof(wbuf));
    35. 35 printf("请输入>>>");
    36. 36 fgets(wbuf,sizeof(wbuf),stdin);//终端输入
    37. 37 wbuf[strlen(wbuf)-1]=0;
    38. 38 //将数据发送给服务器
    39. 39 send(cfd,wbuf,strlen(wbuf),0);
    40. 40 printf("发送成功\n");
    41. 41 if(strcmp(wbuf,"quit")==0)
    42. 42 break;
    43. 43 //接收服务器发来的消息
    44. 44 //清空数据
    45. 45 bzero(wbuf,sizeof(wbuf));
    46. 46 recv(cfd,wbuf,sizeof(wbuf),0);
    47. 47 printf("收到消息为;%s\n",wbuf);
    48. 48 }
    49. 49 //5.关闭套接字
    50. 50 close(cfd);
    51. 51 return 0;
    52. 52 }
    53. ~

    运行结果:

    3.基于UDP通信服务器

    程序代码:

    1. 1 #include
    2. 2 #define SER_IP "192.168.126.121"//服务器IP
    3. 3 #define SER_PORT 8888//服务器端口号
    4. 4 int main(int argc, const char *argv[])
    5. 5 {
    6. 6 //1.创建用于通信的套接字
    7. 7 int sfd=socket(AF_INET,SOCK_DGRAM,0);
    8. 8 if(sfd==-1)
    9. 9 {
    10. 10 perror("socket error");
    11. 11 return -1;
    12. 12 }
    13. 13 printf("sfd=%d\n",sfd);
    14. 14 //2.绑定IP地址和端口号
    15. 15 //2.1填充地址信息结构体
    16. 16 struct sockaddr_in sin;
    17. 17 sin.sin_family=AF_INET;//地址族
    18. 18 sin.sin_port=htons(SER_PORT);//端口号
    19. 19 sin.sin_addr.s_addr=inet_addr(SER_IP);//IP地址
    20. 20 //2.2绑定
    21. 21 if(bind(sfd,(struct sockaddr*)&sin,sizeof(sin))==-1)
    22. 22 {
    23. 23 perror("bind error");
    24. 24 return -1;
    25. 25 }
    26. 26 printf("bind success\n");
    27. 27 //3.收发数据
    28. 28 char rbuf[128]="";
    29. 29 //定义容器接收对端地址信息结构体
    30. 30 struct sockaddr_in cin;
    31. 31 socklen_t socklen=sizeof(cin);
    32. 32 while(1)
    33. 33 {
    34. 34 //清空数组
    35. 35 bzero(rbuf,sizeof(rbuf));
    36. 36 //接收数据
    37. 37 recvfrom(sfd,rbuf,sizeof(rbuf),0,(struct sockaddr*)&cin,&socklen);
    38. 38 printf("收到消息为:%s\n",rbuf);
    39. 39 //将消息加*_*回过去
    40. 40 strcat(rbuf,"*_*");
    41. 41 if(sendto(sfd,rbuf,strlen(rbuf),0,(struct sockaddr*)&cin,socklen)=
    42. 42 {
    43. 43 perror("sendto error");
    44. 44 return -1;
    45. 45 }
    46. 46 }
    47. 47 //4.关闭套接字
    48. 48 close(sfd);
    49. 49 return 0;
    50. 50 }

    运行结果:

    4.基于UDP的客户端

    程序代码:

    1. 1 #include
    2. 2 #define SER_IP "192.168.126.121"//IP地址
    3. 3 #define SER_PORT 8888//服务器端口
    4. 4 int main(int argc, const char *argv[])
    5. 5 {
    6. 6 //1.创建用于通信的套接字
    7. 7 int cfd=socket(AF_INET,SOCK_DGRAM,0);
    8. 8 if(cfd==-1)
    9. 9 {
    10. 10 perror("socket error");
    11. 11 return -1;
    12. 12 }
    13. 13 printf("cfd=%d\n",cfd);
    14. 14 //2.绑定IP地址和端口号
    15. 15 //非必要
    16. 16 //3.收发数据
    17. 17 char wbuf[128]="";
    18. 18 //定义容器,记录服务器的地址信息结构体
    19. 19 struct sockaddr_in sin;
    20. 20 sin.sin_family=AF_INET;
    21. 21 sin.sin_port=htons(SER_PORT);
    22. 22 sin.sin_addr.s_addr=inet_addr(SER_IP);
    23. 23 while(1)
    24. 24 {
    25. 25 //清空数组
    26. 26 bzero(wbuf,sizeof(wbuf));
    27. 27 //从终端获取数据
    28. 28 printf("请输入>>>");
    29. 29 fgets(wbuf,sizeof(wbuf),stdin);
    30. 30 wbuf[strlen(wbuf)-1]=0;
    31. 31 //将数据发送给服务器
    32. 32 sendto(cfd,wbuf,sizeof(wbuf),0,(struct sockaddr*)&sin,sizeof(sin));
    33. 33 printf("发送成功\n");
    34. 34 //接收服务器回复的消息
    35. 35 bzero(wbuf,sizeof(wbuf));
    36. 36 recvfrom(cfd,wbuf,sizeof(wbuf),0,NULL,NULL);
    37. 37 printf("收到消息:%s\n",wbuf);
    38. 38 }
    39. 39 //关闭套接字
    40. 40 close(cfd);
    41. 41 return 0;
    42. 42 }

    运行结果:

     流程图:

    面试:

     

  • 相关阅读:
    java毕业设计爱心互助及物品回收管理系统Mybatis+系统+数据库+调试部署
    巨好看的登录注册界面源码
    数据容器分类总结
    ajax异步传值以及后端接收参数的几种方式
    Linux编辑器-gcc的使用
    JAVA设计模式-桥接模式
    锂电池储能系统建模发展现状及其数据驱动建模初步探讨
    踩坑了!0作为除数,不一定会抛出异常!
    CMD shutdown命令
    清洁机器人--沿边测距传感器 sharp psd红外传感器的FOV角度分析
  • 原文地址:https://blog.csdn.net/2301_81513928/article/details/136382016