当前位置:   article > 正文

Linux下TCP、UDP、UDP广播通信代码及运行_linux怎设置udpping

linux怎设置udpping

一、网络环境配置

网络通信方式可选择一台主机里的两个虚拟机,也可以两台主机里的两个虚拟机,本文采用方式二。

主机一IP地址为192.168.1.109

主机二IP地址为192.168.1.24

必须使两台主机在一个网段内,并且可以ping通才可进行以下操作。

二、TCP连接

客户端(主机一)代码

TCP_client.c

  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <string.h>
  4. #include <stdlib.h>
  5. #include <arpa/inet.h>
  6. #include <sys/socket.h>
  7. #include <netinet/in.h>
  8. int main(int argc, char *argv[])
  9. {
  10. unsigned short port = 8000;
  11. char *server_ip = "192.168.1.24"; //设置第二个主机ip地址为服务器地址,进行TCP通信
  12. char send_buf[512] = "hello every!"; //设置发送的内容
  13. char recv_buf[512] = "";
  14. int sockfd = 0;
  15. int err_log = 0;
  16. struct sockaddr_in server_addr;
  17. if( argc > 1 )
  18. server_ip = argv[1];
  19. if( argc > 2 )
  20. port = atoi(argv[2]);
  21. bzero(&server_addr,sizeof(server_addr));
  22. server_addr.sin_family = AF_INET;
  23. server_addr.sin_port = htons(port);
  24. inet_pton(AF_INET, server_ip, &server_addr.sin_addr);
  25. sockfd = socket(AF_INET, SOCK_STREAM, 0);
  26. if(sockfd < 0)
  27. {
  28. perror("socket");
  29. exit(-1);
  30. }
  31. err_log = connect(sockfd, (struct sockaddr*)&server_addr, sizeof(server_addr));
  32. if(err_log != 0)
  33. {
  34. perror("connect");
  35. close(sockfd);
  36. exit(-1);
  37. }
  38. send(sockfd, send_buf, strlen(send_buf), 0);
  39. recv(sockfd, recv_buf, sizeof(recv_buf), 0);
  40. printf("%s\n", recv_buf);
  41. close(sockfd);
  42. return 0;
  43. }

服务器端(主机二)代码

TCP_server.c

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <unistd.h>
  5. #include <sys/socket.h>
  6. #include <netinet/in.h>
  7. #include <arpa/inet.h>
  8. int main(int argc, char *argv[])
  9. {
  10. char recv_buf[2048] = "";
  11. int sockfd = 0;
  12. int connfd = 0;
  13. int err_log = 0;
  14. struct sockaddr_in my_addr;
  15. unsigned short port = 8000;
  16. if(argc > 1)
  17. port = atoi(argv[1]);
  18. printf("TCP Server Started at port %d!\n", port);
  19. sockfd = socket(AF_INET, SOCK_STREAM, 0);
  20. if(sockfd < 0)
  21. {
  22. perror("socket");
  23. exit(-1);
  24. }
  25. bzero(&my_addr, sizeof(my_addr));
  26. my_addr.sin_family = AF_INET;
  27. my_addr.sin_port = htons(port);
  28. my_addr.sin_addr.s_addr = htonl(INADDR_ANY);
  29. printf("Binding server to port %d\n", port);
  30. err_log = bind(sockfd, (struct sockaddr*)&my_addr, sizeof(my_addr));
  31. if( err_log != 0)
  32. {
  33. perror("binding");
  34. close(sockfd);
  35. exit(-1);
  36. }
  37. err_log = listen(sockfd, 10);
  38. if(err_log != 0)
  39. {
  40. perror("listen");
  41. close(sockfd);
  42. exit(-1);
  43. }
  44. printf("Waiting client...\n");
  45. while(1)
  46. {
  47. size_t recv_len = 0;
  48. struct sockaddr_in client_addr;
  49. char cli_ip[INET_ADDRSTRLEN] = "";
  50. socklen_t cliaddr_len = sizeof(client_addr);
  51. connfd = accept(sockfd, (struct sockaddr*)&client_addr, &cliaddr_len);
  52. if(connfd < 0)
  53. {
  54. perror("accept");
  55. continue;
  56. }
  57. inet_ntop(AF_INET, &client_addr.sin_addr, cli_ip, INET_ADDRSTRLEN);
  58. printf("client ip = %s\n", cli_ip);
  59. while((recv_len = recv(connfd, recv_buf, sizeof(recv_buf), 0)) > 0)
  60. send(connfd, recv_buf, recv_len, 0);
  61. printf("%s\n",recv_buf);
  62. close(connfd);
  63. printf("client closed!\n");
  64. }
  65. close(sockfd);
  66. return 0;
  67. }

