Linux C语言 32-网络编程之UDP例程
本节关键字:C语言 网络编程 UDP协议 套接字操作 服务端 客户端
相关C库函数:setsockopt, socket, bind, recvfrom, sendto, close
相关接口介绍
例程执行任务说明
UDP协议服务端例程实现
// udpserver.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
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 portnn");
return -1;
}
// 解析ip和port
strcpy(ip, argv[1]);
port = atoi(argv[2]);
printf("ip: %s, port: %dn", 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 %sn", 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);
}
UDP协议客户端例程实现
// udpclient.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
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 portnn");
return -1;
}
// 解析ip和port
strcpy(ip, argv[1]);
port = atoi(argv[2]);
printf("ip: %s, port: %dn", 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] -> %sn", 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);
}
例程编译及运行
UDP服务端例程编译及运行结果
$ 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
UDP客户端例程编译及运行结果
# 服务端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
原文地址:https://blog.csdn.net/qq_45157350/article/details/134659743
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若转载,请注明出处:http://www.7code.cn/show_26888.html
如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱:suwngjj01@126.com进行投诉反馈,一经查实,立即删除!
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。