Note that there are some explanatory texts on larger screens.

plurals
  1. POcomparing two strings in c
    primarykey
    data
    text
    <p>I have a basic question on strings and char array's in C.</p> <p>below I have some basic code that compares data coming over a socket(telnet) with an existing string. This however fails. IF I compare a character in the char array it works in[1] == out[1]. Can you advise why the strings don't match and if I am missing something. Do I need to convert the char to anther type of string?</p> <p>thx Art</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;unistd.h&gt; #include &lt;stdlib.h&gt; #include &lt;string.h&gt; #include &lt;sys/types.h&gt; #include &lt;sys/socket.h&gt; #include &lt;netinet/in.h&gt; void dostuff(int); /* function prototype */ void error(const char *msg) { perror(msg); exit(1); } int main(int argc, char *argv[]) { int sockfd, newsockfd, portno, pid; socklen_t clilen; struct sockaddr_in serv_addr, cli_addr; if (argc &lt; 2) { fprintf(stderr,"ERROR, no port provided\n"); exit(1); } sockfd = socket(AF_INET, SOCK_STREAM, 0); if (sockfd &lt; 0) error("ERROR opening socket"); bzero((char *) &amp;serv_addr, sizeof(serv_addr)); portno = atoi(argv[1]); serv_addr.sin_family = AF_INET; serv_addr.sin_addr.s_addr = INADDR_ANY; serv_addr.sin_port = htons(portno); if (bind(sockfd, (struct sockaddr *) &amp;serv_addr, sizeof(serv_addr)) &lt; 0) error("ERROR on binding"); listen(sockfd,5); clilen = sizeof(cli_addr); while (1) { newsockfd = accept(sockfd, (struct sockaddr *) &amp;cli_addr, &amp;clilen); if (newsockfd &lt; 0) error("ERROR on accept"); pid = fork(); if (pid &lt; 0) error("ERROR on fork"); if (pid == 0) { close(sockfd); dostuff(newsockfd); exit(0); } else close(newsockfd); } /* end of while */ close(sockfd); return 0; /* we never get here */ } /******** DOSTUFF() ********************* There is a separate instance of this function for each connection. It handles all communication once a connnection has been established. *****************************************/ void dostuff (int sock) { int n; char buffer[2]; char kjh[] = "aaa"; bzero(buffer,256); n = read(sock,buffer,255); if (n &lt; 0) error("ERROR reading from socket"); if (strcmp(ptr,kjh) == 0) { printf("they are the same: %s.\n",buffer); do_other_stuff(); } printf("Here is the message!: %s\n",buffer); n = write(sock,"I got your message",19); if (n &lt; 0) error("ERROR writing to socket"); } void do_other_stuff() { printf("function works:"); } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload