gpt4 book ai didi

c - 为什么 UDP 组播服务器没有响应?

转载 作者:行者123 更新时间:2023-12-03 02:52:06 26 4
gpt4 key购买 nike

我正在尝试在 Linux 上实现基本的 UDP 多播客户端和服务器。服务器根据客户端发送的消息,应该用系统参数进行回复(有点像 SNMP)。现在,我正在使用一台服务器进行测试。在不同的终端上运行客户端和服务器后,我向服务器发送了一个 3 个字符的请求,但服务器似乎无法继续,只是保持挂起状态,等待客户端。代码如下:

客户端:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
#include <sys/time.h>
#include <time.h>

char* createheader(int , int , int , char , int );

#define PORT 15002
#define MAXLINE 10000
char* MULTICAST = "224.0.0.3";
char* myIP = "127.0.0.1";

// Driver code
int main(int argc, char *argv[])
{
char buffer[10000];
char message[10000];
char *msg;
int sockfd, n;
char c = '0';
int req1, req2, req3;

int ctr = 0;
int prev = 0, curr = 0;

char tp = 'Q';
int seq = 0, len;
int yes = 1;
short int resendflag = 0;
struct timeval time1, time2, tv={2,0}; // structures that can take time in seconds and micro seconds.

struct sockaddr_in servaddr;
bzero(&servaddr, sizeof(servaddr));

servaddr.sin_addr.s_addr = inet_addr(MULTICAST);
servaddr.sin_port = htons(PORT);
servaddr.sin_family = AF_INET;

struct in_addr interface_addr;
interface_addr.s_addr = inet_addr(myIP);


// create datagram socket
sockfd = socket(AF_INET, SOCK_DGRAM, 0);
setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (int*) &yes, sizeof(yes));

if (sockfd < 0) {
perror("Error: socket");
exit(1);
}

/*if(connect(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0)
{
printf("\n Error : Connect Failed \n");
exit(0);
}*/

setsockopt(sockfd,SOL_SOCKET,SO_RCVTIMEO,(char *)&tv,sizeof(struct timeval));

//u_char loop;
//setsockopt(sockfd, IPPROTO_IP, IP_MULTICAST_LOOP, &loop, sizeof(loop));

//Create Tx Header

while(1)
{
prev = seq;
seq++;

printf("\nEnter the 3 request characters, each followed by newline\n");
scanf("%d%d%d", &req1, &req2, &req3);
msg = createheader(req1, req2, req3, tp, seq);


while(c!='\0')
{
c = *(msg+ctr);
//puts(&c);
message[ctr] = c;
ctr++;
}
//printf("\nsize of %d",sizeof(c));
c = '0';
ctr = 0;

//msg = NULL;

// connect to server


// request to send datagram
// connect stores the peers IP and port

sendto(sockfd, message, sizeof(message), 0, (struct sockaddr*)&servaddr, sizeof(servaddr));
puts(message);
// waiting for response
n = recvfrom(sockfd, buffer, sizeof(buffer), 0, (struct sockaddr*)&servaddr, &len);

此后的部分特定于我的应用程序:除非您想测试它,否则可能会被忽略。

        curr = buffer[4]-'0' + (buffer[5]-'0')*256 + (buffer[6]-'0')*65536 + (buffer[7]-'0')*65536*256;

if (prev == curr || n == -1)
{
resendflag = 1;
while(resendflag)
{
printf("No Response Recieved. Resending...\n");
sendto(sockfd, message, strlen(message), 0, (struct sockaddr*)&servaddr, sizeof(servaddr));
n = recvfrom(sockfd, buffer, sizeof(buffer), 0, (struct sockaddr*)NULL, NULL);
curr = buffer[4]-'0' + (buffer[5]-'0')*256 + (buffer[6]-'0')*65536 + (buffer[7]-'0')*65536*256;

if (prev == curr || n == -1)
{
resendflag = 1;
}
else
{
resendflag = 0;
puts(buffer+12);
}

sleep(1);
}
}
else
puts(buffer+12);

//tp = 'A';
//msg = createheader(req1, req2, req3, tp, seq);
// close the descriptor

}
close(sockfd);
}

