Results 1 to 10 of 10
  1. #1
    Rewry is offline Member
    Join Date
    Dec 2011
    Posts
    19
    Rep Power
    0

    Default Are 2 Sockets Required?

    I am currently creating a server and client programs which both can send requests to each other. Therefore, the Server may ask for some
    information from the client, and the client can ask for information from the server at the same time. Thus asking and responding may happen concurrently on different threads.

    For me to program this, do I require 2 sockets? Note I am not asking whether a socket is bidirectional! A socket is bidrectional. What I
    am asking is that for a server and client to send requests and respond to requests at the same time, is it required to use 2 sockets, or is there a way where you can just use one?

    Thanks in advanced

    Rewry

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: Are 2 Sockets Required?

    There are two streams for a socket that should allow communiations both ways with the same socket connection.
    Read from the input and write to the output.

  3. #3
    Rewry is offline Member
    Join Date
    Dec 2011
    Posts
    19
    Rep Power
    0

    Default Re: Are 2 Sockets Required?

    How can It be done? I know that a socket is bidirectional (it has two streams) The server and client have 2 functionality. Let me try and illustrate the cases :

    Funcntion 1: When Server sends a request to client and client responds:

    Server --> client
    Server <-- client



    Function 2: When client sends a request to server and server responds:

    Server <-- client
    Server -> client

    Now these functions can happen at the same time

    Do I need 2 sockets for this?

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: Are 2 Sockets Required?

    When Server sends a request to client
    This is backwards to me. Servers wait for connections from clients and respond to them. There is no way for a server to connect to a client because a client is not waiting for a connection with a ServerSocket.

    If a client connects to a server, it sends data to the server on the output stream and reads on the input stream.
    When a server gets a connection from a client, it reads the clients output on the inputstream and sends data to the client on the socket's outputstream.
    I think that the client can send data to the server at the same time as the server is sending data to the client.
    You would only need one socket.

  5. #5
    Rewry is offline Member
    Join Date
    Dec 2011
    Posts
    19
    Rep Power
    0

    Default Re: Are 2 Sockets Required?

    Quote Originally Posted by Norm;249192
    I think that the client can send data to the server at the [B
    same time[/B] as the server is sending data to the client.
    You would only need one socket.
    Yes you are correct in that regard, but what if the server would like to send data which the client requested AND send a notification to the client at the same time?

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: Are 2 Sockets Required?

    Can the two messages be sent one after the other on the same stream
    or do you think that they must be sent at the same time on separate streams?
    What would be the advantage of sending them at the same time?

  7. #7
    Rewry is offline Member
    Join Date
    Dec 2011
    Posts
    19
    Rep Power
    0

    Default Re: Are 2 Sockets Required?

    The advantage is trying to achieve (or at least get closer to) better real time processing, especially in critical systems, where notifications from a server cannot wait for the client's request to be handled before being sent. Also the fact that your separating the streams, for catering requests (sending requested data) and sending requests (asking for required data), makes it easier for you (the programmer) to work with them.

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: Are 2 Sockets Required?

    If your requirements are that a second response needs to be sent while the first one is being sent, then you would need another parallel connection to send it.
    Would an example of that be when the server is returning a hugh file and then needs to tell the client something immediately in the midst of sending the hugh file and can not wait for the sending of the hugh file to complete?
    If that is the case the client would need to have a ServerSocket that the "server" can connect to.

  9. #9
    Rewry is offline Member
    Join Date
    Dec 2011
    Posts
    19
    Rep Power
    0

    Default Re: Are 2 Sockets Required?

    Yes Exactly! I am not sure, but you could design the server having two ServerSockets where the client connects to both of them, rather than having a ServerSocket within the client.

    Thanks for your reply =)

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: Are 2 Sockets Required?

    It depends on your application's requirements.
    The client can make multiple connections to the same port on the server if needed.
    Or have different priority ports. Some for longer messages and some for shorter.

Similar Threads

  1. Sockets
    By Maya in forum Networking
    Replies: 6
    Last Post: 04-20-2011, 12:52 AM
  2. Sockets
    By PhQ in forum New To Java
    Replies: 1
    Last Post: 05-04-2010, 04:36 AM
  3. Sockets NIO
    By aamp in forum New To Java
    Replies: 3
    Last Post: 01-15-2009, 11:56 AM
  4. Sockets
    By Zosden in forum Networking
    Replies: 16
    Last Post: 05-27-2008, 05:55 PM
  5. Help with Sockets
    By Eric in forum Networking
    Replies: 3
    Last Post: 12-01-2007, 09:09 PM

Tags for this Thread

Posting Permissions

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