2012-11-27 30 views
2

使用strtok解析字符串,現在我遇到了IF語句的問題。懷疑我正在使用錯誤的案例(價值與地址),但我已經用完了想法。任何幫助將不勝感激。字符串指針用於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); 

CODE:

#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 */ 
} 
+0

做一個'printf(「[%s] [%s] \ n」,s1,s2)'是看看字符串是否等於真.. – Jack

回答

1

試試這個:

printf("comparing [%s] to [%s]\n", hldType, REQTYPE); 
    if (strcmp(hldType,REQTYPE) == 0) /* NOT WORKING */ 
     printf("REQTYPE myString: %s\n", REQTYPE); 

你可以在你的字符串中尋找額外的空格。您也可以打印來自strcmp()的返回值。

如果hldTypeREQTYPE均爲"0",然後strcmp()應該返回0(相等)。

+0

很高興聽到IF條件應該返回0.我認爲我在比較字符串指針(值與地址等)時做了錯誤。我會嘗試你的建議,並在今天晚些時候發佈我的結果。謝謝你的幫助。 – aww91

+0

這裏是我上面的printf語句的結果:比較[String Data:0]和[0] – aww91

+0

因此'hldType'是'「String Data:0」'而不僅僅是'「0」'。如果你想比較零,你需要去掉'「String Data:」前綴。您是否意外地在套接字的另一端包含''String Data:「'? – tomlogic

0

我覺得strtok的是沒有找到任何可解析的標記。我的意思是,沒有「。」在輸入字符串中。覈實。

+0

感謝您的及時回覆。項目hldType正在由strtok填充。我已經使用一系列printf證實了這一點。 – aww91