udp编程c
㈠ C语言 UDP socket 简单客户端 编程,急
提一下,你那个地址不好用,换成了127.0.0.1,端口可以用,完全按照要求写的,编译没错误,调试通过:
gcc server.c -o server
gcc client.c -o client
打开2个控制台:一个运行 ./server 另一个运行 ./client
server.c:
========================================
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <errno.h>
#define BUFFERSIZE 1024
typedef struct sockaddr SA;
int main(void)
{
char buf[BUFFERSIZE];
struct sockaddr_in addr_s;
struct sockaddr_in addr_c;
int sockfd;
socklen_t length;
int i;
if((sockfd = socket(AF_INET,SOCK_DGRAM,0)) == -1)
{
perror("socket fail");
return -1;
}
memset(&addr_s,0,sizeof(addr_s));
addr_s.sin_family = AF_INET;
addr_s.sin_addr.s_addr = inet_addr("127.0.0.1");
addr_s.sin_port = htons(31180);
if(bind(sockfd,(SA *)&addr_s,sizeof(addr_s)) == -1)
{
perror("bind fail");
return -1;
}
length = sizeof(addr_c);
memset(buf,'\0',sizeof(buf));
if(recvfrom(sockfd,buf,sizeof(buf),0
,(SA *)&addr_c,&length) == -1)
{
perror("recvfrom fail");
}
printf("recvfrom client:%s\n",buf);
sendto(sockfd,buf,sizeof(buf),0,(SA *)&addr_c,sizeof(addr_c));
close(sockfd);
}
====================================
client.c:
====================================
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <errno.h>
#define BUFFERSIZE 1024
typedef struct sockaddr SA;
int main(void)
{
int sockfd;
char buf[BUFFERSIZE];
struct sockaddr_in addr_s;
if((sockfd = socket(AF_INET,SOCK_DGRAM,0)) == -1)
{
perror("socket fail");
return -1;
}
memset(&addr_s,0,sizeof(addr_s));
addr_s.sin_family = AF_INET;
addr_s.sin_addr.s_addr = inet_addr("127.0.0.1");
addr_s.sin_port = htons(31180);
memset(buf,'\0',sizeof(buf));
sprintf(buf,"abcde");
if(sendto(sockfd,buf,sizeof(buf)
,0,(SA *)&addr_s,sizeof(addr_s)) < 0)
{
perror("sendto fail");
}
memset(buf,'\0',sizeof(buf));
recvfrom(sockfd,buf,sizeof(buf),0,NULL,NULL);
printf("recvfrom server:%s\n",buf);
close(sockfd);
}
㈡ C语言UDP协议
简单的学习代码,仅供参考:
server.c
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include<netdb.h>
#include<sys/socket.h>
#include<arpa/inet.h>
#defineSERVICE_PORT 8080
#defineBUFSIZE2048
#defineHEAD4
intmain(intargc,char**argv)
{
structsockaddr_inmyaddr; /*ouraddress*/
structsockaddr_inremaddr; /*remoteaddress*/
socklen_taddrlen=sizeof(remaddr); /*lengthofaddresses*/
intrecvlen; /*#bytesreceived*/
intfd; /*oursocket*/
intmsgcnt=0; /*count#ofmessageswereceived*/
unsignedcharbuf[BUFSIZE]="ret_"; /*receivebuffer*/
/*createaUDPsocket*/
if((fd=socket(AF_INET,SOCK_DGRAM,0))<0){
perror("cannotcreatesocket ");
return0;
}
/**/
memset((char*)&myaddr,0,sizeof(myaddr));
myaddr.sin_family=AF_INET;
myaddr.sin_addr.s_addr=htonl(INADDR_ANY);
myaddr.sin_port=htons(SERVICE_PORT);
if(bind(fd,(structsockaddr*)&myaddr,sizeof(myaddr))<0){
perror("bindfailed");
return0;
}
/*nowloop,*/
for(;;)
{
printf("waitingonport%d ",SERVICE_PORT);
recvlen=recvfrom(fd,buf+HEAD,BUFSIZE,0,(structsockaddr*)&remaddr,&addrlen);
if(recvlen>0)
{
buf[HEAD+recvlen]=0;
printf("receivedmessage:"%s"(%dbytes) ",buf+HEAD,recvlen);
}
else
{
printf("uhoh-somethingwentwrong! ");
}
printf("sendingresponse"%s" ",buf);
if(sendto(fd,buf,strlen(buf),0,(structsockaddr*)&remaddr,addrlen)<0)
{
perror("sendto");
}
}
close(fd);
return0;
}
client.c
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include<netdb.h>
#include<sys/socket.h>
#defineSERVICE_PORT 8080
#defineBUFLEN2048
intmain(intargc,char*argv[])
{
structsockaddr_inmyaddr,remaddr;
intfd,i,slen=sizeof(remaddr);
charsendbuf[BUFLEN]="hello"; /*messagebuffer*/
charreadbuf[BUFLEN]={0};
intrecvlen; /*#bytesinacknowledgementmessage*/
char*server="127.0.0.1"; /**/
/*createasocket*/
if((fd=socket(AF_INET,SOCK_DGRAM,0))==-1)
{
printf("socketcreated ");
}
/**/
memset((char*)&myaddr,0,sizeof(myaddr));
myaddr.sin_family=AF_INET;
myaddr.sin_addr.s_addr=htonl(INADDR_ANY);
myaddr.sin_port=htons(0);
if(bind(fd,(structsockaddr*)&myaddr,sizeof(myaddr))<0)
{
perror("bindfailed");
return0;
}
memset((char*)&remaddr,0,sizeof(remaddr));
remaddr.sin_family=AF_INET;
remaddr.sin_port=htons(SERVICE_PORT);
if(inet_aton(server,&remaddr.sin_addr)==0)
{
fprintf(stderr,"inet_aton()failed ");
exit(1);
}
printf("Sendingpacket%dto%sport%d ",i,server,SERVICE_PORT);
if(sendto(fd,sendbuf,strlen(sendbuf),0,(structsockaddr*)&remaddr,slen)==-1)
{
perror("sendto");
exit(1);
}
/**/
recvlen=recvfrom(fd,readbuf,BUFLEN,0,(structsockaddr*)&remaddr,&slen);
if(recvlen>=0)
{
readbuf[recvlen]=0; /*expectaprintablestring-terminateit*/
printf("receivedmessage:"%s" ",readbuf);
}
close(fd);
return0;
}
如果需要整个工程,可以联系我。
㈢ 请教用C语言编的借助UDP协议实现的文件传输的程序
本程序在 Windows 7 Visual Studio 2015 和 Linux Ubuntu 15.04 GCC 5.11 下均编译运行测试通过。
本程序支持 Windows 和 Linux 之间传送文件,如果要在 Windows 和 Linux 之间传送文件,文件名不能出现中文。
本程序支持无线 WiFi,支持 USB 收发器,但仅支持局域网内传送文件,传送文件需要输入对方的 IP 地址。
本程序包括服务器端和客户端,既可以发送文件又可以接收文件。如果要在同一台机器上测试需要同时打开两个程序。
Windows 下查看本机 IP 地址的命令是:
ipconfig
Linux 下查看本机 IP 地址的命令是:
ifconfig
以下是程序代码:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#ifdef_MSC_VER
#include<winsock2.h>
#include<windows.h>
#pragmacomment(lib,"ws2_32.lib")
#else
#include<pthread.h>
#include<unistd.h>
#include<signal.h>
#include<sys/socket.h>
#include<arpa/inet.h>
#endif
//存放发送接收字符数组大小
#defineSIZEA65501
//每次发送接收字节数
#defineSIZEB65500
typedefstructsockaddr_inSockAddrIn;
SockAddrInserverAddr,remoteAddr,clientAddr;
//端口号
intiServerPort,iClientPort;
//新建socket信息
intiUDP;
//字符串转整型
intstrToInt(char*acStr)
{
inti,iIndex=0,iNum=0,iSize=0;
if(acStr[0]=='+'||acStr[0]=='-')
iIndex=1;
for(iSize=iIndex;;iSize++)
if(acStr[iSize]<'0'||acStr[iSize]>'9')
break;
for(i=iIndex;i<iSize;i++)
iNum+=(int)pow(10,iSize-i-1)*(acStr[i]-48);
if(acStr[0]=='-')
iNum=-iNum;
returniNum;
}
//整型转字符串
voidintToStr(intiInt,char*acStr)
{
intiIndex=0,iSize,iNum,iBit,i,j;
if(iInt<0)
{
acStr[0]='-';
iInt=-iInt;
iIndex=1;
}
for(i=0;;i++)
if(iInt<pow(10,i))
break;
iSize=i;
for(i=0;i<iSize;i++)
{
iNum=pow(10,iSize-i-1);
iBit=iInt/iNum;
iInt-=iNum*iBit;
acStr[i+iIndex]=iBit+48;
}
if(iSize!=0)
acStr[iSize+iIndex]='