客户端输入代码

  1. gcc TCP_client.c -o TCP_client
  2. ./TCP_clent

服务器端输入代码

  1. gcc TCP_server.c -o TCP_server
  2. ./TCP_server

 三、UDP连接

 客户端(主机一)代码

UDP_client.c

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <unistd.h>
  5. #include <sys/socket.h>
  6. #include <netinet/in.h>
  7. #include <arpa/inet.h>
  8. int main (int argc, char *argv[])
  9. {
  10. int sockfd = 0;
  11. struct sockaddr_in server_addr;
  12. unsigned short port = 8000;
  13. char *ser_ip = "192.168.1.24";
  14. if(argc >1)
  15. ser_ip = argv[1];
  16. if(argc >2)
  17. port = atoi(argv[2]);
  18. sockfd = socket (AF_INET, SOCK_DGRAM, 0);
  19. if(sockfd < 0)
  20. {
  21. perror("socket error!");
  22. exit(-1);
  23. }
  24. bzero (&server_addr, sizeof(server_addr));
  25. server_addr.sin_family = AF_INET;
  26. server_addr.sin_port = htons(port);
  27. inet_pton(AF_INET, ser_ip,&server_addr.sin_addr);
  28. printf("ready send data to UDP server %s:%d!\n",ser_ip,port);
  29. while (1)
  30. {
  31. char send_buf[2048] = "";
  32. int len = 0;
  33. fgets(send_buf, sizeof(send_buf),stdin);
  34. send_buf[strlen(send_buf)-1] ='\0';
  35. sendto(sockfd, send_buf, strlen(send_buf),0,(struct sockaddr*)&server_addr,sizeof(server_addr));
  36. len = recvfrom(sockfd, send_buf, sizeof(send_buf),0,NULL,NULL);
  37. printf("%s\n",send_buf);
  38. }
  39. close(sockfd);
  40. return 0;
  41. }

服务器端(主机二)代码

UDP_server.c

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <unistd.h>
  5. #include <sys/socket.h>
  6. #include <netinet/in.h>
  7. #include <arpa/inet.h>
  8. int main(int argc, char *argv[])
  9. {
  10. int sockfd = 0;
  11. int error_log = 0;
  12. struct sockaddr_in bind_addr;
  13. unsigned short port = 8000;
  14. if (argc > 1)
  15. port = atoi(argv[1]);
  16. printf("UDP server Started!\n");
  17. sockfd = socket(AF_INET, SOCK_DGRAM,0);
  18. if (sockfd < 0)
  19. {
  20. perror("socket error");
  21. exit(-1);
  22. }
  23. bzero(&bind_addr, sizeof(bind_addr));
  24. bind_addr.sin_family = AF_INET;
  25. bind_addr.sin_port = htons(port);
  26. bind_addr.sin_addr.s_addr = htonl(INADDR_ANY);
  27. printf("Binding server to port %d\n", port);
  28. error_log = bind(sockfd, (struct sockaddr *)&bind_addr, sizeof(bind_addr));
  29. if (error_log != 0)
  30. {
  31. perror("bind error");
  32. close(sockfd);
  33. exit(-1);
  34. }
  35. printf("waiting data from other client...\n");
  36. while(1)
  37. {
  38. char recv_buf[1024] = "";
  39. char cli_ip[INET_ADDRSTRLEN] = "";
  40. int recv_len =0;
  41. struct sockaddr_in client_addr;
  42. socklen_t client_addr_len = sizeof(client_addr);
  43. recv_len = recvfrom(sockfd, recv_buf,sizeof(recv_buf), 0 ,(struct sockaddr*)&client_addr,&client_addr_len);
  44. inet_ntop(AF_INET, &client_addr.sin_addr,cli_ip,INET_ADDRSTRLEN);
  45. printf("clinet ip = %s\n", cli_ip);
  46. printf("recver message :%s\n",recv_buf);
  47. sendto(sockfd, recv_buf,recv_len,0,(struct sockaddr*)&client_addr,&client_addr_len);
  48. }
  49. close(sockfd);
  50. return 0;
  51. }

