如何在C [LINUX]中使用UDP套接字 [英] How do I use UDP sockets in C [LINUX]

查看:102
本文介绍了如何在C [LINUX]中使用UDP套接字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将 UDP 数据包发送到ip或域,我不想接收eny数据包。就这么简单,只需回答如下:



1:制作套接字。

2:设置为发送数据包到ip。

3:发送数据包。



这就是全部。 :)



我尝试过的事情:



i刚试过googleing它和我刚刚发现给我错误的教程,可能是过时的教程。

解决方案

我编译了相同的示例代码(我想 [ ^ ]),在我的Lubuntu 15.10 Box上。



得到以下输出:



(构建过程)

 gcc -g -c -o udp-send.o udp-send.c 
udp-send.c:在函数'main'中:
udp-send.c:54:6:警告:隐式声明函数'inet_aton'[-Wimplicit-function-declaration]
if(inet_aton(server,& remaddr.sin_addr)== 0){
^
udp-send.c:75 :2:警告:隐式声明函数'close'[-Wimplicit-function-declaration]
关闭(FD);
^
gcc -o udp-send udp-send.o
gcc -g -c -o udp-recv.o udp-recv.c
gcc -o udp- recv udp-recv.o





(服务器执行)

〜 / devel / junk / demo-udp-04 


./ udp-recv
等待端口21234
收到消息:这是数据包0(16字节)
发送响应ack 0
等待端口21234
收到消息:这是数据包1(16字节)
发送响应ack 1
等待端口21234
收到消息:这是数据包2(16字节)
发送响应ack 2
等待端口21234
收到消息:这是数据包3(16字节)
发送响应ack 3
等待端口21234
收到消息:这是数据包4(16字节)
发送响应ack 4
等待港口21234





(客户执行)

发送数据包0到127.0.0.1端口21234 
收到消息:ack 0
发送数据包1到127.0.0.1端口21234
收到消息:ack 1
发送数据包2到127.0.0.1端口21234
收到消息:ack 2
发送数据包3到127.0.0.1端口21234
收到消息:ack 3
发送数据包4到127.0.0.1端口21234
收到消息:ack 4


I want to send a UDP packet to a ip or domain and i don't want to receive eny packets. Just that simple, Just answer how to:

1: Make the socket.
2: Make it so that the packet will be sent to ip.
3: Send the packet.

That's all. :)

What I have tried:

i have just tried googleing it and i just found tutorials that gave me errors, probably out dated tutorials.

解决方案

I compiled the same sample code (I suppose[^]), on my Lubuntu 15.10 Box.

Got the following output:

(build process)

gcc -g    -c -o udp-send.o udp-send.c
udp-send.c: In function ‘main’:
udp-send.c:54:6: warning: implicit declaration of function ‘inet_aton’ [-Wimplicit-function-declaration]
  if (inet_aton(server, &remaddr.sin_addr)==0) {
      ^
udp-send.c:75:2: warning: implicit declaration of function ‘close’ [-Wimplicit-function-declaration]
  close(fd);
  ^
gcc -o udp-send udp-send.o 
gcc -g    -c -o udp-recv.o udp-recv.c
gcc -o udp-recv udp-recv.o



(server execution)

~/devel/junk/demo-udp-04


./udp-recv waiting on port 21234 received message: "This is packet 0" (16 bytes) sending response "ack 0" waiting on port 21234 received message: "This is packet 1" (16 bytes) sending response "ack 1" waiting on port 21234 received message: "This is packet 2" (16 bytes) sending response "ack 2" waiting on port 21234 received message: "This is packet 3" (16 bytes) sending response "ack 3" waiting on port 21234 received message: "This is packet 4" (16 bytes) sending response "ack 4" waiting on port 21234



(client execution)

Sending packet 0 to 127.0.0.1 port 21234
received message: "ack 0"
Sending packet 1 to 127.0.0.1 port 21234
received message: "ack 1"
Sending packet 2 to 127.0.0.1 port 21234
received message: "ack 2"
Sending packet 3 to 127.0.0.1 port 21234
received message: "ack 3"
Sending packet 4 to 127.0.0.1 port 21234
received message: "ack 4"


这篇关于如何在C [LINUX]中使用UDP套接字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