基于UDP的socket编程

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了基于UDP的socket编程相关的知识,希望对你有一定的参考价值。

一.相关函数说明

    UDP是无连接的,即发送数据之前不需要建立连接。

    除了基于TCP中的socket编程所需的函数之外,基于UDP的socket编程中还需要用到两个函数。

    1.sendto函数:用于客户端中指定一目的地发送数据。

    (1)函数原型

    技术分享

    (2)参数说明

    sockfd:套接字

    buf:待发送数据的缓冲区

    len:缓冲区长度

    flags:调用方式标志位,一般为0;若改变flags,则sendto发送数据的形式会变成阻塞

  dest_addr:指向目的套接字的地址

    addrlen:指向目的套接字的长度

    (3)返回值

    技术分享

    2.recvfrom函数:用于服务器端从套接口上接受数据。

    (1)函数原型

    技术分享

    (2)参数说明

    sockfd:套接字

    buf:待发送数据的缓冲区

    len:缓冲区长度

    flags:调用方式标志位,一般为0;若改变flags,则sendto发送数据的形式会变成阻塞

    dest_addr:指向源套接字的地址

    addrlen:指向源套接字的长度

    (3)返回值

    技术分享

 二.程序实现

    server端:

  1 #include<stdio.h>
  2 #include<stdlib.h>
  3 #include<errno.h>
  4 #include<string.h>
  5 #include<netinet/in.h>
  6 #include<arpa/inet.h>
  7 #include<sys/types.h>
  8 #include<sys/socket.h>
  9 
 10 void usage(char *proc)
 11 {
 12         printf("%s [ip][port]\n",proc);
 13 }
 14 
 15 int main(int argc,char *argv[])
 16 {
 17         if(argc!=3)
 18         {
 19                 usage(argv[0]);
 20                 exit(1);
 21         }
 22         int sock=socket(AF_INET,SOCK_DGRAM,0);
 23         if(sock<0)
 24         {
 25                 perror("socket");
 26                 exit(2);
 27         }
 28         int port=atoi(argv[2]);
 29         char *ip=argv[1];
 30 
 31         struct sockaddr_in local;
 32         local.sin_family=AF_INET;
 33         local.sin_port=htons(port);
 34         local.sin_addr.s_addr=inet_addr(ip);
 35 
 36         if(bind(sock,(struct sockaddr*)&local,sizeof(local))<0)
 37         {
 38                 perror("bind");
 39                 exit(3);
 40         }
 41 
 42         char buf[1024];
 43         struct sockaddr_in remote;
 44         socklen_t len=sizeof(remote);
 45         while(1)
 46         {
 47                 memset(buf,‘\0‘,sizeof(buf));
 48                 ssize_t _s=recvfrom(sock,buf,sizeof(buf)-1,0,(struct sockadd    r*)&remote,&len);
 49                 char *client_ip=inet_ntoa(remote.sin_addr);
 50                 int client_port=ntohs(remote.sin_port);
 51                 if(_s<0)
 52                 {
 53                         perror("recvfrom");
 54                         break;
 55                 }
 56                 else if(_s==0)
 57                 {
 58                         printf("client %s close...\n",client_ip);
 59                         exit(4);
 60                 }
 61                 else
 62                 {
 63                         printf("client:[ip:%s][port:%d]:%s",client_ip,client    _port,buf);
 64                 }
 65 
 66         }
 67         close(sock);
 68         return 0;
 69 }

    client端:

  1 #include<stdio.h>
  2 #include<stdlib.h>
  3 #include<string.h>
  4 #include<errno.h>
  5 #include<netinet/in.h>
  6 #include<arpa/inet.h>
  7 #include<sys/types.h>
  8 #include<sys/socket.h>
  9 
 10 void usage(char *proc)
 11 {
 12         printf("%s [ip][port]\n",proc);
 13 }
 14 
 15 int main(int argc,char *argv[])
 16 {
 17         if(argc!=3)
 18         {
 19                 usage(argv[0]);
 20                 exit(1);
 21         }
 22         int sock=socket(AF_INET,SOCK_DGRAM,0);
 23         if(sock<0)
 24         {
 25                 perror("socket");
 26                 exit(2);
 27         }
 28         char *ip=argv[1];
 29         int port=atoi(argv[2]);
 30 
 31         struct sockaddr_in client;
 32         client.sin_family=AF_INET;
 33         client.sin_port=htons(port);
 34         client.sin_addr.s_addr=inet_addr(ip);
 35 
 36         char buf[1024];
 37         while(1)
 38         {
 39                 memset(buf,‘\0‘,sizeof(buf));
 40                 ssize_t _s=read(0,buf,sizeof(buf)-1);
 41                 _s=sendto(sock,buf,sizeof(buf)-1,0,(struct sockaddr*)&client    ,sizeof(client));
 42         }
 43         return 0;
 44 }

    实现结果:

    技术分享

本文出自 “zwy” 博客,请务必保留此出处http://10548195.blog.51cto.com/10538195/1789329

以上是关于基于UDP的socket编程的主要内容,如果未能解决你的问题,请参考以下文章

基于UDP协议之——socket编程

linux网络编程之用socket实现简单客户端和服务端的通信(基于UDP)

基于UDP的socket编程

基于UDP的socket编程

基于UDP协议的socket套接字编程

网络 基于UDP协议的socket编程