char* createheader(int req1, int req2, int req3, char tp, int seq)
{
static char msg1[1000];
int len;
//char req;
msg1[0] = 'A';
msg1[1] = tp;
msg1[3] = '0';
msg1[4] = seq%256+'0';
msg1[5] = (seq/256)%256+'0';
msg1[6] = (seq/65536)%256+'0';
msg1[7] = (seq/(65536*256))%256+'0';
msg1[8] = req1+'0';
msg1[9] = req2+'0';
msg1[10] = req3+'0';
msg1[12] = '\0';
len = strlen(msg1);

msg1[2] = len +'0';


return msg1;

}

这是服务器:

    #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <sys/time.h>
#include <time.h>

#define PORT 15002
#define MAXLINE 10000
char* MULTICAST = "224.0.0.3";
char* myIP = "127.0.0.2";

char* createheader(int , int , int , char , int , int);
char* sysfunc(int , int , int);

// Driver code
int main(int argc, char *argv[])
{
setbuf(stdout, NULL);
printf("lololol5"); //Just some indicators to see the progress
char buffer[10000];
char *message = "Hello Client";
char msg[10000];
char *msg1;
char tp = 'R';
int yes = 1;
int listenfd, len, l=0, seq = 0, req1, req2, req3, i, curr = 0, exc = 0;
const char* syscl= NULL;

int drop;
srand(time(NULL));

FILE* fp;

struct sockaddr_in servaddr, cliaddr;
bzero(&servaddr, sizeof(servaddr));
printf("lololol4");
// Create a UDP Socket
listenfd = socket(AF_INET, SOCK_DGRAM, 0);
if (listenfd < 0) {
perror("socket");
exit(1);
}

if (setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, (int*)&yes, sizeof(yes))<0)
{
perror("socket");
exit(1);
}

printf("lololol3");

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

char *ip = inet_ntoa(servaddr.sin_addr);
printf("\nip is %s\n", ip);

// bind server address to socket descriptor
if (bind(listenfd, (struct sockaddr *) &servaddr, sizeof(servaddr)) < 0) {
perror("bind");
exit(1);
}

printf("lololol2");

struct ip_mreq mreq;
mreq.imr_multiaddr.s_addr = inet_addr(MULTICAST);
mreq.imr_interface.s_addr = inet_addr(myIP);

char *ip1 = inet_ntoa(mreq.imr_multiaddr);
printf("\nmulip is %s\n", ip1);

if (setsockopt(listenfd, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char*) &mreq, sizeof(mreq)) < 0)
{
perror("setsockopt");
return 1;
}
while(1)
{
//receive the datagram
len = sizeof(cliaddr);
int n = recvfrom(listenfd, (char*) buffer, sizeof(buffer), 0, (struct sockaddr*)&cliaddr, &len); //receive message from client

和以前一样,应用程序特定部分。

printf("\nHi\n"); 
puts(buffer); //display message
printf("\n");
curr = buffer[4]-'0' + (buffer[5]-'0')*256 + (buffer[6]-'0')*65536 + (buffer[7]-'0')*65536*256;//acquire client seq. no

req1 = buffer[8]; req2 = buffer[9]; req3 = buffer[10];//extract request bytes
syscl = sysfunc(req1-48, req2-48, req3-48); //Function call to get system corresponding system command string

l = 12; //After 12 bytes of header
puts(syscl);

if (strcmp(syscl, "\nInvalid Command...\n\0"))
{
system(syscl); //The system call with the string gotten
fp = fopen("sysstat.txt","r"); //Open the file where system output is written
while(!feof(fp))
{
msg[l] = fgetc(fp); //write file into an array
l++;
}
fclose(fp);
msg[l] = '\0'; //Append string with end of text char

}
else
{
char c1 = '0';
while(c1 != '\0')
{
c1 = *(syscl+l-12); //write file into an array
//printf("\n %c", msg[l]);
msg[l] = c1;
l++;
}
}



msg1 = createheader(req1, req2, req3, tp, curr, l); //Create Header

for (i=0; i<12; i++)
{
msg[i] = *(msg1+i);

}

drop = rand()%10+1; //to simulate dropped packets

if (drop > 2) //Drop with a given prob (i.e (x-1)/10)
{
sendto(listenfd, &msg, MAXLINE, 0,(struct sockaddr*)&cliaddr, sizeof(cliaddr));
puts(msg);
}

l = 0;
}
}