客户端输入代码

  1. gcc UDP_client.c -o UDP_client
  2. ./UDP_client
  3. hello

 服务器端输入代码

  1. gcc UDP_server.c -o UDP_server
  2. ./UDP_server

 四、UDP广播

发送端(主机一)代码

send.c

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <unistd.h>
  5. #include <sys/socket.h>
  6. #include <netinet/in.h>
  7. #include <sys/types.h>
  8. #include <arpa/inet.h>
  9. int main(){
  10. char msg[128] = "I am broadCast message from server!";
  11. struct sockaddr_in saddr, caddr;
  12. socklen_t slen, clen;
  13. int lfd, ret;
  14. lfd = socket(AF_INET, SOCK_DGRAM, 0);
  15. if(lfd < 0){
  16. perror("socket create error");
  17. exit(1);
  18. }
  19. int flag = 1;
  20. setsockopt(lfd, SOL_SOCKET, SO_BROADCAST | SO_REUSEADDR, &flag, sizeof(flag) );
  21. caddr.sin_family = AF_INET;
  22. caddr.sin_port = htons(4001);
  23. caddr.sin_addr.s_addr = inet_addr("192.168.1.255");
  24. clen = sizeof(caddr);
  25. int n = sendto(lfd, msg, strlen(msg), 0, (struct sockaddr *)&caddr, clen);
  26. if(n < 0){
  27. perror("sendto Error:");
  28. exit(1);
  29. }
  30. close(lfd);
  31. return 0;
  32. }

接收端(主机二)代码

recv.c

  1. #include<stdlib.h>
  2. #include<stdio.h>
  3. #include<string.h>
  4. #include<sys/types.h>
  5. #include<netinet/in.h>
  6. #include<netdb.h>
  7. #include<sys/socket.h>
  8. #include<sys/wait.h>
  9. #include<arpa/inet.h>
  10. int main(){
  11. int sockListen;
  12. if((sockListen = socket(AF_INET, SOCK_DGRAM, 0)) == -1){
  13. printf("socket fail\n");
  14. return -1;
  15. }
  16. int set = 1;
  17. setsockopt(sockListen, SOL_SOCKET, SO_REUSEADDR, &set, sizeof(int));
  18. struct sockaddr_in recvAddr;
  19. memset(&recvAddr, 0, sizeof(struct sockaddr_in));
  20. recvAddr.sin_family = AF_INET;
  21. recvAddr.sin_port = htons(4001);
  22. recvAddr.sin_addr.s_addr = INADDR_ANY;
  23. if(bind(sockListen, (struct sockaddr *)&recvAddr, sizeof(struct sockaddr)) == -1){
  24. printf("bind fail\n");
  25. return -1;
  26. }
  27. int recvbytes;
  28. char recvbuf[128];
  29. int addrLen = sizeof(struct sockaddr_in);
  30. if((recvbytes = recvfrom(sockListen, recvbuf, 128, 0,
  31. (struct sockaddr *)&recvAddr, &addrLen)) != -1){
  32. recvbuf[recvbytes] = '\0';
  33. printf("receive a broadCast messgse:%s\n", recvbuf);
  34. }else{
  35. printf("recvfrom fail\n");
  36. }
  37. close(sockListen);
  38. return 0;
  39. }

发送端输入代码

  1. gcc send.c -o send
  2. ./send

接收端输入代码

  1. gcc recv.c -o recv
  2. ./recv

  

本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号