下面给出udp通信的代码,服务器端和客户端都已经封装好,只要参考main函数中的方法使用即可。
服务器端:
udp_server.h
/******************************************************************** ** 文件名:udp_server.h ** 创建人:ss ** 完成日期:2010-11-16 ********************************************************************/ #ifndef UdpServer_CONNECT_H #define UdpServer_CONNECT_H #include <sys/types.h> #include <sys/socket.h> #include <string.h> #include <stdio.h> #include <netinet/in.h> #include <arpa/inet.h> #include <stdlib.h> #include <unistd.h> #include <errno.h> #include <malloc.h> typedef struct UdpServer UdpServer; struct UdpServer{ /* *函数介绍:接收信息 *输入参数:UdpServer 类型指针,msg接收消息指针,接收内存大小 *输出参数:msg接收到的消息,返回消息长度 *返回值: */ int (*recv)(UdpServer *this,char *msg,int len); /* *函数介绍:发送信息 *输入参数:UdpServer 类型指针,发送消息指针 *输出参数: *返回值: */ void (*send)(UdpServer *this,char *msg); /* *函数介绍:回收内存 *输入参数:UdpServer 类型指针 *输出参数: *返回值: */ void (*destory)(UdpServer *this); }; UdpServer* UdpServer_create(unsigned short port); #endif
?
udp_server.c
??
#include "udp_server.h" typedef struct private_UdpServer private_UdpServer; struct private_UdpServer { UdpServer public; int fd; struct sockaddr_in client; }; static int recv_msg(private_UdpServer *this,char *msg,int len) { int structlength = sizeof(struct sockaddr_in); memset(&this->client,0,structlength); int recvd = recvfrom(this->fd,msg,len,0,(struct sockaddr *) &this->client,&structlength); if(recvd < 0){ perror("recvfrom"); exit(EXIT_FAILURE); } else{ msg[recvd] = '\0'; } return recvd; } static void send_msg(private_UdpServer *this,char *msg) { int structlength = sizeof(struct sockaddr_in); int snd = sendto(this->fd,msg,strlen(msg),0,(struct sockaddr *)&this->client,structlength); if(snd < 0){ perror("sendto"); exit(1); } } static void destory(private_UdpServer *this){ free(this); } UdpServer* UdpServer_create(unsigned short port) { private_UdpServer *this=malloc(sizeof(private_UdpServer)); int sock; struct sockaddr_in server; int structlength; memset(&server,0,sizeof(server)); server.sin_family = AF_INET; server.sin_addr.s_addr = htonl(INADDR_ANY); server.sin_port = htons(port); if((sock = socket (AF_INET,SOCK_DGRAM,0)) < 0 ){ printf("socket create error!\n"); exit(1); } structlength = sizeof(server); if( bind(sock,(struct sockaddr *) &server,structlength) < 0){ printf("socket bind error!\n"); perror("bind"); exit(1); } this->fd = sock; this->public.recv=(int (*)(UdpServer *,char *,int ))recv_msg; this->public.send=(void (*)(UdpServer *,char *))send_msg; this->public.destory=(void (*)(UdpServer *))destory; } /* int main() { int sock; char msg[1024]; UdpServer *server=UdpServer_create(8000);// 创建Udp服务器,绑定ip为本机ip,绑定端口8000端口 while(1) { printf("wait .....\n"); server->recv(server,msg,1024); //接收信息 printf("%s",msg); if(strcmp(msg,"exit")==0) break; else server->send(server,msg); //发送信息 } server->destory(server); } */
?注:使用方法在参见udp_server.c中注释掉的main函数,可去掉注释运行。
?????? 在linux下输入: gcc -o server udp_server.c udp_server.h???
?????? 便可以在当前目录下生成 server可执行文件,运行即可。
??
?
?客户端:
?? udp_client.h
??
???
#include <stdlib.h> #include <unistd.h> #include <errno.h> #include <malloc.h> typedef struct UdpClient UdpClient; struct UdpClient{ /* *函数介绍:接收信息 *输入参数:UdpClient 类型指针,msg接收消息指针,接收内存大小 *输出参数:msg接收到的消息,返回消息长度 *返回值: */ int (*recv)(UdpClient *this,char *msg,int len); /* *函数介绍:发送信息 *输入参数:UdpClient 类型指针,发送消息指针 *输出参数: *返回值: */ void (*send)(UdpClient *this,char *msg); /* *函数介绍:回收内存 *输入参数:UdpClient 类型指针 *输出参数: *返回值: */ void (*destory)(UdpClient *this); }; UdpClient* UdpClient_create(char *ip,unsigned short port); #endif
?udp_client.c
??
#include "udp_client.h" typedef struct private_UdpClient private_UdpClient; struct private_UdpClient { UdpClient public; int fd; struct sockaddr_in server; }; static int recv_msg(private_UdpClient *this,char *msg,int len) { int structlength = sizeof(struct sockaddr_in); memset(&this->server,0,structlength); int recvd = recvfrom(this->fd,msg,len,0,(struct sockaddr *) &this->server,&structlength); if(recvd < 0){ perror("recvfrom"); exit(EXIT_FAILURE); } else{ msg[recvd] = '\0'; } return recvd; } static void send_msg(private_UdpClient *this,char *msg) { int structlength = sizeof(struct sockaddr_in); int snd = sendto(this->fd,msg,strlen(msg),0,(struct sockaddr *) &this->server,structlength); if(snd < 0){ perror("sendto"); exit(1); } } static void destory(private_UdpClient *this){ free(this); } UdpClient* UdpClient_create(char *ip,unsigned short port) { private_UdpClient *this=malloc(sizeof(private_UdpClient)); int sock; struct sockaddr_in server; int structlength; memset(&this->server,0,sizeof(this->server)); this->server.sin_family = AF_INET; this->server.sin_addr.s_addr = inet_addr(ip); this->server.sin_port = htons(port); if((sock = socket (AF_INET,SOCK_DGRAM,0)) < 0 ){ printf("socket create error!\n"); exit(1); } this->fd = sock; this->public.recv=(int (*)(UdpClient *,char *,int ))recv_msg; this->public.send=(void (*)(UdpClient *,char *))send_msg; this->public.destory=(void (*)(UdpClient *))destory; } /* int main() { int sock; char msg[1024]; UdpClient *client=UdpClient_create("192.168.101.201",8000); //创建发送信息到192.168.101.201:8000的客户端 while(1) { int len=read(0,msg,1024); msg[len]='\0'; client->send(client,msg); client->recv(client,msg,1024); printf("from server:%s",msg); } } */
?
?注:使用方法在参见udp_client.c中注释掉的main函数,可去掉注释运行。
?????? 在linux下输入: gcc -o?client udp_client.c udp_client.h???
?????? 便可以在当前目录下生成 client可执行文件,运行即可。
?
? 附件中包含全部代码