分类: LINUX
2013-12-20 14:53:14
http://blog.sina.com.cn/s/blog_566baa7e0100ct4l.html
UDP编程的服务器端一般步骤是:
1、创建一个socket,用函数socket();
3、绑定IP地址、端口等信息到socket上,用函数bind();
4、循环接收发送数据,用函数recvfrom()、sendto();
5、关闭网络连接;
UDP编程的客户端一般步骤是:
1、创建一个socket,用函数socket();
2、设置对方的IP地址和端口等属性;
3、连接服务器,用函数connect();
若是只是一次发送、接收数据,此函数可不用;
若是循环发送接收,则此处要用connect函数;因为若不用,当sendto()后这时服务器器kill掉,则 client端阻塞在recvfrom处;
4、循环发送接收数据,用函数sendto()、recvfrom();
5、关闭网络连接;
例子:
1、服务器:
SOCKADDR_IN sockAddr;
SocketClose(m_socketId);
//m_socketId = socket (AF_INET, SOCK_STREAM, 0);
m_socketId = socket (AF_INET, SOCK_DGRAM, 0);
if(INVALID_SOCKET == m_socketId)
{
return -1;
}
memset(&sockAddr,0,sizeof(sockAddr));
sockAddr.sin_family = AF_INET;
if (strIp == NULL)
{
//列举ip
}
else
{
//将常用的用点分开的IP地址转换为unsigned long类型的IP地址
int lResult = inet_addr(strIp);
if (lResult == INADDR_NONE)
{
return -2;
}
sockAddr.sin_addr.s_addr = lResult;
}
sockAddr.sin_port = htons((u_short)nPort);
if(bind(m_socketId, (SOCKADDR* )&sockAddr, sizeof(sockAddr)))
{
return -3;
}
struct sockaddr_in cliaddr;
//m_pClient.sockfd = sock;
2、客户端:
#include
#include "parainput.h"
int main(int argc, char **argv)
//输入参数处理
while(1)
socklen_t src_len;
short len;
char clientdata[255];
char serverdata[65535];
int iret;
//客户端数据接收
memset(clientdata,0,sizeof(clientdata));
src_len = sizeof(cliaddr);
iret = recvfrom(m_socketId, clientdata, sizeof(clientdata), 0, (struct sockaddr *)&cliaddr, &src_len);
if(iret< 0)
{
return 0;
}
len = iret;
//printf("clientdata:=["); for(int i=0;i
strcpy(m_pClient.ip,inet_ntoa(cliaddr.sin_addr));
m_pClient.port = cliaddr.sin_port;
m_pClient.lastTime = m_pClient.connectTime = time(NULL);
//DoClientConnect(&m_pClient);
//客户端数据处理
memset(serverdata,0,sizeof(serverdata));
len=ClientDataTypeService(clientdata,serverdata);
//printf("serverdata:=[%s]\n",serverdata);
//服务器数据发送
iret = sendto(m_socketId, serverdata, len, 0, (struct sockaddr *)&cliaddr, sizeof(struct sockaddr));
if(iret < 0)
{
return 0;
}
#include
#include
#include
#include
{
int sockfd;
socklen_t src_len;
socklen_t len;
struct sockaddr_in dest_addr;
char send_msg[SOCKET_BLOCK], rece_msg[SOCKET_BLOCK];
int send_msg_len;
int iret,i;
if (g_pParaInput.parainput(argc,argv)) return 0;
if((sockfd=socket(AF_INET,SOCK_DGRAM,0))==-1)
{
perror("socket creat failed!\n");
exit(1);
}
bzero(&dest_addr, sizeof(dest_addr));
dest_addr.sin_family = AF_INET;
dest_addr.sin_port = htons(g_pParaInput.serverport);
if(inet_aton(g_pParaInput.serveradd, &dest_addr.sin_addr) < 0)
{
printf("[%s] is not a valid IPaddress\n", g_pParaInput.serveradd);
exit(1);
}
if (connect(sockfd,(struct sockaddr *)&dest_addr,sizeof(dest_addr))<0)
{
printf("Connect error\n");
shutdown(sockfd,2);
close(sockfd);
exit(1);
}
{
sleep(30);
for(i=1;i<=8;i++)
{
memset(send_msg,0,sizeof(send_msg));
send_msg_len = 3;
memcpy(send_msg,&send_msg_len,2);
sprintf(send_msg+2,"%c",i);
printf("send_msg:[%02x %02x %02x]\n",send_msg[0],send_msg[1],send_msg[2]);
if(sendto(sockfd, send_msg, send_msg_len, 0, (struct sockaddr *)&dest_addr, sizeof(struct sockaddr_in)) < 0)
{
perror("sendto error!\n");
break;;
exit(1);
}
src_len = sizeof(dest_addr);
memset(rece_msg,0,sizeof(rece_msg));
iret = recvfrom(sockfd, rece_msg, sizeof(rece_msg), 0, (struct sockaddr *)&dest_addr, &src_len);
if(iret < 0)
{
perror("receive error!\n");
break;;
exit(0);
}
printf("%s\n",rece_msg);
}
}
shutdown(sockfd,2);
close(sockfd);
return 0;
}