strtok を使用して文字列を解析しましたが、IF ステートメントで問題が発生しています。間違ったケース (値とアドレス) を使用していると思われますが、アイデアが不足しています。どんな助けでも大歓迎です。
一連の printf を使用して、strtok が "hldType" を正しく設定したことを確認しました。助けてくれてありがとう。私は何日もこれにこだわっています。
短縮コードは以下です。完全なコード ソースも含まれています。
char *hldType; /* Parsing holding field */
static const char *REQTYPE = "0"; /* Comparison */
hldType = strtok(echoBuffer, "."); /* Parse the string */
if (strcmp(hldType,REQTYPE) == 0) /* NOT WORKING */
printf("REQTYPE myString: %s\n", REQTYPE);
コード:
#include <stdio.h> /* for printf() and fprintf() */
#include <sys/socket.h> /* for socket() and bind() */
#include <arpa/inet.h> /* for sockaddr_in and inet_ntoa() */
#include <stdlib.h> /* for atoi() and exit() */
#include <string.h> /* for memset() */
#include <unistd.h> /* for close() */
#include <time.h> /* Display time */
#define ECHOMAX 255 /* Longest string to echo */
void DieWithError(char *errorMessage); /* External error handling function */
/* User Defined type */
typedef struct _ServerMessage{
enum {New, Old, No_Message} messageType; /* same size as an unsigned int */
unsigned int SenderId; /* unique client identifier */
unsigned int RecipientId; /* unique client identifier */
char message[100]; /* text message */
} ServerMessage; /* an unsigned int is 32 bits = 4 bytes */
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 */
char echoBuffer[ECHOMAX]; /* Buffer for echo string */
unsigned short echoServPort; /* Server port */
int recvMsgSize; /* Size of received message */
char *hldType; /* Parsing holding field */
char *hldSend; /* Parsing holding field */
char *hldRecip; /* Parsing holding field */
char *hldMsg; /* Parsing holding field */
char tmpType[1]; /* Type of action requested by client, where 0 is Send and 1 is Received */
static const char *REQTYPE = "0";
/* Test Struct */
/*ServerMessage ServerMessage_new = {No_Message, 1234,5678,"Hello Server World - No Message"}; */
ServerMessage ServerMessage_new[100];
/* printf("Message Type: %d\n", ServerMessage_new.messageType);
printf("Message SenderID: %04d\n", ServerMessage_new.SenderId);
printf("Message RecipentID: %04d\n", ServerMessage_new.RecipientId);
printf("Message Content: %s\n", ServerMessage_new.message); */
if (argc != 2) /* Test for correct number of parameters */
{
fprintf(stderr,"Usage: %s <UDP SERVER PORT>\n", argv[0]);
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, echoBuffer, ECHOMAX, 0,
(struct sockaddr *) &echoClntAddr, &cliAddrLen)) < 0)
DieWithError("recvfrom() failed");
printf("Handling client %s\n", inet_ntoa(echoClntAddr.sin_addr));
/* Parse string from client */
printf("echoBuffer Content: %s\n", echoBuffer);
hldType = strtok(echoBuffer, ".");
hldSend = strtok(NULL, ".");
hldRecip = strtok(NULL, ".");
hldMsg = strtok(NULL, ".");
printf("value of hldType: %s\n", hldType); /* Validated that it prints "0" */
/* Store message sent from client */
time_t now;
time(&now);
printf("%s", ctime(&now));
if (strcmp(hldType,REQTYPE) == 0) /* NOT WORKING */
printf("REQTYPE myString: %s\n", REQTYPE);
ServerMessage_new[0].messageType = atoi(hldType);
printf("hldType Content: %d\n", ServerMessage_new[0].messageType);
ServerMessage_new[0].SenderId = atoi(hldSend);
printf("hldSend Content: %04d\n", ServerMessage_new[0].SenderId);
ServerMessage_new[0].RecipientId = atoi(hldRecip);
printf("hldRecip Content: %04d\n", ServerMessage_new[0].RecipientId);
strncpy(ServerMessage_new[0].message, hldMsg, 40);
printf("hldMsg Content: %s\n", ServerMessage_new[0].message);
/* Send received datagram back to the client */
if (sendto(sock, echoBuffer, recvMsgSize, 0,
(struct sockaddr *) &echoClntAddr, sizeof(echoClntAddr)) != recvMsgSize)
DieWithError("sendto() sent a different number of bytes than expected");
}
/* NOT REACHED */
}