一个linux UDP网络通讯的例子源代码(server、client方式)
来源:互联网 发布:tcp端口号 不够 编辑:程序博客网 时间:2024/06/05 03:23
服务器端代码
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
int main(int argc, char *argv[])
{
int sock;
//sendto中使用的对方地址
struct sockaddr_in toAddr;
//在recvfrom中使用的对方主机地址
struct sockaddr_in fromAddr;
int recvLen;
unsigned int addrLen;
char recvBuffer[128];
sock = socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP);
if(sock < 0)
{
printf("创建套接字失败了.\r\n");
exit(0);
}
memset(&fromAddr,0,sizeof(fromAddr));
fromAddr.sin_family=AF_INET;
fromAddr.sin_addr.s_addr=htonl(INADDR_ANY);
fromAddr.sin_port = htons(4000);
if(bind(sock,(struct sockaddr*)&fromAddr,sizeof(fromAddr))<0)
{
printf("bind() 函数使用失败了.\r\n");
close(sock);
exit(1);
}
while(1){
addrLen = sizeof(toAddr);
if((recvLen = recvfrom(sock,recvBuffer,128,0,(struct sockaddr*)&toAddr,&addrLen))<0)
{
printf("()recvfrom()函数使用失败了.\r\n");
close(sock);
exit(1);
}
if(sendto(sock,recvBuffer,recvLen,0,(struct sockaddr*)&toAddr,sizeof(toAddr))!=recvLen){
printf("sendto fail\r\n");
close(sock);
exit(0);
}
return 0;
}
}
客户端代码
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
int main(int argc, char *argv[])
{
if(argc < 2)
{
printf("请输入要传送的内容.\r\n");
exit(0);
}
int sock;
//sendto中使用的对方地址
struct sockaddr_in toAddr;
//在recvfrom中使用的对方主机地址
struct sockaddr_in fromAddr;
unsigned int fromLen;
char recvBuffer[128];
sock = socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP);
if(sock < 0)
{
printf("创建套接字失败了.\r\n");
exit(1);
}
memset(&toAddr,0,sizeof(toAddr));
toAddr.sin_family=AF_INET;
toAddr.sin_addr.s_addr=inet_addr("127.0.0.1");
toAddr.sin_port = htons(4000);
if(sendto(sock,argv[1],strlen(argv[1]),0,(struct sockaddr*)&toAddr,sizeof(toAddr)) != strlen(argv[1]))
{
printf("sendto() 函数使用失败了.\r\n");
close(sock);
exit(1);
}
fromLen = sizeof(fromAddr);
if(recvfrom(sock,recvBuffer,128,0,(struct sockaddr*)&fromAddr,&fromLen)<0)
{
printf("()recvfrom()函数使用失败了.\r\n");
close(sock);
exit(1);
}
printf("recvfrom() result:%s\r\n",recvBuffer);
close(sock);
return 0;
}
====
http://haibor1x.blog.163.com/blog/static/763407200751052458572/
- 一个linux UDP网络通讯的例子源代码(server、client方式
- 一个linux UDP网络通讯的例子源代码(server、client方式)
- 一个linux UDP网络通讯的例子源代码(server、client方式)
- 一个linux UDP网络通讯的例子源代码(server、client方式)
- 一个linux UDP网络通讯的例子源代码(server、client方式)
- 一个linux UDP网络通讯的例子源代码(server、client方式)
- 一个简单的Linux下Client/Server应答例子
- 一个简单的Linux下Client/Server应答例子
- linux 下的UDP client/server 示例
- nio udp server client Demo例子(五)
- 网络通讯方式 ------ TCP、UDP
- 编写Linux下的UDP Client/Server程序
- 编写Linux下的UDP Client/Server程序
- 编写Linux下的UDP Client/Server程序
- 编写Linux下的UDP Client/Server程序
- 编写Linux下的UDP Client/Server程序
- 编写Linux下的UDP Client/Server程序
- 编写Linux下的UDP Client/Server程序
- SQL Server DBA工作内容详解
- sigaction函数 - 改变信号行为
- Android 中Message,MessageQueue,Looper,Handler详解+实例
- Android中ListView分页加载数据
- asterisk拨号规则
- 一个linux UDP网络通讯的例子源代码(server、client方式)
- android Theme使用总结
- fedora 16安装gnochm阅读器
- 伪静态和静态混合设置出错,没有为扩展名.html
- Watched Ones
- java jvm defaultCharset问题
- 枚举如何使用?有什么用处? enum
- Linux内核源代码的阅读和工具介绍
- 103、Pro C 连接Oracle数据库和问题解决