Thread: Question concerning Send() and recv()

  1. #1
    Registered User
    Join Date
    Jan 2020
    Posts
    6

    Question concerning Send() and recv()

    Hello, I am currently learning about socket programming in c and have a question about the built-in functions send() and receive().

    Does the first argument in send() Identity the socket from which you send data or the one to which you send the data?

    Likewise, does the first argument in recv() Identity the socket from which you receive the data or the one on which you receive it?

  2. #2
    Registered User
    Join Date
    Jan 2020
    Posts
    6
    Quote Originally Posted by HelpMe12 View Post
    Hello, I am currently learning about socket programming in c and have a question about the built-in functions send() and receive().

    Does the first argument in send() Identity the socket from which you send data or the one to which you send the data?

    Likewise, does the first argument in recv() Identity the socket from which you receive the data or the one on which you receive it?
    I am a bit confused because the man page for send() says:

    The argument sockfd is the file descriptor of the sending socket.

    Other sources say that sockfd is the file descriptor of the socket
    to which the data has been sent

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,556
    It seems to me that the man page you consulted has answered your question.

    Quote Originally Posted by HelpMe12
    Other sources say that sockfd is the file descriptor of the socket
    to which the data has been sent
    Which other sources are these?

    EDIT:
    My bad. Something sounded wrong to me, and I decided to check my own system's man page, and it does state what you observed. However, it also doesn't make sense: if you're going to send to something, you need to specify where to send it to, hence sockfd must designate the socket to send to. You may have spotted a bug in a man page as "sending socket" implies the opposite (if there's a "sending socket", a reasonable interpretation is that that means your program is receiving data from it, not sending data to it).
    Last edited by laserlight; 01-21-2020 at 05:30 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Jan 2020
    Posts
    6
    Quote Originally Posted by laserlight View Post
    It seems to me that the man page you consulted has answered your question.


    Which other sources are these?

    EDIT:
    My bad. Something sounded wrong to me, and I decided to check my own system's man page, and it does state what you observed. However, it also doesn't make sense: if you're going to send to something, you need to specify where to send it to, hence sockfd must designate the socket to send to. You may have spotted a bug in a man page as "sending socket" implies the opposite (if there's a "sending socket", a reasonable interpretation is that that means your program is receiving data from it, not sending data to it).
    Thanks for the quick response. I have changed my opinion though and think that the man page gives the right information.

    I agree with this sentence:
    In the send function the argument sockfd is the file descriptor of the sending socket(not the one to which the data is sent)

    This is how I understand the client-server relation:

    On the client side you create a client socket and connect it to the server socket. You then send data over the client socket to the server and receive data back from the server over the same client socket.

    On the server side you create a server socket for listening to and accepting incoming connections and by accepting a connection you create a new socket on the server side to which the client socket is connected. Over that new socket you send data to the client socket and retreive data back from the client socket over that new socket.



Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Send() and Recv()
    By Rodaxoleaux in forum Networking/Device Communication
    Replies: 0
    Last Post: 12-07-2011, 12:57 PM
  2. Send/Recv
    By brietje698 in forum Networking/Device Communication
    Replies: 13
    Last Post: 12-13-2007, 10:16 PM
  3. send and recv
    By DeadManWalking in forum C Programming
    Replies: 1
    Last Post: 12-04-2005, 09:17 AM
  4. Using recv() and send()
    By Sam_bham in forum C Programming
    Replies: 3
    Last Post: 06-08-2004, 04:36 PM
  5. recv/send
    By Wisefool in forum Networking/Device Communication
    Replies: 8
    Last Post: 10-26-2003, 06:40 PM

Tags for this Thread