char* createheader(int req1, int req2, int req3, char tp, int seq, int len) //header
{
static char msg1[10000];
//int len;
//char req;
msg1[0] = '$'; //Start Char
msg1[1] = tp; //Type of req.
//msg1[3] = '0';
msg1[4] = seq%256+'0'; //4-7: seq no in little endian
msg1[5] = (seq/256)%256+'0';
msg1[6] = (seq/65536)%256+'0';
msg1[7] = (seq/(65536*256))%256+'0';
msg1[8] = req1;
msg1[9] = req2;
msg1[10] = req3;
msg1[11] = '0';//Reserved Byte, Also for alignment
//len = strlen(msg1);

msg1[2] = (len +'0')%256+'0'; //2-3:Length in lil' endian
msg1[3] = ((len+'0')/256)%256+'0';

return msg1;

}

char* sysfunc(int req1, int req2, int req3)
{
static char syscl[100];
//printf("\ncomm %d\n", req2);
//printf("\ncomm %d\n", req3);

switch (req2)
{
case 1: //Hardware
{
switch (req3)
{
case 1: strcpy(syscl, "lscpu > sysstat.txt\0"); //CPU
break;

case 2: strcpy(syscl, "lsmem > sysstat.txt\0"); //Memoru=y
break;

case 3: strcpy(syscl, "lsblk > sysstat.txt\0"); //HDDs
break;

case 4: strcpy(syscl, "lspci > sysstat.txt\0"); //PCI Add-Ons
break;

default: strcpy(syscl, "\nInvalid Command...\n\0"); //Default
break;
}

}
break;

case 2: //OS
{
switch (req3)
{
case 1: strcpy(syscl, "hostname > sysstat.txt\0");//Hostname
break;

case 2: strcpy(syscl, "hostnamectl > sysstat.txt\0");//OS and Kernel
break;

case 3: strcpy(syscl, "uptime > sysstat.txt\0");//Uptime
break;

default: strcpy(syscl, "\nInvalid Command...\n\0");
break;
}
}
break;

case 3: //Network
{
switch (req3)
{
case 1: strcpy(syscl, "ip link show > sysstat.txt\0");//Ifs
break;

case 2: strcpy(syscl, "ifconfig | grep ether > sysstat.txt\0");//Ethernet
break;

case 3: strcpy(syscl, "ifconfig > sysstat.txt\0");//IP
break;

case 4: strcpy(syscl, "route -n > sysstat.txt\0");//Routing Table
break;

default: strcpy(syscl, "\nInvalid Command...\n\0");
break;
}
}
break;

default: strcpy(syscl, "\nInvalid Command...\n\0");
break;
}

return syscl;
}

已经搞了2天了。无法弄清楚我哪里出错了。由于服务器没有显示任何反应,我假设初始部分(服务器端的recvfrom)存在问题,因此以这种方式分割代码。抱歉,如果我犯了一些明显的菜鸟错误。

最佳答案

看来您在服务器中选择了错误的接口(interface)本地IP地址(您从哪里获得“127.0.0.2”?) - 如果我更改

    mreq.imr_interface.s_addr = inet_addr(myIP);

    mreq.imr_interface.s_addr = INADDR_ANY;

服务器开始接收。

Aren't 127.0.0.0/8 block of local addresses?

这些是环回地址(请参阅 What is the rest of the 127.0.0.0/8 address space used for? )。 struct ip_mreq定义中的注释接口(interface)的本地IP地址可能有点误导,因为它可以提醒localhost,但它实际上是指从外部看到的分配给本地主机接口(interface)的IP地址。

It is still not working with INADDR_ANY.

通常,“不工作”是一个不充分的问题描述。您可以收集更多信息。 G。通过使用 strace -enetwork ... 运行服务器。我建议先把不必要的复杂性(例如 Mininet)放在一边,直到程序在纯网络上运行为止。

关于c - 为什么 UDP 组播服务器没有响应?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58098389/

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