user1679463
user1679463

Reputation: 23

client acting both a as a client and server - JAVA

I have an interesting question. I am trying to establish a peer to peer connection which means a client process acts both as a server and client. Ideally, it should have a client socket (Socket class) and a server socket(Server Socket class). Now I tried to use this concept but it does not work. Please take a look at it:

  public static void main(String argv[]) throws Exception
  {

  Socket clientSocket = null;


  BufferedReader inFromUser = new BufferedReader( new InputStreamReader(System.in));
  DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());



 System.out.println("Enter the server port u want to be assigned to this peer:"); 
 sentence = inFromUser.readLine(); 
 System.out.println("writing current port to client = "+ sentence);
 outToServer.writeBytes("p~"+sentence + "\n" );

 int serverport = Integer.parseInt(sentence);

ServerSocket server = new ServerSocket(serverport);
Socket client;
//client 
System.out.println("enter port no of the server port of an other peer:");
               int msg=Integer.parseInt(inFromUser.readLine());
               clientSocket = new Socket("localhost", msg);
                outToServer = new DataOutputStream(clientSocket.getOutputStream());
               outToServer.writeBytes("hi");

    while(true)
    {
         //server port listens infinitely and spawns new thread
          System.out.println("inside true");
       client = server.accept();
      Thread serverThread = new Thread(new acceptconnection1(client));
        serverThread.start();

    }}}

class acceptconnection1 implements Runnable {
 BufferedReader inFromClient, inn; 
 DataOutputStream ds;
 Socket socket;
  int peersocket;

 String clientSentence;

         int serverport ;

    Socket clientSocket = null;
acceptconnection1 (Socket socket,) throws IOException{

this.socket = socket;

 inn = new BufferedReader (new InputStreamReader(System.in));
      inFromClient =new BufferedReader(new InputStreamReader(socket.getInputStream()));
  ds = new DataOutputStream(socket.getOutputStream());
}

@Override
public void run () {

        String cs,a;
    try {
            System.out.println("waiting for connection ");

             if(( clientSentence = inFromClient.readLine())!=null)
             {

            System.out.println("Message from other peer" + clientSentence);

             }


    } catch (IOException ex) {
        Logger.getLogger(acceptconnection1.class.getName()).log(Level.SEVERE, null, ex);
    }}}

Output: when I create two client processes,

o/p of client1: Enter the server port u want to be assigned to this peer: 1111 writing current port to client = 1111 hi enter port no u want to connect to: 2222 inside true inside true waiting for connection

Enter the server port u want to be assigned to this peer: 2222 writing current port to client = 2222 hi enter port no u want to connect to: 1111 inside true inside true waiting for connection

what happens is both of them wait for connections. how do i solve this?

Upvotes: 0

Views: 2141

Answers (1)

Peter Lawrey
Peter Lawrey

Reputation: 533462

You have a deadlock condition. To result this, create the ServerSocket first so the Socket has something to talk to. Create the Socket which will connect but do nothing until accepted. Then accept connections.

BTW: You don't need to create two connections for traffic to pass both ways. Once a connection has been established, you can use that one connection as client-server, or server-server or what ever.

Upvotes: 1

Related Questions