本文转载至:http://www.programminglogic.com/sockets-programming-in-c-using-udp-datagrams/
以下是一段简单的server-client C程序,它使用UDP socket来进行传输。基本上客户端往服务器发一个消息,服务器把消息转成大写再返回给客户端。
如果你想看一段更简单的代码,先看一下这个client-server 程序,它仅仅发一个“Hello World”消息。对应的函数和结构体在那边说明的更清楚。其次,你也可以看一个类似的使用TCP的示例(服务端把消息转化成大写)
Server 端代码
/************* UDP SERVER CODE *******************/
#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
#include <stdlib.h>
int main(){
int udpSocket, nBytes;
char buffer[1024];
struct sockaddr_in serverAddr, clientAddr;
struct sockaddr_storage serverStorage;
socklen_t addr_size, client_addr_size;
int i;
/*Create UDP socket*/
udpSocket = socket(PF_INET, SOCK_DGRAM, 0);
/*Configure settings in address struct*/
serverAddr.sin_family = AF_INET;
serverAddr.sin_port = htons(7891);
serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
memset(serverAddr.sin_zero, '\0', sizeof serverAddr.sin_zero);
/*Bind socket with address struct*/
bind(udpSocket, (struct sockaddr *) &serverAddr, sizeof(serverAddr));
/*Initialize size variable to be used later on*/
addr_size = sizeof serverStorage;
while(1){
/* Try to receive any incoming UDP datagram. Address and port of
requesting client will be stored on serverStorage variable */
nBytes = recvfrom(udpSocket,buffer,1024,0,(struct sockaddr *)&serverStorage, &addr_size);
/*Convert message received to uppercase*/
for(i=0;i<nBytes-1;i++)
buffer[i] = toupper(buffer[i]);
/*Send uppercase message back to client, using serverStorage as the address*/
sendto(udpSocket,buffer,nBytes,0,(struct sockaddr *)&serverStorage,addr_size);
}
return 0;
}
client 端代码
/************* UDP CLIENT CODE *******************/
#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
int main(){
int clientSocket, portNum, nBytes;
char buffer[1024];
struct sockaddr_in serverAddr;
socklen_t addr_size;
/*Create UDP socket*/
clientSocket = socket(PF_INET, SOCK_DGRAM, 0);
/*Configure settings in address struct*/
serverAddr.sin_family = AF_INET;
serverAddr.sin_port = htons(7891);
serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
memset(serverAddr.sin_zero, '\0', sizeof serverAddr.sin_zero);
/*Initialize size variable to be used later on*/
addr_size = sizeof serverAddr;
while(1){
printf("Type a sentence to send to server:\n");
fgets(buffer,1024,stdin);
printf("You typed: %s",buffer);
nBytes = strlen(buffer) + 1;
/*Send message to server*/
sendto(clientSocket,buffer,nBytes,0,(struct sockaddr *)&serverAddr,addr_size);
/*Receive message from server*/
nBytes = recvfrom(clientSocket,buffer,1024,0,NULL, NULL);
printf("Received from server: %s\n",buffer);
}
return 0;
}