• hw0921


     多线程发送方

    1. #include
    2. int main(int argc, const char *argv[])
    3. {
    4. //创建流式套接字
    5. int cfd=socket(AF_INET,SOCK_STREAM,0);
    6. printf("socket success\n");
    7. //填充服务器地址
    8. struct sockaddr_in sin;
    9. sin.sin_family=AF_INET;
    10. sin.sin_port=htons(4444);
    11. sin.sin_addr.s_addr=inet_addr("192.168.125.230");
    12. //连接服务器
    13. if(connect(cfd,(struct sockaddr*)&sin,sizeof(sin))<0)
    14. {
    15. perror("connect");
    16. return -1;
    17. }
    18. printf("connect success\n");
    19. char buf[128]="";
    20. ssize_t res=0;
    21. while(1)
    22. {
    23. bzero(buf,sizeof(buf));
    24. printf("输入:");
    25. scanf(" %s",buf);
    26. // buf[strlen(buf)-1]='\0';
    27. write(cfd,buf,sizeof(buf));
    28. /* res=recv(cfd,buf,sizeof(buf),0);
    29. if(res<0)
    30. {
    31. perror("recv");
    32. }
    33. else if(0==res)
    34. {
    35. printf("服务器链接失败\n");
    36. break;
    37. }
    38. */ printf("[192.168.122.84,4444]:%s\n",buf);
    39. }
    40. close(cfd);
    41. return 0;
    42. }

    多线程接收方

    1. #include
    2. int main(int argc, const char *argv[])
    3. {
    4. //创建流式套接字
    5. int cfd=socket(AF_INET,SOCK_STREAM,0);
    6. printf("socket success\n");
    7. //填充服务器地址
    8. struct sockaddr_in sin;
    9. sin.sin_family=AF_INET;
    10. sin.sin_port=htons(4444);
    11. sin.sin_addr.s_addr=inet_addr("192.168.125.230");
    12. //连接服务器
    13. if(connect(cfd,(struct sockaddr*)&sin,sizeof(sin))<0)
    14. {
    15. perror("connect");
    16. return -1;
    17. }
    18. printf("connect success\n");
    19. char buf[128]="";
    20. ssize_t res=0;
    21. while(1)
    22. {
    23. bzero(buf,sizeof(buf));
    24. printf("输入:");
    25. scanf(" %s",buf);
    26. // buf[strlen(buf)-1]='\0';
    27. write(cfd,buf,sizeof(buf));
    28. /* res=recv(cfd,buf,sizeof(buf),0);
    29. if(res<0)
    30. {
    31. perror("recv");
    32. }
    33. else if(0==res)
    34. {
    35. printf("服务器链接失败\n");
    36. break;
    37. }
    38. */ printf("[192.168.122.84,4444]:%s\n",buf);
    39. }
    40. close(cfd);
    41. return 0;
    42. }
    43. ubuntu@ubuntu:TCP$ cat pth_ser.c
    44. #include
    45. #define PORT 4444
    46. #define IP "192.168.125.230"
    47. struct Climsg{
    48. int newfd;
    49. struct sockaddr_in cin;
    50. };
    51. int main(int argc, const char *argv[])
    52. {
    53. //创建流式套接字
    54. int sfd=socket(AF_INET,SOCK_STREAM,0);
    55. printf("socket success\n");
    56. //允许端口被重复使用
    57. int reuse=1;
    58. setsockopt(sfd,SOL_SOCKET,SO_REUSEADDR,&reuse,sizeof(reuse))<0;
    59. printf("允许端口被重复使用\n");
    60. //bind
    61. struct sockaddr_in sin;
    62. sin.sin_family=AF_INET;
    63. sin.sin_port=htons(PORT);
    64. sin.sin_addr.s_addr=inet_addr(IP);
    65. bind(sfd,(struct sockaddr*)&sin,sizeof(sin));
    66. printf("bind success\n");
    67. //listen
    68. listen(sfd,128);
    69. //存储客户端信息0
    70. struct sockaddr_in cin;
    71. socklen_t addrlen=sizeof(cin);
    72. int newfd;
    73. pthread_t tid;
    74. struct Climsg info;
    75. while(1)
    76. {
    77. newfd=accept(sfd,(struct sockaddr*)&cin,&addrlen);
    78. if(newfd<0)
    79. {
    80. perror("accept");
    81. return -1;
    82. }
    83. printf("connect success\n");
    84. printf("[%s:%d]\n",\
    85. inet_ntoa(cin.sin_addr),ntohs(cin.sin_port));
    86. info.newfd=newfd;
    87. info.cin=cin;
    88. pthread_create(&tid,NULL,deal_cli_msg,(void*)&info);
    89. pthread_detach(tid);//分离线程
    90. }
    91. close(sfd);
    92. return 0;
    93. }
    94. void* deal_cli_msg(void *info)
    95. {
    96. int newfd=((struct Climsg*)info)->newfd;
    97. struct sockaddr_in cin=((struct Climsg*)info);
    98. char buf[128];
    99. ssize_t res=0;
    100. while(1)
    101. {
    102. bzero(buf,sizeof(buf));
    103. //接收数据
    104. res=recv(newfd,buf,sizeof(buf),0);
    105. if(res<0)
    106. {
    107. perror("recv");
    108. break;
    109. }
    110. else if(0==res)
    111. {
    112. printf("[%s:%d]客户端下线\n",inet_ntoa(cin.sin_addr),ntohs(cin.sin_port));
    113. break;
    114. }
    115. printf("[%s:%d]:%s\n",inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),buf);
    116. if(strcmp(buf,"quit")==0)
    117. {
    118. break;
    119. }
    120. //发送数据
    121. strcat(buf,"-_-_--");
    122. if(send(newfd,buf,sizeof(buf),0)<0)
    123. {
    124. perror("send");
    125. break;
    126. }printf("发送成功\n");
    127. }
    128. close(newfd);
    129. }

    多进程发送方

    1. #include
    2. int main(int argc, const char *argv[])
    3. {
    4. //创建流式套接字
    5. int cfd=socket(AF_INET,SOCK_STREAM,0);
    6. printf("socket success\n");
    7. //填充服务器地址
    8. struct sockaddr_in sin;
    9. sin.sin_family=AF_INET;
    10. sin.sin_port=htons(4444);
    11. sin.sin_addr.s_addr=inet_addr("192.168.125.230");
    12. //连接服务器
    13. if(connect(cfd,(struct sockaddr*)&sin,sizeof(sin))<0)
    14. {
    15. perror("connect");
    16. return -1;
    17. }
    18. printf("connect success\n");
    19. char buf[128]="";
    20. ssize_t res=0;
    21. while(1)
    22. {
    23. bzero(buf,sizeof(buf));
    24. printf("输入:");
    25. scanf(" %s",buf);
    26. // buf[strlen(buf)-1]='\0';
    27. write(cfd,buf,sizeof(buf));
    28. /* res=recv(cfd,buf,sizeof(buf),0);
    29. if(res<0)
    30. {
    31. perror("recv");
    32. }
    33. else if(0==res)
    34. {
    35. printf("服务器链接失败\n");
    36. break;
    37. }
    38. */ printf("[192.168.122.84,4444]:%s\n",buf);
    39. }
    40. close(cfd);
    41. return 0;
    42. }

    多线程接收方

    1. ubuntu@ubuntu:TCP$ cat tcp_ser.c
    2. #include
    3. #define PORT 4444
    4. #define IP "192.168.125.230"
    5. void handler(int sig)
    6. {
    7. while(waitpid(-1,NULL,WNOHANG)>0);
    8. return;
    9. }
    10. int main(int argc, const char *argv[])
    11. {
    12. //17信号
    13. if(signal(SIGCHLD,handler)==SIG_ERR)
    14. {
    15. ERR_MSG("signal");
    16. return -1;
    17. }
    18. //创建流式套接字
    19. int sfd=socket(AF_INET,SOCK_STREAM,0);
    20. printf("socket success\n");
    21. //允许端口被重复使用
    22. int reuse=1;
    23. setsockopt(sfd,SOL_SOCKET,SO_REUSEADDR,&reuse,sizeof(reuse))<0;
    24. printf("允许端口被重复使用\n");
    25. //bind
    26. struct sockaddr_in sin;
    27. sin.sin_family=AF_INET;
    28. sin.sin_port=htons(PORT);
    29. sin.sin_addr.s_addr=inet_addr(IP);
    30. bind(sfd,(struct sockaddr*)&sin,sizeof(sin));
    31. printf("bind success\n");
    32. //listen
    33. listen(sfd,128);
    34. //存储客户端信息0
    35. struct sockaddr_in cin;
    36. socklen_t addrlen=sizeof(cin);
    37. int newfd;
    38. pid_t p;
    39. while(1)
    40. {
    41. newfd=accept(sfd,(struct sockaddr*)&cin,&addrlen);
    42. printf("1\n");
    43. if(newfd<0)
    44. {
    45. perror("accept");
    46. return -1;
    47. }
    48. printf("connect success\n");
    49. printf("[%s:%d]\n",\
    50. inet_ntoa(cin.sin_addr),ntohs(cin.sin_port));
    51. p=fork();
    52. if(p==0)
    53. {
    54. //接收
    55. char buf[128]="";
    56. size_t res=0;
    57. while(1)
    58. {
    59. bzero(buf,sizeof(buf));
    60. res=recv(newfd,buf,sizeof(buf),0);
    61. if(res<0)
    62. {
    63. perror("recv");
    64. }
    65. else if(0==res)
    66. {
    67. printf("客户端链接失败,客戶端IP:%s\n,客戶端端口:%d\n",\
    68. inet_ntoa(cin.sin_addr),ntohs(cin.sin_port));
    69. break;
    70. }
    71. printf("[%s:%d]:%s\n",inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),buf);
    72. }
    73. close(newfd);
    74. exit(0);
    75. }
    76. close(newfd);
    77. }
    78. return 0;
    79. }

    广播发送方

    1. #include
    2. #define PORT 4444
    3. #define IP "192.168.125.255"
    4. int main(int argc, const char *argv[])
    5. {
    6. //创建流式套接字
    7. int cfd=socket(AF_INET,SOCK_DGRAM,0);
    8. printf("socket success\n");
    9. //允许端口被重复使用
    10. int reuse=1;
    11. setsockopt(cfd,SOL_SOCKET,SO_REUSEADDR,&reuse,sizeof(reuse))<0;
    12. printf("允许端口被重复使用\n");
    13. int a=1;
    14. if(setsockopt(cfd,SOL_SOCKET,SO_BROADCAST,&a,sizeof(a))<0)
    15. {
    16. printf("setsockopt broadcast");
    17. }
    18. printf("a=%d\n",a);
    19. printf("设置广播成功\n");
    20. //服务器地址
    21. struct sockaddr_in sin;
    22. sin.sin_family=AF_INET;
    23. sin.sin_port=htons(PORT);
    24. sin.sin_addr.s_addr=inet_addr(IP);
    25. char buf[128]="";
    26. ssize_t res=0;
    27. socklen_t addrlen=sizeof(sin);
    28. while(1)
    29. {
    30. //发送
    31. bzero(buf,sizeof(buf));
    32. printf("输入:");
    33. scanf("%s",buf);
    34. sendto(cfd,buf,sizeof(buf),0,(struct sockaddr*)&sin,addrlen);
    35. printf("发送成功\n");
    36. //接收
    37. /* bzero(buf,sizeof(buf));
    38. res=recvfrom(cfd,buf,sizeof(buf),0,(struct sockaddr*)&sin,&addrlen);
    39. if(res<0)
    40. {
    41. perror("recv");
    42. }
    43. printf(":%s\n",buf);
    44. */
    45. }
    46. close(cfd);
    47. return 0;
    48. }

    广播接收方

    1. #include
    2. #define PORT 4444
    3. #define IP "192.168.125.255"
    4. int main(int argc, const char *argv[])
    5. {
    6. //创建流式套接字
    7. int sfd=socket(AF_INET,SOCK_DGRAM,0);
    8. printf("socket success\n");
    9. //允许端口被重复使用
    10. int reuse=1;
    11. setsockopt(sfd,SOL_SOCKET,SO_REUSEADDR,&reuse,sizeof(reuse))<0;
    12. printf("允许端口被重复使用\n");
    13. /* int size1=0;
    14. socklen_t len=sizeof(size1);
    15. getsockopt(sfd,SOL_SOCKET,SO_RCVBUF,&size1,&len);
    16. printf("size1=%d\n",size1);
    17. */ //bind
    18. struct sockaddr_in sin;
    19. sin.sin_family=AF_INET;
    20. sin.sin_port=htons(PORT);
    21. sin.sin_addr.s_addr=inet_addr(IP);
    22. bind(sfd,(struct sockaddr*)&sin,sizeof(sin));
    23. printf("bind success\n");
    24. //客户端地址
    25. struct sockaddr_in cin;
    26. char buf[128]="";
    27. ssize_t res=0;
    28. socklen_t addrlen=sizeof(cin);
    29. while(1)
    30. {
    31. //接收
    32. bzero(buf,sizeof(buf));
    33. res=recvfrom(sfd,buf,sizeof(buf),0,(struct sockaddr*)&cin,&addrlen);
    34. if(res<0)
    35. {
    36. perror("recv");
    37. }
    38. printf(":%s\n",buf);
    39. //与发送信息的客户端绑定
    40. /* connect(sfd,(struct sockaddr*)&cin,addrlen);
    41. if(strcmp(buf,"quit")==0)
    42. {
    43. sin.sin_family=AF_UNSPEC;
    44. printf("断开连接\n");
    45. }
    46. //发送
    47. bzero(buf,sizeof(buf));
    48. printf("输入:");
    49. scanf("%s",buf);
    50. sendto(sfd,buf,sizeof(buf),0,(struct sockaddr*)&cin,addrlen);
    51. printf("发送成功\n");
    52. */
    53. }
    54. close(sfd);
    55. return 0;
    56. }

    组播接收方

    1. #include
    2. #define PORT 4444
    3. #define GIP "224.5.5.5"
    4. #define LIP "192.168.125.230"
    5. int main(int argc, const char *argv[])
    6. {
    7. //创建流式套接字
    8. int sfd=socket(AF_INET,SOCK_DGRAM,0);
    9. printf("socket success\n");
    10. //允许端口被重复使用
    11. int reuse=1;
    12. setsockopt(sfd,SOL_SOCKET,SO_REUSEADDR,&reuse,sizeof(reuse))<0;
    13. printf("允许端口被重复使用\n");
    14. //加入多播组
    15. struct ip_mreqn mq;
    16. mq.imr_multiaddr.s_addr=inet_addr(GIP);//组播ip
    17. mq.imr_address.s_addr=inet_addr(LIP);//本地ip
    18. mq.imr_ifindex=2;//网络设备索引号
    19. if(setsockopt(sfd,IPPROTO_IP,IP_ADD_MEMBERSHIP,&mq,sizeof(mq))<0)
    20. {
    21. perror("setsockopt(group)");
    22. return -1;
    23. }
    24. printf("加入多播组[%s]成功\n",GIP);
    25. //服务器地址
    26. struct sockaddr_in sin;
    27. sin.sin_family=AF_INET;
    28. sin.sin_port=htons(PORT);
    29. sin.sin_addr.s_addr=inet_addr(LIP);
    30. bind(sfd,(struct sockaddr*)&sin,sizeof(sin));
    31. printf("绑定地址信息成功\n");
    32. struct sockaddr_in cin;
    33. char buf[128]="";
    34. ssize_t res=0;
    35. socklen_t addrlen=sizeof(cin);
    36. while(1)
    37. {
    38. //接收
    39. bzero(buf,sizeof(buf));
    40. res=recvfrom(sfd,buf,sizeof(buf),0,(struct sockaddr*)&cin,&addrlen);
    41. if(res<0)
    42. {
    43. perror("recv");
    44. }
    45. printf(":%s\n",buf);
    46. }
    47. close(sfd);
    48. return 0;
    49. }

    组播发送方

    1. #include
    2. #define PORT 4444
    3. #define GIP "224.5.5.5"
    4. int main(int argc, const char *argv[])
    5. {
    6. //创建报式套接字
    7. int cfd=socket(AF_INET,SOCK_DGRAM,0);
    8. printf("socket success\n");
    9. //允许端口被重复使用
    10. int reuse=1;
    11. setsockopt(cfd,SOL_SOCKET,SO_REUSEADDR,&reuse,sizeof(reuse))<0;
    12. printf("允许端口被重复使用\n");
    13. //服务器地址
    14. struct sockaddr_in sin;
    15. sin.sin_family=AF_INET;
    16. sin.sin_port=htons(PORT);
    17. sin.sin_addr.s_addr=inet_addr(GIP);
    18. char buf[128]="";
    19. ssize_t res=0;
    20. socklen_t addrlen=sizeof(sin);
    21. while(1)
    22. {
    23. //发送
    24. bzero(buf,sizeof(buf));
    25. printf("输入:");
    26. scanf("%s",buf);
    27. sendto(cfd,buf,sizeof(buf),0,(struct sockaddr*)&sin,addrlen);
    28. printf("发送成功\n");
    29. //接收
    30. /* bzero(buf,sizeof(buf));
    31. res=recvfrom(cfd,buf,sizeof(buf),0,(struct sockaddr*)&sin,&addrlen);
    32. if(res<0)
    33. {
    34. perror("recv");
    35. }
    36. printf(":%s\n",buf);
    37. */
    38. }
    39. close(cfd);
    40. return 0;
    41. }

  • 相关阅读:
    关于electron中使用ffi-napi窗口遍历的过程及问题
    搬砖日记:vue2 用require引入图片项目编译失败
    Java基于B/S架构,包括PC后台管理端、APP移动端、可视化数据大屏的智慧工地源码
    人大金仓数据库创建与还原--九五小庞
    基于C#的Windows控制台的吃豆豆小游戏
    Win11拖拽文件偶现卡顿死机情况解决
    实现内网穿透的最佳解决方案(无实名认证,完全免费)
    进阶——旋转数组
    排序学习总结
    烽火传递——单调队列优化dp问题
  • 原文地址:https://blog.csdn.net/qiujunhang/article/details/133148231