我在通过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);
}
答案 0 :(得分: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);
}