Results 1 to 2 of 2

Thread: Server Socket

  1. #1
    Moncleared is offline Member
    Join Date
    Jan 2009
    Posts
    92
    Rep Power
    0

    Default Server Socket

    Hey guys, I'm trying to create a pretty simple Server/Client application that communicates back and forth. My problem is that as multiple clients join, the server only responds to the latest client connected, regardless of which client asks for the response. For example:

    Server Starts
    Client 1 connects
    Client 1 asks for Response
    Server Responds to client 1
    Client 2 connects
    Client 1 asks for Responses
    Server Responds to client 2

    Here is the code

    Java Code:
    import java.net.*;
    import java.io.*;
    
    public class mainServer {
    	public static void main(String[] args) throws IOException {
    		ServerSocket serverSocket = null;
    		boolean listening = true;
    		int port = 5555;
    		
    		try {
    			serverSocket = new ServerSocket(port);
    			System.out.println("Server started...Running Mode");
    		} catch (IOException e) {
    			System.err.println("Could not listen on port: " + port);
    			System.exit(1);
    		}
    		while(listening){
    			new clientConnectedThread( serverSocket.accept() ).start();
    		}
    		serverSocket.close();
    	}
    }
    
    class clientConnectedThread extends Thread {
    	private Socket clientSocket = null;
    	private static PrintWriter out = null;
    
    	public  clientConnectedThread(Socket socket){
    		super("clientConnectedThread");
    		clientSocket = socket;
    	}
    	public void run() {
    		try {
    		BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
    		out = new PrintWriter(clientSocket.getOutputStream(), true);
    
    		String inputLine;
    		boolean Listening = true;
    		out.println("Welcome to the Server");
    		this.setName(in.readLine());
    		System.out.println("Client: successfully connected...");
    
    		while(Listening){
    			inputLine = null;
    			inputLine = in.readLine();
    			if(inputLine.equals("talkback")){
    				out.println("Message Recieved");
    			}
    			if(inputLine.equalsIgnoreCase("exit"))
    				break;
    		}
    		out.close();
    		in.close();
    		clientSocket.close();
    		}
    		catch (IOException e) {
    			e.printStackTrace();
    		}
    	}
    }
    This has me baffled because the way i understand the code is that when I create this new thread, a specific outputStream() is created for this particular thread and it should be bound to the client that opened the socket. However, clearly I am wrong and mis-understand this. If anyone could give me a better understanding on how the relationship works between this Thread and the client that opened the socket (which ultimately spawned the Thread), please let me know.

    Any help is appreciated...Thanks
    Last edited by Moncleared; 09-05-2009 at 07:05 AM.

  2. #2
    Moncleared is offline Member
    Join Date
    Jan 2009
    Posts
    92
    Rep Power
    0

    Default

    Please forgive my stupidity. Its 1AM and I've been drinking.

    The above problem was solved.

    Keyword in the code to look for....

    "static"....

    FML

Similar Threads

  1. Server socket - send image to client
    By Hinty in forum Networking
    Replies: 2
    Last Post: 03-14-2009, 08:39 AM
  2. Replies: 1
    Last Post: 02-10-2009, 10:16 AM
  3. Client Server socket problem - help needed
    By kellaw in forum Threads and Synchronization
    Replies: 6
    Last Post: 10-03-2008, 07:49 PM
  4. (Server)Socket application deploying
    By mdinant in forum Networking
    Replies: 0
    Last Post: 04-09-2008, 06:10 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
  •