您的位置:首页 > 其它

udp socket example

2018-03-06 11:48 113 查看
前一篇文章提及socket编程,其实只有一半,就是tcp编程.那么socket的udp编程是怎么样的呢?我找到一篇例子,这里做一点翻译。

原文:https://www.binarytides.com/programming-udp-sockets-c-linux/

linux
ubuntu
c


UDP sockets

本文描述了如何在Linux / Unix平台上使用C编写一个简单的echo服务器和客户端使用udp套接字。 UDP套接字或数据报套接字在许多方面与TCP套接字不同。 最重要的区别是UDP套接字不是面向连接的。 从技术上讲,UDP服务器不接受连接,udp客户端不连接到服务器。

服务器将绑定并直接接收数据,客户端应直接发送数据。

ECHO 服务器

因此,我们首先用UDP套接字制作一个非常简单的ECHO服务器。 代码的流程是

socket() -> bind() -> recvfrom() -> sendto()

/*
Simple udp server
*/
#include<stdio.h> //printf
#include<string.h> //memset
#include<stdlib.h> //exit(0);
#include<arpa/inet.h>
#include<sys/socket.h>

#define BUFLEN 512  //Max length of buffer
#define PORT 8888   //The port on which to listen for incoming data

void die(char *s)
{
perror(s);
exit(1);
}

int main(void)
{
struct sockaddr_in si_me, si_other;

int s, i, slen = sizeof(si_other) , recv_len;
char buf[BUFLEN];

//create a UDP socket
if ((s=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1)
{
die("socket");
}

// zero out the structure
memset((char *) &si_me, 0, sizeof(si_me));

si_me.sin_family = AF_INET;
si_me.sin_port = htons(PORT);
si_me.sin_addr.s_addr = htonl(INADDR_ANY);

//bind socket to port
if( bind(s , (struct sockaddr*)&si_me, sizeof(si_me) ) == -1)
{
die("bind");
}

//keep listening for data
while(1)
{
printf("Waiting for data...");
fflush(stdout);
memset(buf,’\0′, BUFLEN);

//try to receive some data, this is a blocking call
if ((recv_len = recvfrom(s, buf, BUFLEN, 0, (struct sockaddr *) &si_other, (socklen_t*)&slen)) == -1)
{
die("recvfrom()");
}

//print details of the client/peer and the data received
printf("Received packet from %s:%d\n", inet_ntoa(si_other.sin_addr), ntohs(si_other.sin_port));
printf("Data: %s\n" , buf);

//now reply the client with the same data
if (sendto(s, buf, recv_len, 0, (struct sockaddr*) &si_other, slen) == -1)
{
die("sendto()");
}
}

close(s);
return 0;
}


gcc server.c && a.out


下一步将是使用客户端连接到此服务器。 我们将在稍后制作客户端程序,但首先要测试此代码,我们可以使用netcat。

打开另一个终端并使用netcat连接到此udp服务器,然后发送一些数据。 相同的数据将被服务器发回。 在这里,我们使用nmap包中的ncat命令。

nc -vv localhost 8888 -u


注意:我们必须使用netcat,因为普通的telnet命令不支持udp协议。 netcat的-u选项指定udp协议。

netstat命令可用于检查udp端口是否打开。

netstat -u -a


请注意输出的*:8888条目。 这就是我们的服务器程序。

在“外部地址”列中有localhost:8888的条目指示连接到它的一些客户端,这里是netcat。

ECHO 客户端

现在我们已经使用netcat测试了我们的服务器,现在是创建客户端并使用它来代替netcat的时候了。

程序流程就像

socket() -> sendto()/recvfrom()

/*
Simple udp client
*/
#include<stdio.h> //printf
#include<string.h> //memset
#include<stdlib.h> //exit(0);
#include<arpa/inet.h>
#include<sys/socket.h>

#define SERVER "127.0.0.1"
#define BUFLEN 512  //Max length of buffer
#define PORT 8888   //The port on which to send data

void die(char *s)
{
perror(s);
exit(1);
}

int main(void)
{
struct sockaddr_in si_other;
int s, i, slen=sizeof(si_other);
char buf[BUFLEN];
char message[BUFLEN];

if ( (s=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1)
{
die("socket");
}

memset((char *) &si_other, 0, sizeof(si_other));
si_other.sin_family = AF_INET;
si_other.sin_port = htons(PORT);

if (inet_aton(SERVER , &si_other.sin_addr) == 0)
{
fprintf(stderr, "inet_aton() failed\n");
exit(1);
}

while(1)
{
printf("Enter message : ");
gets(message);

//send the message
if (sendto(s, message, strlen(message) , 0 , (struct sockaddr *) &si_other, slen)==-1)
{
die("sendto()");
}

//receive a reply and print it
//clear the buffer by filling null, it might have previously received data
memset(buf,'\0', BUFLEN);
//try to receive some data, this is a blocking call
if (recvfrom(s, buf, BUFLEN, 0, (struct sockaddr *) &si_other, &slen) == -1)
{
die("recvfrom()");
}

puts(buf);
}

close(s);
return 0;
}


gcc client.c -o client && ./client


结论

UDP套接字被DNS等协议所使用。使用UDP的主要思想是传输少量数据,并且可靠性不是一个非常重要的问题。 UDP也用于广播/多播。

当文件传输正在完成或大量数据正在传输时,传输必须更加可靠才能完成任务。 然后使用TCP套接字。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  C socket