CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Dec 2004
    Location
    DeathValley
    Posts
    184

    Can a server connect to two clients on the same port?

    Can a server program connect to two clients at the same port and exchange different data depending on the client?

  2. #2
    Join Date
    Mar 2005
    Location
    Canada Alberta
    Posts
    80

    Re: Can a server connect to two clients on the same port?

    I believe this is where multithreading comes in handy.
    In C, you merely shoot yourself in the foot.

    In C++, you accidentally create a dozen instances of yourself and shoot them all in the foot. Providing emergency medical care is impossible, because you can't tell which are bitwise copies and which are just pointing at others and saying, "That's me, over there."

  3. #3
    Join Date
    Mar 2001
    Posts
    2,529

    Re: Can a server connect to two clients on the same port?

    Technically, this should not really happen.

    You have to look at the life cycle of a socket on a socket server.

    1. binds the server to a socket. (ip and port.)
    2. set that socket to listen with listen.
    3. accept an incoming connection.

    Accept returns a new socket for communication each time. While the
    server socket is on a particular port and so on, the communications
    actually occurs on a seperate socket that is returned by accept. This
    also happens to be the socket that is already connected to the client
    socket. While yes the server address is the same for both clients, there
    are actually 2 sockets involved on the server. So while it is said that
    we are connecting to this server at this IP on this particular port, there is
    a little shuffle that is occurring to allow 2 clients to use the same server on the
    *same port*.

    HTH,

    ahoodin

    PS dont forget to rate.

    Oh yeah, and a server accepts a connection, it really doesn't connect to a client. a client connect()s to a server.
    Last edited by ahoodin; March 17th, 2005 at 02:27 PM.

  4. #4
    Join Date
    Mar 2005
    Location
    Canada Alberta
    Posts
    80

    Re: Can a server connect to two clients on the same port?

    Quote Originally Posted by ahoodin
    Oh yeah, and a server accepts a connection, it really doesn't connect to a client. a client connect()s to a server.
    This is why we have servre and client not just one thing which could have been called a "user"

    so... what is a socket? I can't realy figure this one out.
    In C, you merely shoot yourself in the foot.

    In C++, you accidentally create a dozen instances of yourself and shoot them all in the foot. Providing emergency medical care is impossible, because you can't tell which are bitwise copies and which are just pointing at others and saying, "That's me, over there."

  5. #5
    Join Date
    Feb 2002
    Posts
    4,640

    Re: Can a server connect to two clients on the same port?

    A "socket" is kinda like a channel on the machine. One machine can be used to serve many different applciations. Sockets are a way to allow that to happen.

    One analogy I like is that of an apartment building. The aparment building has one postal address, but many mailboxes. Sockets are like the mailboxes. If I want to send a message to the apartment manager, then I send it to his mailbox. On the other hand, if I also want to send a message to my friend who lives in the same building, then I send my message to a different mailbox.

    Viggy

  6. #6
    Join Date
    Dec 2004
    Location
    DeathValley
    Posts
    184

    Re: Can a server connect to two clients on the same port?

    Well I never expected such a response, but I am glad. So lemme brief yall with the actual problem. I have an application which tracks the motion of a moving body and tries to display it path graphically. Its the client that recieves the co-ordinates from a server and using these co-ordinates, it plots a pixel in the location corresponding to those co-ordinates (of course, after proper handling of the mapping system is done).

    The reason I need to have two clients connect to the same server is that I need to have one client to just keep track of the identity every new body detected, and the other client is used to do the plotting.

    The first client just accepts the Body Id and adds to a database, while the other clients uses this Id to request the server to continuously send co-ordinates for plotting.

    The main goal is to have a single server, the same port thing can be ignored.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured