Thread: Need help with sending an int via sockets.

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

    Post Need help with sending an int via sockets.

    Howdy all,

    I've been having touble trying to get my head around how (UNIX) sockets actually send and receive an int or an int variable via a streaming socket.

    I have a value (int px) and I have a socket (sa) and connect (c), and wish to send it from the server to the client. My understanding is that it goes something along these lines:

    For the server:

    Code:
    send(sa, &px, sizeof(px), 0);
    Then I have in the client code:

    Code:
    recv(c, &px, sizeof(px), 0)
    Though whatever I send (just an int) ends up casting a floating point exception to the client. Can anyone see where I'm going wrong, or offer some advice?

    Thanks in advance.
    Last edited by pheeze; 11-10-2009 at 01:18 PM.

  2. #2
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    Quickly this may help.

    Beej's Guide to Network Programming

  3. #3
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    What is "n" in your code?

  4. #4
    Registered User
    Join Date
    Nov 2009
    Posts
    3
    Erm, sorry about that! It ought to be sizeof(px), shouldn't it. I edited that in.

  5. #5
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    I believe there is still a problem or was that the problem?

  6. #6
    Registered User
    Join Date
    Nov 2009
    Posts
    3
    I have realised I'm totally wrong there. Now I have:

    Code:
    //Server side
    int n = 5
    int *value = n;
    int len = sizeof(n);
    send(sa, value, len, 0);
    
    
    //Client side
    int *value;
    int len;
    int n = recv(s, value, len, 0);
    Then a printf for n, but it seems to return nothing but 0. How do I get the actual value out of the recv? I'm not sure what I have is right. I think figuring out that is all I have left to do.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. NEED HELP READING FILE and PRINTING
    By geoffr0 in forum C Programming
    Replies: 4
    Last Post: 04-16-2009, 05:26 PM
  2. memory leak
    By aruna1 in forum C++ Programming
    Replies: 3
    Last Post: 08-17-2008, 10:28 PM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. getting a headache
    By sreetvert83 in forum C++ Programming
    Replies: 41
    Last Post: 09-30-2005, 05:20 AM
  5. Quack! It doesn't work! >.<
    By *Michelle* in forum C++ Programming
    Replies: 8
    Last Post: 03-02-2003, 12:26 AM

Tags for this Thread