Socket相关函数(2)- sendto(), recvfrom() UDP模型

udp_server.c

复制代码
#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <arpa/inet.h>
#include <ctype.h>
#include <string.h>

#define SERV_PORT 49130
#define MAXLINE 80


int main()
{
    int sockfd;
    struct sockaddr_in servaddr, cliaddr;    
    char buf[MAXLINE];
    char str[INET_ADDRSTRLEN];
    int cliaddr_len;
    int n, i;    

    if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
        perror("socket");
        exit(1);        
    }

    bzero(&servaddr, sizeof(servaddr));
    servaddr.sin_family = AF_INET;
    servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
    servaddr.sin_port = htons(SERV_PORT); // 一定要使用htons函数将端口转换为大端格式
    
    if (bind(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0) {
        perror("bind");
        exit(1);
    }

    printf("Accepting connections...\n");

    while (1) {
        cliaddr_len = sizeof(cliaddr);
        n = recvfrom(sockfd, buf, MAXLINE, 0, (struct sockaddr *)&cliaddr, &cliaddr_len);
        if (n == -1) {
            perror("recvfrom err");
        }
        // print source address's ip,port
        printf("Received from %s at PORT %d, recvlen is: %d\n", inet_ntop(AF_INET, &cliaddr.sin_addr, str, sizeof(str)), ntohs(cliaddr.sin_port), n);

        for (i = 0; i < n; i++)
            buf[i] = toupper(buf[i]);    
        n = sendto(sockfd, buf, n, 0, (struct sockaddr *)&cliaddr, sizeof(cliaddr));
        // printf("send length is: %d\n", n);
        if (n == -1) {
            perror("sendto err");
        }
    }

    close(sockfd);
    return 0;
}
复制代码

udp_client.c

复制代码
#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <arpa/inet.h>
#include <unistd.h>


#define MAXLINE 80
#define SERV_PORT 49130

static const char *SERV_ADDR = "127.0.0.1";

int main()
{
    
    int sockfd;
    char buf[MAXLINE];
    struct sockaddr_in servaddr;
    int n, i;    

    bzero(&servaddr, sizeof(servaddr));
    servaddr.sin_family = AF_INET;
    inet_pton(AF_INET, SERV_ADDR, &servaddr.sin_addr);    
    servaddr.sin_port = htons(SERV_PORT);    

    if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
        perror("socket");
        exit(1);
    }

    while (fgets(buf, MAXLINE, stdin) != NULL) {
        n = sendto(sockfd, buf, strlen(buf), 0, (struct sockaddr *)&servaddr, sizeof(servaddr));
        if (n == -1) {
            perror("sendto err");
        }
        
        n = recvfrom(sockfd, buf, MAXLINE, 0, NULL, 0);
        if (n == -1) {
            perror("recvfrom err");
        }
        
        //printf("recv len is: %d\n", n);
        write(STDOUT_FILENO, buf, n);
    }

    close(sockfd);
    return 0;
}
复制代码

先运行Server, 再运行Client,在client输入:

yongdaimi@ubuntu:~/Documents/code$ gcc udp_client.c -o client
yongdaimi@ubuntu:~/Documents/code$ ./client
java
JAVA
j2sdk
J2SDK
jdbc
JDBC
cpp
CPP
jni
JNI

Server:

Accepting connections...
Received from 127.0.0.1 at PORT 45162, recvlen is: 3
Received from 127.0.0.1 at PORT 51366, recvlen is: 3
Received from 127.0.0.1 at PORT 51366, recvlen is: 5
Received from 127.0.0.1 at PORT 51366, recvlen is: 5
Received from 127.0.0.1 at PORT 57741, recvlen is: 5
Received from 127.0.0.1 at PORT 51904, recvlen is: 5
Received from 127.0.0.1 at PORT 51904, recvlen is: 6
Received from 127.0.0.1 at PORT 51904, recvlen is: 5
Received from 127.0.0.1 at PORT 51904, recvlen is: 4
Received from 127.0.0.1 at PORT 51904, recvlen is: 4

 



如果您觉得阅读本文对您有帮助,请点一下“推荐”按钮,您的“推荐”将是我最大的写作动力!欢迎各位转载,但是未经作者本人同意,转载文章之后必须在文章页面明显位置给出作者和原文连接,否则保留追究法律责任的权利。
posted @   夜行过客  阅读(1966)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
阅读排行:
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
点击右上角即可分享
微信分享提示