本节关键字:C语言 网络编程 UDP协议 套接字操作 服务端 客户端
相关C库函数:setsockopt, socket, bind, recvfrom, sendto, close
Linux C语言 30-套接字操作
本例程中服务端的任务:
本例程中客户端的任务:
// udpserver.c
#include
#include
#include
#include
#include
#include
#include
void msleep(int msecs);
int getOneServerSocket(const char *ip, int port);
int main(int argc, char *argv[])
{
int rc, rxn, txn, srvfd, port;
int addrLen;
struct sockaddr_in remoteAddr;
char ip[32] = {0};
char rxBuffer[1024] = {0};
char txBuffer[1024] = {0};
if (argc != 3)
{
printf("please input correct arguments:\n");
printf("\tudpserver ip port\n\n");
return -1;
}
// 解析ip和port
strcpy(ip, argv[1]);
port = atoi(argv[2]);
printf("ip: %s, port: %d\n", ip, port);
// 创建套接字
srvfd = getOneServerSocket(ip, port);
addrLen = sizeof(remoteAddr);
while (1)
{
bzero(rxBuffer, sizeof(rxBuffer));
rxn = recvfrom(srvfd, rxBuffer, sizeof(rxBuffer)-1, 0, (struct sockaddr*)&remoteAddr, &addrLen);
if (rxn > 0)
{
printf("client[%s:%d] socket:%d %s\n", inet_ntoa(remoteAddr.sin_addr), ntohs(remoteAddr.sin_port), srvfd, rxBuffer);
bzero(txBuffer, sizeof(txBuffer));
sprintf(txBuffer, "server reply client[%s:%d] -> %s", inet_ntoa(remoteAddr.sin_addr), ntohs(remoteAddr.sin_port), rxBuffer);
sendto(srvfd, txBuffer, strlen(txBuffer), 0, (struct sockaddr*)&remoteAddr, addrLen);
}
}
close(srvfd);
return 0;
}
void msleep(int msecs)
{
struct timeval stoptime;
stoptime.tv_sec = (int)(msecs/1000);
stoptime.tv_usec = (int)(msecs%1000)*1000;
select(0, 0, 0, 0, &stoptime);
}
int getOneServerSocket(const char *ip, int port)
{
int rc, srvfd, reused, timeout;
struct sockaddr_in addr;
srvfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (srvfd == -1)
{
perror("create socket");
exit(-1);
}
// 设置端口复用
reused = 1;
rc = setsockopt(srvfd, SOL_SOCKET, SO_REUSEADDR, &reused, sizeof(reused));
if (rc == -1)
{
perror("SO_REUSEDADDR");
goto EXIT;
}
// 设置发送超时限制
timeout = 1000; // 1秒
setsockopt(srvfd, SOL_SOCKET, SO_SNDTIMEO, &timeout, sizeof(timeout));
if (rc == -1)
{
perror("SO_SNDTIMEO");
goto EXIT;
}
// 套接字绑定本地的ip和port
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
addr.sin_addr.s_addr = inet_addr(ip);
bzero(addr.sin_zero, sizeof(addr.sin_zero));
rc = bind(srvfd, (struct sockaddr*)&addr, sizeof(addr));
if (rc == -1)
{
perror("bind");
goto EXIT;
}
return srvfd;
EXIT:
close(srvfd);
exit(-1);
}
// udpclient.c
#include
#include
#include
#include
#include
#include
#include
void msleep(int msecs);
int getOneClientSocket(const char *ip, int port, struct sockaddr_in *addr);
int main(int argc, char *argv[])
{
int rc, rxn, txn, clifd, port;
int addrLen;
struct sockaddr_in serverAddr;
struct sockaddr_in remoteAddr;
unsigned int packet_id = 0;
char ip[32] = {0};
char rxBuffer[1024] = {0};
char txBuffer[1024] = {0};
if (argc != 3)
{
printf("please input correct arguments:\n");
printf("\tudpserver ip port\n\n");
return -1;
}
// 解析ip和port
strcpy(ip, argv[1]);
port = atoi(argv[2]);
printf("ip: %s, port: %d\n", ip, port);
// 创建套接字
clifd = getOneClientSocket(ip, port, &serverAddr);
addrLen = sizeof(remoteAddr);
while (1)
{
bzero(txBuffer, sizeof(txBuffer));
sprintf(txBuffer, "Hello, i'm client[%d], packet id: %d", clifd, packet_id++);
sendto(clifd, txBuffer, strlen(txBuffer), 0, (struct sockaddr*)&serverAddr, addrLen);
bzero(rxBuffer, sizeof(rxBuffer));
rxn = recvfrom(clifd, rxBuffer, sizeof(rxBuffer)-1, 0, (struct sockaddr*)&remoteAddr, &addrLen);
if (rxn > 0)
{
printf("server[%s:%d] -> %s\n", inet_ntoa(remoteAddr.sin_addr), ntohs(remoteAddr.sin_port), rxBuffer);
}
msleep(1000);
}
return 0;
}
void msleep(int msecs)
{
struct timeval stoptime;
stoptime.tv_sec = (int)(msecs/1000);
stoptime.tv_usec = (int)(msecs%1000)*1000;
select(0, 0, 0, 0, &stoptime);
}
int getOneClientSocket(const char *ip, int port, struct sockaddr_in *addr)
{
int rc, srvfd, reused, timeout;
srvfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (srvfd == -1)
{
perror("create socket");
exit(-1);
}
// 设置端口复用
reused = 1;
rc = setsockopt(srvfd, SOL_SOCKET, SO_REUSEADDR, &reused, sizeof(reused));
if (rc == -1)
{
perror("SO_REUSEDADDR");
goto EXIT;
}
// 设置发送超时限制
timeout = 1000; // 1秒
setsockopt(srvfd, SOL_SOCKET, SO_SNDTIMEO, &timeout, sizeof(timeout));
if (rc == -1)
{
perror("SO_SNDTIMEO");
goto EXIT;
}
// 套接字绑定本地的ip和port
addr->sin_family = AF_INET;
addr->sin_port = htons(port);
addr->sin_addr.s_addr = inet_addr(ip);
bzero(addr->sin_zero, sizeof(addr->sin_zero));
return srvfd;
EXIT:
close(srvfd);
exit(-1);
}
$ gcc udpserver.c -o udpserver
$ ./udpserver 0.0.0.0 88888
ip: 0.0.0.0, port: 88888
client[192.168.146.128:52147] socket:3 Hello, i'm client[3], packet id: 0
client[192.168.146.128:52147] socket:3 Hello, i'm client[3], packet id: 1
client[192.168.146.128:52147] socket:3 Hello, i'm client[3], packet id: 2
client[192.168.146.128:52147] socket:3 Hello, i'm client[3], packet id: 3
client[192.168.146.128:52147] socket:3 Hello, i'm client[3], packet id: 4
client[192.168.146.128:52147] socket:3 Hello, i'm client[3], packet id: 5
client[192.168.146.128:52147] socket:3 Hello, i'm client[3], packet id: 6
client[192.168.146.128:52147] socket:3 Hello, i'm client[3], packet id: 7
client[192.168.146.128:52147] socket:3 Hello, i'm client[3], packet id: 8
client[192.168.146.128:52147] socket:3 Hello, i'm client[3], packet id: 9
client[192.168.146.128:52147] socket:3 Hello, i'm client[3], packet id: 10
client[192.168.146.128:52147] socket:3 Hello, i'm client[3], packet id: 11
client[192.168.146.128:52147] socket:3 Hello, i'm client[3], packet id: 12
client[192.168.146.128:52147] socket:3 Hello, i'm client[3], packet id: 13
client[192.168.146.128:52147] socket:3 Hello, i'm client[3], packet id: 14
# 服务端IP为192.168.146.128
$ gcc udpclient.c -o udpclient
$ ./udpclient 192.168.146.128 88888
ip: 192.168.146.128, port: 88888
server[192.168.146.128:23352] -> server reply client[192.168.146.128:52147] -> Hello, i'm client[3], packet id: 0
server[192.168.146.128:23352] -> server reply client[192.168.146.128:52147] -> Hello, i'm client[3], packet id: 1
server[192.168.146.128:23352] -> server reply client[192.168.146.128:52147] -> Hello, i'm client[3], packet id: 2
server[192.168.146.128:23352] -> server reply client[192.168.146.128:52147] -> Hello, i'm client[3], packet id: 3
server[192.168.146.128:23352] -> server reply client[192.168.146.128:52147] -> Hello, i'm client[3], packet id: 4
server[192.168.146.128:23352] -> server reply client[192.168.146.128:52147] -> Hello, i'm client[3], packet id: 5
server[192.168.146.128:23352] -> server reply client[192.168.146.128:52147] -> Hello, i'm client[3], packet id: 6
server[192.168.146.128:23352] -> server reply client[192.168.146.128:52147] -> Hello, i'm client[3], packet id: 7
server[192.168.146.128:23352] -> server reply client[192.168.146.128:52147] -> Hello, i'm client[3], packet id: 8
server[192.168.146.128:23352] -> server reply client[192.168.146.128:52147] -> Hello, i'm client[3], packet id: 9
server[192.168.146.128:23352] -> server reply client[192.168.146.128:52147] -> Hello, i'm client[3], packet id: 10
server[192.168.146.128:23352] -> server reply client[192.168.146.128:52147] -> Hello, i'm client[3], packet id: 11
server[192.168.146.128:23352] -> server reply client[192.168.146.128:52147] -> Hello, i'm client[3], packet id: 12
server[192.168.146.128:23352] -> server reply client[192.168.146.128:52147] -> Hello, i'm client[3], packet id: 13
server[192.168.146.128:23352] -> server reply client[192.168.146.128:52147] -> Hello, i'm client[3], packet id: 14