一个linux UDP网络通讯的例子源代码(server、client方式)

c语言UDP传输系统源码(附完整源码) 阅读详情
服务器端代码
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
int main(int argc, char *argv[])
{
int sock;
//sendto中使用的对方地址
struct sockaddr_in toAddr;
//在recvfrom中使用的对方主机地址
struct sockaddr_in fromAddr;
int recvLen;
unsigned int addrLen;
char recvBuffer[128];
sock = socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP);
if(sock < 0)
{
 printf("创建套接字失败了./r/n");
 exit(0);
}
memset(&fromAddr,0,sizeof(fromAddr));
fromAddr.sin_family=AF_INET;
fromAddr.sin_addr.s_addr=htonl(INADDR_ANY);
fromAddr.sin_port = htons(4000);
if(bind(sock,(struct sockaddr*)&fromAddr,sizeof(fromAddr))<0)
{
 printf("bind() 函数使用失败了./r/n");
 close(sock);
 exit(1);
}
while(1){
addrLen = sizeof(toAddr);
if((recvLen = recvfrom(sock,recvBuffer,128,0,(struct sockaddr*)&toAddr,&addrLen))<0)
{
 printf("()recvfrom()函数使用失败了./r/n");
 close(sock);
 exit(1);
}
if(sendto(sock,recvBuffer,recvLen,0,(struct sockaddr*)&toAddr,sizeof(toAddr))!=recvLen){
printf("sendto fail/r/n");
close(sock);
exit(0);
}
return 0;
}
}

客户端代码
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
int main(int argc, char *argv[])
{
if(argc < 2)
{
 printf("请输入要传送的内容./r/n");
 exit(0);
}
int sock;
//sendto中使用的对方地址
struct sockaddr_in toAddr;
//在recvfrom中使用的对方主机地址
struct sockaddr_in fromAddr;
unsigned int fromLen;
char recvBuffer[128];
sock = socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP);
if(sock < 0)
{
 printf("创建套接字失败了./r/n");
 exit(1);
}
memset(&toAddr,0,sizeof(toAddr));
toAddr.sin_family=AF_INET;
toAddr.sin_addr.s_addr=inet_addr("127.0.0.1");
toAddr.sin_port = htons(4000);
if(sendto(sock,argv[1],strlen(argv[1]),0,(struct sockaddr*)&toAddr,sizeof(toAddr)) != strlen(argv[1]))
{
 printf("sendto() 函数使用失败了./r/n");
 close(sock);
 exit(1);
}
fromLen = sizeof(fromAddr);
if(recvfrom(sock,recvBuffer,128,0,(struct sockaddr*)&fromAddr,&fromLen)<0)
{
 printf("()recvfrom()函数使用失败了./r/n");
 close(sock);
 exit(1);
}
printf("recvfrom() result:%s/r/n",recvBuffer);
close(sock);
}
linuxudp单播组播广播实现归纳.pdf 立即下载

相关推荐

Applied Intelligence投稿经验分享(正在投稿中)

这几天在投稿Applied Intelligence这个期刊,过程很曲折,反反复复,因此写一篇经验分享,希望能记录一下自己的过程,也能给其他人一些帮助(可以在评论区讨论),提醒一下,一定要写一些声明,不会写、不知道写啥就看第四章的第二个问题。

weixin_55988068的博客 1万+

UDP通讯应用于多种环境的Demo

udp 单播 visual studio 2013 udp 单播 纯C 环境

weixin_44759598的博客 621

免费搞定DWG转DXF:ODA File Converter保姆级配置指南(附常见报错解决)

本文详细介绍了如何使用ODA File Converter免费工具实现DWG到DXF的格式转换,包括环境配置、命令行参数解析、编程集成方案及常见问题解决。特别针对网络访问异常、参数配置等实际痛点提供应对策略,帮助用户高效完成工程设计文件的格式转换。

weixin_29291185的博客 6037

linux udp 程序代码,Window/Linux通用的一个简单UDPSocket示例

