Problems with string pointers in IF statement

This is a discussion on Problems with string pointers in IF statement within the C Programming forums, part of the General Programming Boards category; Hi, Used strtok to parse a string and now I am having difficulties with my IF statement. Suspect that I ...

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    3

    Problems with string pointers in IF statement

    Hi,

    Used strtok to parse a string and now I am having difficulties with my IF statement. Suspect that I am using the wrong case (value vs. address) but I have run out of ideas. Any help would be greatly appreciated.

    The abbreviated code is below. The full code source is also included.

    Code:
    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. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,672
    Your indentation is a nice serving of dog food (puke)
    SourceForge.net: Indentation - cpwiki
    It looks like code you just copy/pasted from somewhere else.

    c - String Pointers Used in If Statements Not Working - Stack Overflow
    At least they got indented code.

    Code:
    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" */
    1. Show us your test results - what you saw printed.
    2. The first bug is that recvfrom() does not automatically append a \0 to make it a proper C-string you can print or use str... functions with.

    Code:
    // note -1 to allow for later appending of \0
    if ((recvMsgSize = recvfrom(sock, echoBuffer, ECHOMAX-1, 0, (struct sockaddr *) &echoClntAddr, &cliAddrLen)) < 0)
        DieWithError("recvfrom() failed");
    echoBuffer[recvMsgSize] = '\0';
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Nov 2012
    Posts
    3
    My apologies for the original lack of indentation (unfamiliar with tags code, /code). Attempted to correct but exceeded 1 hour Edit time limit. Again my apologies. Will supply exact test results later today (different computers), however line# 74 printf result is "0" as expected from strtok. Not sure if this helps without further detail.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,672
    You might want to use a debugger, to actually inspect the contents of echoBuffer without relying on the data being printable (as with say %s).
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Nov 2012
    Posts
    3

    Resolved

    Quote Originally Posted by Salem View Post
    You might want to use a debugger, to actually inspect the contents of echoBuffer without relying on the data being printable (as with say %s).
    Problem caused by inadvertently including prefix “String Data:” in string data sent from the other end of the socket.

    Thanks for your assistance.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. having problems with this typedef statement
    By shakisparki in forum C Programming
    Replies: 1
    Last Post: 03-30-2011, 08:07 AM
  2. if statement and pointers
    By sababa.sababa in forum C Programming
    Replies: 10
    Last Post: 12-15-2009, 01:56 PM
  3. problems with scanf/if statement plz help
    By dezz101 in forum C Programming
    Replies: 22
    Last Post: 03-30-2008, 01:12 PM
  4. Problems with string variables in an IF statement
    By Calef13 in forum C++ Programming
    Replies: 5
    Last Post: 12-30-2005, 09:40 PM
  5. Switch statement problems
    By ComDriver in forum C++ Programming
    Replies: 5
    Last Post: 02-21-2005, 10:31 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21