Results 1 to 4 of 4
  1. #1
    Yogesh_P is offline Member
    Join Date
    Mar 2011
    Posts
    10
    Rep Power
    0

    Default Client and Server sockets on same machine

    I need to make 3 processes residing on different machines to communicate with each other. Each machine can talk to one other so each machine should have a ServerSocket to send outgoing messages and an client Socket to receive messages from others. When I try to run ServerSocket it goes in infinite loop as it is waiting for requests to be accepted, hence other parts of my program do not work. Can anyone help me as to how to implement this.

    Here is my code

    Java Code:
    public class Networking
    {
    ServerSocket serverSock;
    Socket clientSock;
    
    public void makeSockets()
    	{
    		try
    		{
    			serverSock = new ServerSocket(Port);
    				Socket sock = serverSock.accept();
    				PrintWriter serverWriter = new PrintWriter(sock.getOutputStream());
    				System.out.println("Is code working here");
    				System.out.println(serverSock.getLocalPort());
    				
    				// Everytime a new request comes it spawns a new worker thread
    				Worker newWorker = new Worker(sock); 
    				serverSock.close();
    			
    		}
    		catch(IOException ex)
    		{
    			System.out.println("Exception on new ServerSocket: " + ex);
    		}
    			
    		try
    		{
    			clientStreamReader = new InputStreamReader(clientSock.getInputStream());
    			clientReader = new BufferedReader(clientStreamReader);
    			clientWriter = new PrintWriter(clientSock.getOutputStream());
    			clientSock.close();
    		}
    		catch(IOException ex)
    		{
    			ex.printStackTrace();
    		}
    		
    	}
    Whether I put serverSock.accept() in while loop or outside it doesnt reaches any of System.out.println() statements.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Yogesh_P View Post
    Whether I put serverSock.accept() in while loop or outside it doesnt reaches any of System.out.println() statements.

    What while loop?

  3. #3
    Yogesh_P is offline Member
    Join Date
    Mar 2011
    Posts
    10
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    What while loop?
    Sorry I edited it in this code, in case I do it like this:

    Java Code:
    serverSock = new ServerSocket(Port);
    while(true)
    {
    				Socket sock = serverSock.accept();
    				PrintWriter serverWriter = new PrintWriter(sock.getOutputStream());
    				System.out.println("Is code working here");
    				System.out.println(serverSock.getLocalPort());
    				
    				// Everytime a new request comes it spawns a new worker thread
    				Worker newWorker = new Worker(sock); 
    				serverSock.close();
    }
    In both cases ServerSocket provides an blocking call. Should I create a seprate thread for that too ?

  4. #4
    lovelesh is offline Senior Member
    Join Date
    Nov 2010
    Location
    Delhi
    Posts
    135
    Blog Entries
    1
    Rep Power
    0

Similar Threads

  1. Replies: 2
    Last Post: 09-24-2010, 12:31 PM
  2. Java Server & C# client communicating through sockets
    By yukijocelyn in forum Networking
    Replies: 0
    Last Post: 09-05-2008, 11:05 PM
  3. searching the local ip server from the WAN machine
    By Amruth Shreyass in forum Networking
    Replies: 0
    Last Post: 08-21-2008, 02:52 PM
  4. An echo server using UDP sockets
    By Java Tip in forum java.net
    Replies: 0
    Last Post: 04-07-2008, 09:09 PM

Posting Permissions

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