源代码是在网上找到的。自己做了一点修改。可以用作Broadcast UDP Socket。编译链接完成后的可执行程序名例为:testudp使用方法:testudp 90.0.1.33//(与90.0.1.33链接通信)testudp 90.0.1.255 //(向90.0.1. 网段的所有机器广播数据)注意:程序中的变量‘opt’需为‘int’型,不能是‘bool’型,否则在Linux下无法做B...

weixin_30847063的博客 247

我用WorkBuddy“克隆“了一个我,从此每句话像我自己说的

各位,做这件事花了多长时间?准备样本可能要半小时,提炼风格十分钟,保存固化再花十分钟。加起来不到一个小时。但这一个小时之后呢?你就拥有了一个"永远在线的文字替身"。以后写口播稿,打开WorkBuddy跟它说;写课程逐字稿,跟它说;写公众号文章,也跟它说。写出来的东西,你的学员一看——“嘿,这不就是翟猛老师说的嘛。说到这儿,可能有人会问:老师,我的风格有那么重要吗?AI写的不都挺好的吗?各位你想想看,你为什么愿意听某个老师的课?不只是因为他讲的内容好,更是因为你喜欢他讲话的方式。

weixin_39284145的博客 822

李航《统计学习方法》课后习题答案(第2版)

李航《统计学习方法》课后习题答案(第2版) 章节 链接 第1章 统计学习及监督学习概论 点击进入 第2章 感知机 点击进入 第3章 k近邻法 点击进入 第4章 朴素贝叶斯法 点击进入 第5章 决策树 点击进入 第6章 逻辑斯谛回归与最大熵 点击进入 第7章 支持向量机 点击进入 第8章 提升方法 点击进入 第9章 EM算法及其推广 点击进入 第10章 隐马尔可夫模型 点击进入 第11章 条件随机场 点击进入     也可进入我的个人主页查看分类专栏

weixin_43646592的博客 2万+

UDP协议源码解析之发送

