Thread: Send/Recieve problem

  1. #1
    Registered User
    Join Date
    Jan 2003
    Posts
    99

    Send/Recieve problem

    I need some assistance. The server part of my program is not recieving the screen name that the client should be sending. I will include both the client and server source. The error in the client should be the send before the main loop and in the server it is the recv in first main block of code in the main loop.
    Last edited by _Cl0wn_; 05-14-2003 at 01:31 PM.

  2. #2
    Registered User
    Join Date
    Jan 2003
    Posts
    99
    This is the client

  3. #3
    Registered User
    Join Date
    Jan 2003
    Posts
    99
    And this is the server

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>cin.getline(buf, 1, '\n');
    Why limit the size to 1? That only leaves room for the \0, no actual data, making this a little pointless:
    >>len = strlen(buf);
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Registered User
    Join Date
    Jan 2003
    Posts
    99
    Ohh haha. I had made it like that to test something earlier and forgot to change it back. I still doesn't work though.

  6. #6
    Registered User
    Join Date
    Jan 2003
    Posts
    99
    Client is updated

  7. #7
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Your server is doing wrong here:

    >>if (recv(sock, name, MAX, 0) == -1)
    It should be :
    >>if (recv(newfd, name, MAX, 0) == -1)

    There might be other places where you've used "sock" when you should've used "newfd", so I'd check through your code, if I were you.


    Agghhh, wait a minute !! What's this:
    >>char *buf, *name, *userName, *ann;
    buf and name are only a pointers? But you used name to write data to (from recv), where is that pointer pointing to ??
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  8. #8
    Registered User
    Join Date
    Jan 2003
    Posts
    99
    Hehe. Both of those I caught and I believe updated them with the new client. When the screen name is sent to the server, it is now getting to the line what it shows their screen name but instead of showing the screen name is prints (null) on the server and the client seg faults due to an error in the server.

  9. #9
    Registered User
    Join Date
    Jan 2003
    Posts
    99
    Updated

  10. #10
    Registered User
    Join Date
    Sep 2002
    Posts
    417
    I want to do a very simple network thing where it passes a character array with 5 characters (6 with the null terminator)...

    both clients need to act like servers as well... I want it to be that when one person types in the 5 characters, it sends it out to the other... and vice versa.

    I only want two people (I'm fine with having to do IP stuff)... how do you do this? Do I need as much code as he had?

  11. #11
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    @Trauts: A good starting point: http://www.ecst.csuchico.edu/~beej/guide/net/
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  12. #12
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    @Clown: This:
    buf = name = userName = ann = NULL;
    doesn't solve your problem. You need to point the pointers at some valid memory where they can store data. Either that, or make them arrays instead:
    >>char name[LENGTH];
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  13. #13
    Registered User
    Join Date
    Jan 2003
    Posts
    99
    You won't need as much. You would probably only need one file to do it. You would just need to set up a menu at start up where the user can make a choice to become the server or client. If he wants to be a server then bind and listen else then he wants to be a client and then just connect to the server NOTE: the server would already need to be up to connect to. Then you could either user a while loop of send and recv or use select. I have found something that should help you with what you are doing.

    Alternating Chat

  14. #14
    Registered User
    Join Date
    Jan 2003
    Posts
    99
    Good ole Computer Science teacher was wrong. He said it would work but let's see if it solves the problem. One question that has been bothering me. The recv function, does it sit on the socket and wait for data to be sent or does it check the connection for data to be read at the time of calling and if there is no data to read it quits?

  15. #15
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>The recv function, does it sit on the socket and wait for data
    You need to research "blocking" and "non-blocking".

    A blocking socket will wait for data, and your program will hang until it gets some.

    A non-blocking socket will cause recv() to terminate early if there's no data (with a specific errno value).

    There is some examples in here and in Beej's guide (the link I posted for Trauts.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM