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
相关推荐
疯狂飙车的蜗牛24 分钟前
从零玩转CanMV-K230(4)-小核Linux驱动开发参考
linux·运维·驱动开发
2401_857439691 小时前
SSM 架构下 Vue 电脑测评系统:为电脑性能评估赋能
开发语言·php
SoraLuna1 小时前
「Mac畅玩鸿蒙与硬件47」UI互动应用篇24 - 虚拟音乐控制台
开发语言·macos·ui·华为·harmonyos
xlsw_1 小时前
java全栈day20--Web后端实战(Mybatis基础2)
java·开发语言·mybatis
Dream_Snowar2 小时前
速通Python 第三节
开发语言·python
远游客07133 小时前
centos stream 8下载安装遇到的坑
linux·服务器·centos
马甲是掉不了一点的<.<3 小时前
本地电脑使用命令行上传文件至远程服务器
linux·scp·cmd·远程文件上传
jingyu飞鸟3 小时前
centos-stream9系统安装docker
linux·docker·centos
XH华3 小时前
初识C语言之二维数组(下)
c语言·算法
超爱吃士力架3 小时前
邀请逻辑
java·linux·后端