UDP不是面向连接的协议,发送数据的时候指定目的地址的信息即可以发送。下面是入口函数的代码。 static int sock_sendto(int fd, void * buff, int len, unsigned flags, struct sockaddr *addr, int addr_len) { struct socket *sock; struct file *file;...

标子 的专栏 790

Open3D 计算FPFH特征【2025最新版】

FPFH特征描述子的python实现,本文最新更新时间为:2025年3月18日。

点云侠的博客 5278

linux udp通讯实例,一个linux UDP网络通讯的例子源代码(server、client方式)

服务器端代码#include #include #include #include #include #include #include #include int main(int argc, char *argv[]){int sock;//sendto中使用的对方地址struct sockaddr_in toAddr;//在recvfrom中使用的对方主机地址struct sockaddr_i...

weixin_42566121的博客 228

SPI为什么不需要加上拉电阻

大家好,我是记得诚。I2C因为其开漏的结构,无法输出高电平,所以外部加上拉电阻。我们在使用SPI的时候,发现是不需要加上拉电阻的。主要因为SPI多为推挽的结构,推挽电路,高低电平都可以输出,所以外部不需要加上拉电阻。推挽电路可以用2个三极管或者2个MOS管搭建而成。

记得诚 2629

基于 transformer、lstm、LSTM多特征的交通流量预测 完整数据代码 超全项目 本科硕士毕设卓卓有余

基于 transformer、lstm、LSTM多特征的交通流量预测 完整数据代码 超全项目 本科硕士毕设卓卓有余

4656

linux添加udp,一个linux UDP网络通讯的例子源代码(server、client方式)

服务器端代码#include #include #include #include #include #include #include #include int main(int argc, char *argv[]){int sock;//sendto中使用的对方地址struct sockaddr_in toAddr;//在recvfrom中使用的对方主机地址struct sockaddr_i...

weixin_39849762的博客 181

基于YOLOv26的智能农业无人机巡检系统

本文提出了一种基于YOLOv26的智能农业无人机巡检系统。系统采用模块化设计,包含数据采集、预处理、目标检测、图像拼接、分析决策和部署控制六大核心模块。无人机搭载多光谱相机、RGB相机和热红外相机进行农田数据采集,通过YOLOv26实现作物病虫害、杂草及灌溉设备的检测。系统采用PyTorch、OpenCV等技术栈,支持无人机自动路径规划和多传感器数据采集,并包含数据增强和预处理功能。该系统可有效提升农业巡检效率,为精准农业提供智能化解决方案。

一键难忘的博客 808

一个linux UDP网络通讯的例子源代码(server、client方式

服务器端代码#include #include #include #include #include #include #include #include int main(int argc, char *argv[]){int sock;//sendto中使用的对方地址struct sockaddr_in toAddr;//在recvfro

xiaoyangger的专栏 782

精雕软件(JDpaint) 5.5 绿色版

精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

C/C++之网络编程

人与人沟通的方式有很多:书信、电话、qq、等。两个人如果想沟通,必须选择一种沟通方式,一方用电话,另一方也应该用电话,而不是书信。人与人聊天可以说反话,说套话,人脑是智能的所以可以理解,但是机器不是,机器是死板的,所以协议是网络通讯的规则,是约定。

m0_74078963的博客 1472

Probability and Statistics - 4th edition

Probability and Statistics - 4th edition

作业11-网络

参考资料 本次作业参考文件-包含实验任务书正则表达式参考资料 1. 本周学习总结 以你喜欢的方式(思维导图、OneNote或其他)归纳总结多网络相关内容。 2. 为你的系统增加网络功能(购物车、图书馆管理、斗地主等)-分组完成 为了让你的系统可以被多个用户通过网络同时使用,需要为你的系统增加网络功能。 2.1 简述你想为你的系统增加什么网络功能?设计思路是什么? 2.2 系统通过网络传递了些...

zhrb 285

efm32 入门.pdf

本PDF共计22页,介绍的是操作驱动API,下载与否自己斟酌。

RemObjects SDK

 随着各家厂商的强力背书与推销,Web Services 俨然成为未来分布式系统开发的主流架构,但是Web Services 至今仍然存在一些问题,其中有些是属于规格的问题,有些则是先天上的限制,许多使用Web Services 开发系统的人都会有一个困扰,那就是效率不高,其原因很简单,XML 本身属于纯文字型态,加上必须依赖XML Parser 剖析XML 文件,在传输与解译上都是造成效率不彰的

挨踢生涯 868

Ubuntu 环境下编译udp单播 Demo代码实例

Ubuntu环境下udp协议单播 Demo 实例

weixin_44759598的博客 533

UDP 实现宿主机(windows)和ubuntu 互相通信

假设 A 和B两台电脑之间相互通信,如A想发一段话给电脑B ,A电脑必须知道B电脑的ip地址,ip地址是电脑的唯一标识符,假设A电脑知道了B电脑的ip ,但是A电脑知道给B电脑那个程序(进程)发 嘛?或者B得拿什么东西来接收,这就是端口的作用,端口就是表示主机中唯一进程的标识符,假设 A和B 电脑上各有一个wechat 正在运行,A电脑上的微信给B电脑上的微信发了一条消息,除了要知道B电脑的ip外,是不是也要知道B电脑上微信的唯一标识符(端口),要不然谁知道你要发给谁.

weixin_59131972的博客 1976

udp服务器php代码例子,libevent 做UDP 服务器的例子源码

UDP server over libevent – sample code#include #include #include #include #include #include #include #include #define SVR_IP "127.0.0.1"#define SVR_PORT ...

weixin_33887804的博客 446

ubuntu C/C++UDP客户端的一个简单实现

#include #include #include #include #include #include #include <stdio.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h&g...

weixin_42096202的博客 4125
上一篇: 编写Linux下的UDP Client/Server程序
下一篇: 资料整理--socket之select函数
ryanzz
博客等级 码龄19年 7粉丝 2原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值