gpt4 book ai didi

c - 通过 UDP c 发送结构

转载 作者:行者123 更新时间:2023-11-30 15:32:52 25 4
gpt4 key购买 nike

我在通过 UDP 发送结构时遇到问题。我是网络和 C 的新手,但需要将数据作为结构(未序列化)发送。当我尝试发送时遇到段错误。新手如有错误请多多包涵。任何帮助将不胜感激。

服务器端:

struct TextMessage
{
unsigned int SenderId; /* unique client identifier */
char message[100]; /* text message */
};

int main(int argc, char *argv[])
{
int sock; /* Socket */
struct sockaddr_in echoServAddr; /* Local address */
struct sockaddr_in echoClntAddr; /* Client address */
unsigned int cliAddrLen; /* Length of incoming message */
unsigned short echoServPort; /* Server port */
int recvMsgSize; /* Size of received message */

struct TextMessage * temp = malloc(sizeof(struct TextMessage));


echoServPort = atoi(argv[1]); /* First arg: local port *

/* Create socket for sending/receiving datagrams */
if ((sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0)
DieWithError("socket() failed");

/* Construct local address structure */
memset(&echoServAddr, 0, sizeof(echoServAddr)); /* Zero out structure */
echoServAddr.sin_family = AF_INET; /* Internet address family */
echoServAddr.sin_addr.s_addr = htonl(INADDR_ANY); /* Any incoming interface */
echoServAddr.sin_port = htons(echoServPort); /* Local port */

/* Bind to the local address */
if (bind(sock, (struct sockaddr *) &echoServAddr, sizeof(echoServAddr)) < 0)
DieWithError("bind() failed");

for (;;) /* Run forever */
{
/* Set the size of the in-out parameter */
cliAddrLen = sizeof(echoClntAddr);

/* Block until receive message from a client */
if ((recvMsgSize = recvfrom(sock, &temp, sizeof(temp), 0,
(struct sockaddr *) &echoClntAddr, &cliAddrLen)) < 0)
DieWithError("recvfrom() failed");

printf("Handling client %s\n", inet_ntoa(echoClntAddr.sin_addr));
printf("Incoming Length: %s\n", cliAddrLen);
printf("Received: %s\n", temp.message); /* Print the string in sent struct */

}
/* NOT REACHED */
}

客户端:

struct TextMessage
{
unsigned int SenderId; /* unique client identifier */
char message[100]; /* text message */
};

int main(int argc, char *argv[])
{
int sock; /* Socket descriptor */
struct sockaddr_in echoServAddr; /* Echo server address */
struct sockaddr_in fromAddr; /* Source address of echo */
unsigned short echoServPort; /* Echo server port */
unsigned int fromSize; /* In-out of address size for recvfrom() */
char *servIP; /* IP address of server */
int structLen; /* Length of string to echo */
int respStringLen; /* Length of received response */


struct TextMessage newMess;

memset(&newMess, 0, sizeof(newMess)); /* Zero out structure */
newMess.request_Type = Send;
newMess.SenderId = 300;
newMess.RecipientId = 200;
strcpy(newMess.message, argv[3]);

printf("Size struct: %d\n", structLen);

servIP = argv[1]; /* First arg: server IP address (dotted quad) */
echoServPort = argv[2]; /* Use given port, if any */

/* Create a datagram/UDP socket */
if ((sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0)
DieWithError("socket() failed");

/* Construct the server address structure */
memset(&echoServAddr, 0, sizeof(echoServAddr)); /* Zero out structure */
echoServAddr.sin_family = AF_INET; /* Internet addr family */
echoServAddr.sin_addr.s_addr = inet_addr(servIP); /* Server IP address */
echoServAddr.sin_port = htons(echoServPort); /* Server port */


/* Send the string to the server */
if (sendto(sock, (void *)&newMess, sizeof(newMess), 0, (struct sockaddr *)
&echoServAddr, sizeof(echoServAddr)) != structLen)
DieWithError("sendto() sent a different number of bytes than expected\n");

close(sock);
exit(0);
}

最佳答案

我获取了您的代码并对其进行了更新,以便它能够按您的预期工作。

代码如下:

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

void DieWithError(char *err) {
perror(err);
exit(1);
}

struct TextMessage
{
char request_Type[100];
unsigned int SenderId; /* unique client identifier */
unsigned int RecipientId; /* unique client identifier */
char message[100]; /* text message */
};

int main(int argc, char *argv[])
{
int sock; /* Socket */
struct sockaddr_in echoServAddr; /* Local address */
struct sockaddr_in echoClntAddr; /* Client address */
unsigned int cliAddrLen; /* Length of incoming message */
unsigned short echoServPort; /* Server port */
int recvMsgSize; /* Size of received message */

struct TextMessage * temp = malloc(sizeof(struct TextMessage));

if (!argv[1]) {
fprintf(stderr,"no port number provided");
exit(1);
}

echoServPort = atoi(argv[1]); /* First arg: local port */

/* Create socket for sending/receiving datagrams */
if ((sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0)
DieWithError("socket() failed");

/* Construct local address structure */
memset(&echoServAddr, 0, sizeof(echoServAddr)); /* Zero out structure */
echoServAddr.sin_family = AF_INET; /* Internet address family */
echoServAddr.sin_addr.s_addr = htonl(INADDR_ANY); /* Any incoming interface */
echoServAddr.sin_port = htons(echoServPort); /* Local port */

/* Bind to the local address */
if (bind(sock, (struct sockaddr *) &echoServAddr, sizeof(echoServAddr)) < 0)
DieWithError("bind() failed");

for (;;) /* Run forever */
{
/* Set the size of the in-out parameter */
cliAddrLen = sizeof(echoClntAddr);

/* Block until receive message from a client */
if ((recvMsgSize = recvfrom(sock, temp, sizeof(*temp), 0,
(struct sockaddr *) &echoClntAddr, &cliAddrLen)) < 0)
DieWithError("recvfrom() failed");

printf("Handling client %s\n", inet_ntoa(echoClntAddr.sin_addr));
printf("Incoming Length: %u\n", cliAddrLen);
printf("Received: %s\n", temp->message); /* Print the string in sent struct */

}
/* NOT REACHED */

close(sock);
return 0;
}

对于客户端:

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

void DieWithError(char *err) {
perror(err);
exit(1);
}

struct TextMessage
{
char request_Type[100];
unsigned int SenderId; /* unique client identifier */
unsigned int RecipientId; /* unique client identifier */
char message[100]; /* text message */
};

int main(int argc, char *argv[])
{
int sock; /* Socket descriptor */
struct sockaddr_in echoServAddr; /* Echo server address */
struct sockaddr_in fromAddr; /* Source address of echo */
unsigned short echoServPort; /* Echo server port */
unsigned int fromSize; /* In-out of address size for recvfrom() */
char *servIP; /* IP address of server */
int structLen; /* Length of string to echo */
int respStringLen; /* Length of received response */


if (!argv[1]) {
fprintf(stderr,"No server IP sepcified at arg 1\n");
exit(1);
}

else if (!argv[2]) {
fprintf(stderr,"No port Number Sepcified at arg 2\n");
exit(2);
}

else if (!argv[3]) {
fprintf(stderr,"no message string specified at arg 3\n");
exit(3);
}



struct TextMessage newMess = { "Send" , 300 , 200 };

// memset(&newMess, 0, sizeof(newMess)); /* Zero out structure */
// strcpy(newMess.request_Type,"Send");
// newMess.SenderId = 300;
// newMess.RecipientId = 200;
strncpy(newMess.message, argv[3],sizeof(argv[3]));

printf("Size struct: %d\n", sizeof(newMess));

servIP = argv[1]; /* First arg: server IP address (dotted quad) */
echoServPort = atoi(argv[2]); /* Use given port, if any */

/* Create a datagram/UDP socket */
if ((sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0)
DieWithError("socket() failed");

/* Construct the server address structure */
memset(&echoServAddr, 0, sizeof(echoServAddr)); /* Zero out structure */
echoServAddr.sin_family = AF_INET; /* Internet addr family */
echoServAddr.sin_addr.s_addr = inet_addr(servIP); /* Server IP address */
echoServAddr.sin_port = htons(echoServPort); /* Server port */

int tempint = 0;

/* Send the string to the server */
tempint = sendto(sock, (struct TextMessage*)&newMess, (1024+sizeof(newMess)), 0, (struct sockaddr *)
&echoServAddr, sizeof(echoServAddr));

if (tempint == -1 ) {

printf("Sent struct size: %d\n", tempint);
DieWithError("sendto() sent a different number of bytes than expected\n");
}

close(sock);
exit(0);
}

关于c - 通过 UDP c 发送结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23966080/

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