博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
connect()在UDP连接中的应用一例
阅读量:6200 次
发布时间:2019-06-21

本文共 3240 字,大约阅读时间需要 10 分钟。

服务器:

1:   
2:  #include 
3:  #include 
4:  #include 
5:  #include 
6:  #include 
7:  #include 
8:  #include 
9:   
10:   
11:  #define N 256
12:   
13:  typedef struct sockaddr SA;
14:   
15:  int main(int argc, char* argv[])
16:  {
17:      int sockfd;
18:      struct sockaddr_in my_addr, peer_addr;
19:      socklen_t peerlen = sizeof(peer_addr);
20:      char buffer[N];
21:   
22:      if((sockfd = socket(PF_INET, SOCK_DGRAM, 0)) < 0)
23:      {
24:          perror("fail to socket");
25:          exit(-1);
26:      }
27:   
28:     my_addr.sin_family = PF_INET;
29:     my_addr.sin_port = htons(atoi(argv[2]));
30:     my_addr.sin_addr.s_addr = inet_addr(argv[1]);
31:   
32:     if(bind(sockfd, (SA *)&my_addr, sizeof(my_addr)) < 0)
33:     {
34:         perror("fail to bind");
35:         exit(-1);
36:     }
37:
38:      while(1)
39:      {
40:   
41:          recvfrom(sockfd, buffer, N, 0,(SA*)&peer_addr, &peerlen);
42:          printf("client: [%s : %d], %s",inet_ntoa(peer_addr.sin_addr),ntohs(peer_addr.sin_port),buffer);
43:          sendto(sockfd, buffer, N, 0, (SA *)&peer_addr, sizeof(peer_addr));
44:          bzero(buffer, N);
45:      }
46:
47:   
48:      return 0;
49:  }

 

 

客户端:

1:  #include 
2:  #include 
3:  #include 
4:  #include 
5:  #include 
6:  #include 
7:  #include 
8:  #include 
9:   
10:   
11:  #define N 256
12:   
13:  typedef struct sockaddr SA;
14:   
15:  int main(void)
16:  {
17:      int sockfd;
18:      struct sockaddr_in my_addr;
19:      char buffer[N];
20:   
21:      if((sockfd = socket(PF_INET, SOCK_DGRAM, 0)) < 0)
22:      {
23:          perror("fail to socket");
24:          exit(-1);
25:      }
26:   
27:   
28:
29:      while(1)
30:      {
31:   
32:          bzero(buffer, N);
33:          printf("exit? Y/N");
34:          fgets(buffer, N, stdin);
35:          if(strncmp(buffer, "Y", 1) == 0)
36:          {
37:              break;
38:          }
39:          else
40:          {
41:              bzero(&my_addr,sizeof(my_addr));
42:              my_addr.sin_family = PF_INET;
43:
44:              printf("please input server's ip > ");
45:              fgets(buffer, N, stdin);
46:              buffer[strlen(buffer) -1] = '\0';
47:              my_addr.sin_addr.s_addr = inet_addr(buffer);
48:   
49:              printf("please input server's port > ");
50:              fgets(buffer, N, stdin);
51:              buffer[strlen(buffer) -1] = '\0';
52:              my_addr.sin_port = htons(atoi(buffer));
53:   
54:              connect(sockfd, (SA *)&my_addr, sizeof(my_addr));
55:              while(1)
56:              {
57:                  fgets(buffer, N, stdin);
58:                  if(strncmp(buffer, "quit", 4) == 0)
59:                  {
60:                      break;
61:                  }
62:
63:                  send(sockfd, buffer, N, 0);
64:                  bzero(buffer, N);
65:                  recv(sockfd, buffer, N, 0);
66:                  printf("receive from the server > %s",buffer);
67:              }
68:          }
69:      }
70:
71:      close(sockfd);
72:      return 0;
73:  }

 

 

运行:

运行了两个服务器:

客户端:

这样,通过在UDP中使用connect(),省去了每次都输入服务器地址的操作,而且一个在这种情况下,一个套接字在不关闭的情况下可以实现多次调用connect()。

转载地址:http://grvca.baihongyu.com/

你可能感兴趣的文章
Web Worker
查看>>
第一篇python
查看>>
SVN自动化备份、恢复脚本(上)
查看>>
ospf协议基本概念
查看>>
oracle查看被锁的表和解锁的方法
查看>>
找了篇乐观锁悲观锁的文章学习一下
查看>>
Mysql,zip格式安装、修改密码、建库
查看>>
Linux笔记之chown命令
查看>>
LVS DR模式搭建
查看>>
浅析利用Hessian跨Web网站发送数据包
查看>>
Linux学习笔记(六)相对和绝对路径、cd命令 、创建和删除目录mkdir/rmdir、rm命令...
查看>>
行业大佬预测UBT将冲上3万元
查看>>
Remove MailboxDatabase operation fails to clean up
查看>>
域名注册、域名实名认证、域名解析流程详解
查看>>
「小程序」的崛起和未来在哪
查看>>
TCP应用编程
查看>>
centos7中yum安装ntfs-3g
查看>>
文件(list)的基本操作 1.1
查看>>
jenkins 未授权访问-任意命令执行
查看>>
7-10 基本Java知识
查看>>