gpt4 book ai didi

c - Sendto 返回随机值

转载 作者:行者123 更新时间:2023-11-30 20:34:35 26 4
gpt4 key购买 nike

我在 udp 套接字客户端中调用 sendto,如下所示:

if((num_w = sendto(cli_udp_sock, buffer, strlen(buffer), 0, (struct sockaddr *) &servaddr, sizeof(servaddr) )) < 0)

但服务器端似乎没有收到任何内容,并且以非常奇怪的方式,num_w 在打印出来时给出如下值:

-197379208-14400769362054978424

perror() 显示“成功”

到目前为止的客户端代码:

#include <sys/socket.h>       /*  socket definitions        */
#include <sys/types.h> /* socket types */
#include <arpa/inet.h> /* inet (3) funtions */
#include <unistd.h> /* misc. UNIX functions */
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <errno.h>
#include <netdb.h>
/* Global constants */

#define MAX_LINE (1000)
#define LISTENQ (10)

/* Function declarations */
int ParseCmdLine(int argc, char *argv[], char **szPort, char **szAddress, char ** serv_udp_port);



/* main() */

int main(int argc, char *argv[]) {

//
short int cli_port; /* port number */
short int serv_port;
struct sockaddr_in servaddr; /* socket address structure */
socklen_t serv_addrlen = sizeof(servaddr);
struct sockaddr_in cliaddr;
char buffer[MAX_LINE]; /* character buffer */
char *szAddress; /* Holds remote IP address */
char *szPort; /* Holds remote port */
char *serv_udp_port;
char *endptr; /* for strtol() */
int cli_udp_sock;
/* Get command line arguments */

ParseCmdLine(argc, argv, &szPort, &szAddress, &serv_udp_port);


/* Set the remote port */

cli_port = strtol(szPort, &endptr, 0);
serv_port = strtol(serv_udp_port, &endptr, 0);

if ( *endptr )
{
printf("ECHOCLNT: Invalid port supplied.\n");
exit(EXIT_FAILURE);
}



/* Set all bytes in socket address structure to zero, and fill in the relevant data members */

memset(&cliaddr, 0, sizeof(cliaddr));
cliaddr.sin_family = AF_INET;
cliaddr.sin_port = htons(cli_port);
cliaddr.sin_addr.s_addr = htonl(INADDR_ANY);

memset(&servaddr, 0, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(serv_port);

/* Set the remote IP address */

if ( inet_aton(argv[2], &servaddr.sin_addr) <= 0 )
{
printf("ECHOCLNT: Invalid remote IP address.\n");
exit(EXIT_FAILURE);
}

int list_s;
if ( (list_s = socket(AF_INET, SOCK_STREAM, 0)) < 0 )
{
fprintf(stderr, "ECHOCLNT: Error creating listening socket.\n");
exit(EXIT_FAILURE);
}
if (listen(list_s, LISTENQ) < 0)
{
printf("Failed to called listen.\n");
}
if( ((cli_udp_sock = socket(AF_INET, SOCK_DGRAM, 0) )< 0))
{
printf("UDP socket was not successfully created. Exiting...\n");
exit(0);
}

//if(bind(cli_udp_sock, (struct sockaddr *) &cliaddr, sizeof(cliaddr)) > 0);

char command[100];
/* Get command from user */
printf("==============================================\n");

do
{
if(strcmp(command, "s\n") == 0)
{
printf("______________________________________________\n");
printf("'s' entered, \nEnter the string to request\n"); //prompt for string to enter
strcpy(buffer, "CAP\n"); //set first set of characteres to CAP\n
char bucket[100]; //space for entered string
fgets(bucket, sizeof bucket, stdin);
printf("The string entered is: %s", bucket);
strcat(buffer, bucket);

/* Send string to echo server, and retrieve response */
int num_w;
if((num_w = sendto(cli_udp_sock, buffer, strlen(buffer), 0, (struct sockaddr *) &servaddr, sizeof(servaddr) )) < 0)
{
perror("error: ");
printf("Problem writing data to server, errno: %d", errno);
exit(0);
}
else
{
perror("error 2: ");
printf("num_w: %d\n");
}

提前非常感谢!(哦,我使用的是 Kali linux、虚拟机、Windows 10)

最佳答案

您没有打印num_w。您已将其排除在 printf() 语句之外。您只是打印堆栈上的任何随机垃圾。

And perror() displays "success"

所以 num_w 是正数。在这种情况下调用 perror() 是没有意义的。

关于c - Sendto 返回随机值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42082150/

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