If语句中不使用的字符串指针

时间:2012-11-27 14:25:45

标签: c string pointers strtok

使用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);

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 */
}

2 个答案:

答案 0 :(得分: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(相等)。

答案 1 :(得分:0)

我认为strtok没有找到任何可解析的令牌。我的意思是,没有“。”在输入字符串中。检查一下。