A-A+

Linux UDP socket编程(UDP通讯模型)

2013年06月04日 Linux 评论 2 条 阅读 928 次

Linux UDP socket编程(UDP通讯模型):

UDPClient + UDPService。

Linux下大多数网络程序都是基于TCP的,很少基于UDP,简单的通讯模型如下,开发时候备用!

udpService:

[cpp]

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <strings.h>
#include <unistd.h>
#include <string.h>
#include <ctype.h>

#define SERVER_PORT 2222
#define MAX_SIZE 1024

void UDP_answer(int sockfd)
{
struct sockaddr_in addr;
int i,n;
unsigned int addrlen;
char msg[MAX_SIZE];
while(1)
{
n=recvfrom(sockfd,msg,MAX_SIZE,0,(struct sockaddr *)&addr,&addrlen);
msg[n]=0;
fprintf(stdout,"i have received %s",msg);
n=strlen(msg);
for(i=0;i<n;i++)
msg[i]=toupper(msg[i]);
sendto(sockfd,msg,n,0,(struct sockaddr *)&addr,addrlen);
}
}

int main(void)
{
int sockfd;
struct sockaddr_in addr;
sockfd=socket(AF_INET,SOCK_DGRAM,0);
bzero(&addr,sizeof(struct sockaddr_in));
addr.sin_family=AF_INET;
addr.sin_addr.s_addr=htonl(INADDR_ANY);
addr.sin_port=htons(SERVER_PORT);
if(bind(sockfd,(struct sockaddr *)&addr,sizeof(struct sockaddr_in))<0)
{
perror("bind");
exit(1);
}
UDP_answer(sockfd);
close(sockfd);
return 0;
}

[/cpp]

udpClient:

[cpp]

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <errno.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <arpa/inet.h>

#define MAX_SIZE 1024

void UDP_ask(int sockfd,const struct sockaddr_in *addr,int len)
{
char buf[MAX_SIZE];
int n;
while(1)
{
fgets(buf,MAX_SIZE,stdin);//input from keyboard to service
sendto(sockfd,buf,strlen(buf),0,(struct sockaddr *)addr,len);
printf("i have sent to server %s",buf);
printf("Waiting answer from server\n");
bzero(buf,MAX_SIZE);
n=recvfrom(sockfd,buf,MAX_SIZE,0,NULL,NULL);
buf[n]=0;
printf("i have received from server");
fputs(buf,stdout);
printf("\n");
}
}

int main(int argc,char **argv)
{
int sockfd,port;
struct sockaddr_in addr;
if(argc!=3){
fprintf(stderr,"Usage:%s sercer_ip server_port\n",argv[0]);
exit(1);
}
if((port=atoi(argv[2]))<0){
fprintf(stderr,"Usage:%s server_ip server_port\n",argv[0]);
exit(1);
}
sockfd=socket(AF_INET,SOCK_DGRAM,0);
bzero(&addr,sizeof(struct sockaddr_in));
addr.sin_family=AF_INET;
addr.sin_port=htons(port);
if(inet_aton(argv[1],&addr.sin_addr)<0){
fprintf(stderr,"Ip error:%s\n",strerror(errno));
exit(1);
}
UDP_ask(sockfd,&addr,sizeof(struct sockaddr_in));
close(sockfd);
return 0;
}

[/cpp]

运行结果如下:

服务器在后台运行:./udpservice &

回显来自客服端的消息:略(ps可以查看后台进程PID)

客服端:

[cpp]

[root@localhost lab]# ./udpclient 127.0.0.1 2222
cplusplus
i have sent to server cplusplus
Waiting answer from server
i have received from serverCPLUSPLUS

hellowolrld
i have sent to server hellowolrld
Waiting answer from server
i have received from serverHELLOWOLRLD

[/cpp]

标签:

2 条留言  访客:1 条  博主:1 条

  1. john

    line 23:
    n=recvfrom(sockfd,msg,MAX_SIZE,0,(struct sockaddr *)&addr,&addrlen);

    参数addrlen没有初始化,导致recvfrom返回-1。不知道lz有没有发现。

    • admin

      感谢楼主指出,谢谢,稍后测试更正!

给我留言

Copyright © C/C++程序员之家 保留所有权利.   Theme  Ality 浙ICP备15011757号-3

用户登录