Client doesn't recognize '+'!

This is a discussion on Client doesn't recognize '+'! within the Networking/Device Communication forums, part of the General Programming Boards category; Hello! I'm creating a simple pop3 client, but the problem is that my code doesn't recognize the '+' character! I ...

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    30

    Client doesn't recognize '+'!

    Hello!
    I'm creating a simple pop3 client, but the problem is that my code doesn't recognize the '+' character! I don't get it! It should work!!!


    Code:
    #include <sys/socket.h>
    #include <sys/types.h>
    #include <netinet/in.h>
    #include <arpa/inet.h>
    #include <iostream>
    #include <sys/io.h>
    
    using namespace std;
    
    int main(){
    
    	int sock_fd, recv_data_size;
    	char buffer[100];
    	
    	struct sockaddr_in server;
    	
    	if((sock_fd = socket(AF_INET, SOCK_STREAM, 0)) == -1){
    		perror("socket();");
    	}
    
    	server.sin_family = AF_INET;
    	server.sin_port = htons(324);
    	server.sin_addr.s_addr = inet_addr("127.0.0.1");
    
    	if(connect(sock_fd, (struct sockaddr *)&server, sizeof(struct sockaddr)) == -1){
    		perror("connect();");
    	}
    	
    	recv_data_size = recv(sock_fd, buffer, sizeof(buffer), 0);
    	
    	for(int i=0; i<recv_data_size; i++){
    		if(strcmp(buffer, "+") == 0){
    			cout << "+\n";
    		}	
    	}
    
    return 0;
    }
    It doesn't print out '+'! Thank you!

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,496
    Using string compare to compare 1 character is pretty pointless.

    And you're only ever comparing with the start of the buffer.

    You could do this
    Code:
    		if(strncmp( &buffer[i], "+", 1) == 0){
    			cout << "+\n";
    		}
    But this is better
    Code:
    		if ( buffer[i] == '+' ) {
    			cout << "+\n";
    		}
    Also, use std::endl in place of "\n" inside string constants.
    Unlike in C, a "\n" does NOT guarantee that the output buffer will be flushed.
    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
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Well you are asking it if the string you just got is the same as "+".
    Try this:
    Code:
    for (int i=0; i < recv_data_size; i++)
    if ( buffer[i] == '+' )
      cout<<"+\n";
    else
      cout<<buffer[i]

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    30
    Thank you!!!
    This works:

    if(strncmp( &buffer[i], "l", 1) == 0){
    cout << "+\n";
    }

    But what does &buffer mean? Why do I need to use &?

    and this works allso!
    if(buffer[i] == '+'){
    cout << "+\n";
    }
    Thank you!!!

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,496
    &buffer[i] is a pointer to the i'th character of the buffer.

    strcmp() needs pointers to characters.

    strcmp(buffer,"foo") and strcmp(&buffer[0],"foo) are the same thing.
    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.

  6. #6
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Instead of &buffer[i] wouldn't (buffer + i) be better then you don't have to do any dereferencing?

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,496
    > Instead of &buffer[i] wouldn't (buffer + i) be better then you don't have to do any dereferencing?
    Since neither is a dereference, what's your question?
    Both statements are the same, and compute the address of the i'th element of buffer.
    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.

  8. #8
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Well buffer[i] would get the value, since array names are oftened stored as pointers we are derefencing the name to get the value. Then the & would be applied. I guess any good compiler would optimize it so that it really becomes (buffer+i)

  9. #9
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,496
    > since array names are oftened stored as pointers
    You so need an "arrays are not pointers" refresher course.
    http://www.eskimo.com/~scs/C-faq/s6.html

    > we are derefencing the name to get the value.
    Nothing in &foo[bar] implies the result of foo[bar] ever being evaluated as a dereference.

    > I guess any good compiler would optimize it so that it really becomes (buffer+i)
    Every compiler understands them as being equivalent - quality and optimisation has nothing to do with it.

    I'm shocked at this nonsense!
    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.

  10. #10
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    NM: I know what I mean but don't know how to explain it. I already know those points you mentoined.
    Last edited by Thantos; 01-18-2005 at 01:40 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to Send Mac Address From Client to Server
    By Lieyza197 in forum C Programming
    Replies: 2
    Last Post: 05-27-2009, 09:58 AM
  2. Socket Programming Problem!!!!
    By bobthebullet990 in forum Networking/Device Communication
    Replies: 2
    Last Post: 02-21-2008, 06:36 PM
  3. WSAAsyncSelect Socket Model. She's Not Hot.
    By Tonto in forum Networking/Device Communication
    Replies: 2
    Last Post: 03-24-2007, 08:34 AM
  4. Replies: 1
    Last Post: 09-18-2005, 09:06 PM
  5. Unicode vurses Non Unicode client server application with winsock2 query?
    By dp_76 in forum Networking/Device Communication
    Replies: 0
    Last Post: 05-16-2005, 07:26 AM

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