UDP通信、本地套接字

news/2024/5/18 13:17:05 标签: udp, 通信
#include <sys/types.h>
#include <sys/socket >
ssize_t sendto(int sockfd, const void *buf, size_t len, int flags,const struct sockaddr *dest_addr, socklen_t addrlen);
	- 参数:
		- sockfd : 通信的fd
		- buf : 要发送的数据
		- len : 发送数据的长度
		- flags : 0
		- dest_addr : 通信的另外一端的地址信息
		- addrlen : 地址的内存大小
	
ssize_t recvfrom(int sockfd, void *buf, size_t len, int flags,struct sockaddr *src_addr, socklen_t *addrlen);
	- 参数:
		- sockfd : 通信的fd
		- buf : 接收数据的数组
		- len : 数组的大小

		- flags : 0
		- src_addr : 用来保存另外一端的地址信息,不需要可以指定为NULL
		- addrlen : 地址的内存大小

udp_server.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>

int main() {
    // 1.创建一个通信的socket
    int fd = socket(PF_INET,SOCK_DGRAM,0);
    if(fd == -1) {
        perror("socket");
        exit(-1);
    }
    // 2.绑定
    struct sockaddr_in saddr;
    saddr.sin_family = AF_INET;
    saddr.sin_port = htons(9999);
    saddr.sin_addr.s_addr = INADDR_ANY;
    int ret = bind(fd,(struct sockaddr*)&saddr,sizeof(saddr));
    if(ret == -1) {
        perror("bind");
        exit(-1);
    }
    // 3.通信 
    while (1) {
        char recvbuf[128] = {0};
        char ipbuf[16] = {0};
        struct sockaddr_in caddr;
        int len = sizeof(caddr);
        // 接收数据
        int num = recvfrom(fd,recvbuf,sizeof(recvbuf),0,(struct sockaddr*)&caddr,&len);
        if(num == -1) {
            perror("recvfrom");
            exit(-1);
        }
        inet_ntop(AF_INET,(struct sockaddr*)&caddr.sin_addr.s_addr,ipbuf,sizeof(ipbuf));
        printf("Client IP : %s,Port : %d\n",ipbuf,ntohs(caddr.sin_port));

        printf("client say : %s\n",recvbuf);

        // 发送数据
        sendto(fd,recvbuf,strlen(recvbuf) + 1,0,(struct sockaddr*)&caddr,sizeof(caddr));
    }
    close(fd);
    return 0;
}

udp_client.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>

int main() {
    // 1.创建一个通信的socket
    int fd = socket(PF_INET,SOCK_DGRAM,0);
    if(fd == -1) {
        perror("socket");
        exit(-1);
    }

    // 服务器的地址信息
    struct sockaddr_in saddr;
    saddr.sin_family = AF_INET;
    saddr.sin_port = htons(9999);
    inet_pton(AF_INET,"127.0.0.1",&saddr.sin_addr.s_addr);

    int num = 0;
    // 3.通信 
    while (1) {
        // 发送数据
        char sendBuf[128] = {0};
        sprintf(sendBuf,"hello,i am client %d \n",num++);
        sendto(fd,sendBuf,strlen(sendBuf) + 1,0,(struct sockaddr*)&saddr,sizeof(saddr));

        // 接收数据
        int num = recvfrom(fd,sendBuf,sizeof(sendBuf),0,NULL,NULL);
        printf("server say : %s\n",sendBuf);

        sleep(1);
    }
    close(fd);
    return 0;
}
heheda@heheda:~/Linux/lesson37$ gcc udp_server.c -o server
heheda@heheda:~/Linux/lesson37$ ./server
Client IP : 127.0.0.1,Port : 34127
client say : hello,i am client 0 

Client IP : 127.0.0.1,Port : 34127
client say : hello,i am client 1 

Client IP : 127.0.0.1,Port : 34127
client say : hello,i am client 2 

Client IP : 127.0.0.1,Port : 34127
client say : hello,i am client 3 

Client IP : 127.0.0.1,Port : 34127
client say : hello,i am client 4 

Client IP : 127.0.0.1,Port : 53230
client say : hello,i am client 0 

Client IP : 127.0.0.1,Port : 34127
client say : hello,i am client 5 

Client IP : 127.0.0.1,Port : 53230
client say : hello,i am client 1 

Client IP : 127.0.0.1,Port : 34127
client say : hello,i am client 6 

Client IP : 127.0.0.1,Port : 53230
client say : hello,i am client 2 

Client IP : 127.0.0.1,Port : 34127
client say : hello,i am client 7 

Client IP : 127.0.0.1,Port : 53230
client say : hello,i am client 3 

Client IP : 127.0.0.1,Port : 34127
client say : hello,i am client 8 

Client IP : 127.0.0.1,Port : 53230
client say : hello,i am client 4 

Client IP : 127.0.0.1,Port : 34127
client say : hello,i am client 9 

Client IP : 127.0.0.1,Port : 53230
client say : hello,i am client 5 

Client IP : 127.0.0.1,Port : 34127
client say : hello,i am client 10 

Client IP : 127.0.0.1,Port : 53230
client say : hello,i am client 6 

Client IP : 127.0.0.1,Port : 34127
client say : hello,i am client 11 

Client IP : 127.0.0.1,Port : 53230
client say : hello,i am client 7 

Client IP : 127.0.0.1,Port : 34127
client say : hello,i am client 12 

Client IP : 127.0.0.1,Port : 53230
client say : hello,i am client 8 

Client IP : 127.0.0.1,Port : 34127
client say : hello,i am client 13 

Client IP : 127.0.0.1,Port : 53230
client say : hello,i am client 9 

Client IP : 127.0.0.1,Port : 34127
client say : hello,i am client 14 

Client IP : 127.0.0.1,Port : 53230
client say : hello,i am client 10 

Client IP : 127.0.0.1,Port : 34127
client say : hello,i am client 15 

Client IP : 127.0.0.1,Port : 53230
client say : hello,i am client 11 


http://www.niftyadmin.cn/n/4984405.html

相关文章

【Python数据分析】数据分析之numpy基础

实验环境&#xff1a;建立在Python3的基础之上 numpy提供了一种数据类型&#xff0c;提供了数据分析的运算基础&#xff0c;安装方式 pip install numpy导入numpy到python项目 import numpy as np本文以案例的方式展示numpy的基本语法&#xff0c;没有介绍语法的细枝末节&am…

Spring Boot 中 Nacos 配置中心使用实战

官方参考文档 https://nacos.io/zh-cn/docs/quick-start-spring-boot.html 本人实践 1、新建一个spring boot项目 我的spirngboot版本为2.5.6 2、添加一下依赖 <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-…

Linux操作系统

Linux操作系统 一、 Linux系统的连接1、使用ssh远程连接协议进行连接 二、Linux的常用命令1、查看当前工作目录2、切换工作目录的命令3、查看当前工作目录下有哪些文件或者文件夹4、查看各种命令的使用说明5、创建文件6、创建文件夹7、删除文件或文件夹8、移动文件/文件夹9、复…

Centos7换源

最近需要用到Linux系统进行开发&#xff0c;有些包在国内安装不了&#xff0c;需要换源才可以。简单记录一下换源步骤&#xff1a; 备份系统自带yum源配置文件&#xff0c;该文件位于/etc/yum.repos.d/CentOS-Base.repo mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos…

git 的常用命令

git是一个版本管理器&#xff0c;是程序员必备工具之一&#xff0c;其主分为三个区&#xff1a; 工作区&#xff1a; 暂存区&#xff1a; 仓库&#xff1a; 通过保持软件版本&#xff0c;分支&#xff0c;合并&#xff0c;等多种版本操作&#xff0c;使软件能在自己想要的版本…

用变压器实现德-英语言翻译【01/8】:嵌入层

一、说明 本文是“用变压器实现德-英语言翻译”系列的第一篇文章。它引入了小规模的嵌入来建立感知系统。接下来是嵌入层的变压器使用。下面简要概述了每种方法&#xff0c;然后是德语到英语的翻译。 二、技术背景 嵌入层的目标是使模型能够详细了解单词、标记或其他输入之间的…

04、添加 com.fasterxml.jackson.dataformat -- jackson-dataformat-xml 依赖报错

Correct the classpath of your application so that it contains a single, compatible version of com.fasterxml.jackson.dataformat.xml.XmlMapper 解决&#xff1a; 改用其他版本&#xff0c;我没写版本号&#xff0c;springboot自己默认的是 2.11.4 版本 成功启动项目…

【Axure视频教程】表格编号函数

今天教大家在Axure里如何使用表格编号函数&#xff0c;包括表格编号函数的基本原理、在需要翻页的中继器表格里如何正确使用该函数、函数作为条件的应用&#xff0c;包括让指定第几行的元件默认变色效果以及更新对应第几行内容的效果。该教程主要讲解表格编号函数&#xff0c;不…