gpt4 book ai didi

linux - 用于多个以太网端口的多个 udp 套接字

转载 作者:太空宇宙 更新时间:2023-11-04 12:25:22 24 4
gpt4 key购买 nike

我的 Linux 运行在一个带有多以太网端口的设备上,我已经设置了每个端口,现在我的 UDP 回显服务器运行在我的 Linux 上。我已经从 packetsender 应用程序发送数据包并从同一端口接收回来。具体如下:

192.168.1.100

192.168.1.101

192.168.1.102

192.168.1.103 是 4 个以太网端口

当我将数据包发送到 192.168.1.100 并从该端口返回回声时,当我为第二个端口设置网络并将数据包发送到 192.168.1.101 时,我从 192.168.1.100 收到回声虽然它们是在 192.168.1.101 交付的,但我知道套接字被内核绑定(bind)到第一个端口,如果我想让我的多端口将每个套接字绑定(bind)到端口。要做什么?这是代码套接字的单端口绑定(bind)。我应该使用 select() 并进行相同的绑定(bind),说明每个端口的 IP 地址吗?内核正在分配自己选择的套接字绑定(bind)。

#include <stdio.h>

int main(int argc, char **argv)
{
printf("Hello World!\n");

return 0;
}
*/


#include <stdio.h> /* standard C i/o facilities */
#include <stdlib.h> /* needed for atoi() */
#include <unistd.h> /* defines STDIN_FILENO, system calls,etc */
#include <sys/types.h> /* system data type definitions */
#include <sys/socket.h> /* socket specific definitions */
#include <netinet/in.h> /* INET constants and stuff */
#include <arpa/inet.h> /* IP address conversion stuff */
#include <netdb.h> /* gethostbyname */



/* this routine echos any messages (UDP datagrams) received */

#define MAXBUF 1024*1024

void echo( int sd ) {
int len,n;
char bufin[MAXBUF];
struct sockaddr_in remote;

/* need to know how big address struct is, len must be set before the
call to recvfrom!!! */

len = sizeof(remote);

while (1) {
/* read a datagram from the socket (put result in bufin) */
n=recvfrom(sd,bufin,MAXBUF,0,(struct sockaddr *)&remote,&len);

/* print out the address of the sender */
printf("Got a datagram from %s port %d\n",
inet_ntoa(remote.sin_addr), ntohs(remote.sin_port));

if (n<0) {
perror("Error receiving data");
} else {
printf("GOT %d BYTES\n",n);
printf("%s\n",bufin);
/* Got something, just send it back */

sendto(sd,bufin,n,0,(struct sockaddr *)&remote,len);
// ClUSTERING
}
}
}

/* server main routine */

int main() {
int ld;
struct sockaddr_in skaddr;
int length;

/* create a socket
IP protocol family (PF_INET)
UDP protocol (SOCK_DGRAM)
*/

if ((ld = socket( PF_INET, SOCK_DGRAM, 0 )) < 0) {
printf("Problem creating socket\n");
exit(1);
}

/* establish our address
address family is AF_INET
our IP address is INADDR_ANY (any of our IP addresses)
the port number is assigned by the kernel
*/

skaddr.sin_family = AF_INET;
skaddr.sin_addr.s_addr = htonl(INADDR_ANY);
skaddr.sin_port = htons(0);

if (bind(ld, (struct sockaddr *) &skaddr, sizeof(skaddr))<0) {
printf("Problem binding\n");
exit(0);
}

/* find out what port we were assigned and print it out */

length = sizeof( skaddr );
if (getsockname(ld, (struct sockaddr *) &skaddr, &length)<0) {
printf("Error getsockname\n");
exit(1);
}

/* port number's are network byte order, we have to convert to
host byte order before printing !
*/
printf("The server UDP port number is %d\n",ntohs(skaddr.sin_port));

/* Go echo every datagram we get */
echo(ld);
return(0);
}

最佳答案

您将套接字绑定(bind)到 INADDR_ANY,这意味着任何 IP 地址。如果这不是您想要的,请将其绑定(bind)到特定的 IP 地址。但是那样你需要多个套接字。你现在拥有它的方式,你没有。

关于linux - 用于多个以太网端口的多个 udp 套接字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44905701/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com