Linux C语言 32-网络编程之UDP例程

Linux C语言 32-网络编程之UDP例程

本节关键字:C语言 网络编程 UDP协议 套接字操作 服务端 客户端

相关C库函数:setsockopt, socket, bind, recvfrom, sendto, close

相关接口介绍

Linux C语言 30-套接字操作

例程执行任务说明

本例程中服务端的任务:

  • 等待客户端发来消息
  • 读取客户端发来的消息并回复

本例程中客户端的任务:

  • 创建1个客户端
  • 间隔1秒,发送消息告知服务端自己的包编号

UDP协议服务端例程实现

c 复制代码
// 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 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);
}

UDP协议客户端例程实现

c 复制代码
// 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 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);
}

例程编译及运行

UDP服务端例程编译及运行结果
c 复制代码
$ 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客户端例程编译及运行结果
c 复制代码
# 服务端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
相关推荐
ac.char几秒前
在 Ubuntu 系统上安装 npm 环境以及 nvm(Node Version Manager)
linux·ubuntu·npm
肖永威6 分钟前
CentOS环境上离线安装python3及相关包
linux·运维·机器学习·centos
tian2kong9 分钟前
Centos 7 修改YUM镜像源地址为阿里云镜像地址
linux·阿里云·centos
hopetomorrow10 分钟前
学习路之PHP--使用GROUP BY 发生错误 SELECT list is not in GROUP BY clause .......... 解决
开发语言·学习·php
布鲁格若门13 分钟前
CentOS 7 桌面版安装 cuda 12.4
linux·运维·centos·cuda
网络安全-杰克18 分钟前
网络安全概论
网络·web安全·php
小牛itbull20 分钟前
ReactPress vs VuePress vs WordPress
开发语言·javascript·reactpress
C-cat.20 分钟前
Linux|进程程序替换
linux·服务器·microsoft
dessler21 分钟前
云计算&虚拟化-kvm-扩缩容cpu
linux·运维·云计算
怀澈12222 分钟前
高性能服务器模型之Reactor(单线程版本)
linux·服务器·网络·c++