/*****************************************/
UDP-server based
/*****************************************/
1. Create asocketThe function socket()
2. Bind the IP address, port and other information to the socket, with the function bind()
3. Loop to receive data with the function recvfrom()
4. Close the network connection
/*****************************************/
UDP-client based
/*****************************************/
1. Create a socket with the function socket()
2. Bind the IP address, port and other information to the socket, with the function bind()
3. Set the other party's IP address, port, and other attributes.
4. Send data with the function sendto()
5. Close the network connection
UDP-server based
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define SERVER_PORT 8888
#define MAX_MSG_SIZE 1024
void udps_respon(int sockfd)
{
struct sockaddr_in addr;
int addrlen,n;
char msg[MAX_MSG_SIZE];
while(1)
{ /* Read from and write to the network */
bzero(msg,sizeof(msg)); // initialize,clear
addrlen = sizeof(struct sockaddr);
n=recvfrom(sockfd,msg,MAX_MSG_SIZE,0,(struct sockaddr*)&addr,&addrlen); // Receive the message from client
msg[n]=0;
/* Show that the server has received the message */
fprintf(stdout,"Server have received %s",msg); // display message
}
}
int main(void)
{
int sockfd;
struct sockaddr_in addr;
/* Server side starts building socket descriptors */
sockfd=socket(AF_INET,SOCK_DGRAM,0);
if(sockfd<0)
{
fprintf(stderr,"Socket Error:%s\n",strerror(errno));
exit(1);
}
/* Server-side populated sockaddr structure */
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);
/* Bundle sockfd descriptors */
if(bind(sockfd,(struct sockaddr *)&addr,sizeof(struct sockaddr_in))<0)
{
fprintf(stderr,"Bind Error:%s\n",strerror(errno));
exit(1);
}
udps_respon(sockfd); // perform read and write operations
close(sockfd);
}
UDP-client based
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define SERVER_PORT 8888
#define MAX_BUF_SIZE 1024
void udpc_requ(int sockfd,const struct sockaddr_in *addr,int len)
{
char buffer[MAX_BUF_SIZE];
int n;
while(1)
{ /* Read from keyboard, write to server */
printf("Please input char:\n");
fgets(buffer,MAX_BUF_SIZE,stdin);
sendto(sockfd,buffer,strlen(buffer),0,addr,len);
bzero(buffer,MAX_BUF_SIZE);
}
}
int main(int argc,char **argv)
{
int sockfd;
struct sockaddr_in addr;
if(argc!=2)
{
fprintf(stderr,"Usage:%s server_ip\n",argv[0]);
exit(1);
}
/* Create sockfd descriptor */
sockfd=socket(AF_INET,SOCK_DGRAM,0);
if(sockfd<0)
{
fprintf(stderr,"Socket Error:%s\n",strerror(errno));
exit(1);
}
/* Filling in server-side information */
bzero(&addr,sizeof(struct sockaddr_in));
addr.sin_family=AF_INET;
addr.sin_port=htons(SERVER_PORT);
if(inet_aton(argv[1],&addr.sin_addr)<0) /* the inet_aton function is used to convert a string-type IP address into a network binary digit */
{
fprintf(stderr,"Ip error:%s\n",strerror(errno));
exit(1);
}
udpc_requ(sockfd,&addr,sizeof(struct sockaddr_in)); // perform read and write operations
close(sockfd);
}