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]='