Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Problems with sockets on simple server and client programs.

  1. #1
    Junior Member
    Join Date
    May 2013
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Problems with sockets on simple server and client programs.

    I set up a simple text message server and client using sockets. When I am have both programs running and connected then close the client without inputting "TERMINATE" I can't connect another client. This is the error stack I'm getting when I close the client.

    java.net.BindException: Address already in use: JVM_Bind
    at java.net.DualStackPlainSocketImpl.bind0(Native Method)
    at java.net.DualStackPlainSocketImpl.socketBind(Unkno wn Source)
    at java.net.AbstractPlainSocketImpl.bind(Unknown Source)
    at java.net.PlainSocketImpl.bind(Unknown Source)
    at java.net.ServerSocket.bind(Unknown Source)
    at java.net.ServerSocket.<init>(Unknown Source)
    at java.net.ServerSocket.<init>(Unknown Source)


    I get this error when I have my server and client programs open. I then close the client window, and this error is when a new ServerSocket is created when the runServer() is called in the closeConnection() method is called. When this happens my connection still has a Socket value. How can I properly handle a client termination so another client can connect.

    Another question dealing with threading. At this point I cannot connect more than one client at a time. I know I need to create a thread that handles connecting clients, so I can have multiple clients communicating with each other. I am not sure how to implement this.


    Here is my server class.

    public class Server extends JFrame {
     
    	private JTextField enterField;
    	private JTextArea displayArea;
    	private ObjectOutputStream output;
    	private ObjectInputStream input;
    	private ServerSocket server;
    	private Socket connection;
    	private int counter = 1;
     
    	public Server() {
    		super ("Server");
    		enterField = new JTextField();
    		enterField.setEditable(true);
    		enterField.addActionListener(new ActionListener() {
     
    			@Override
    			public void actionPerformed(ActionEvent event) {
    				sendData(event.getActionCommand());
    				enterField.setText("");
     
    			}
    		});
     
    		add(enterField, BorderLayout.NORTH);
    		displayArea = new JTextArea();
    		add (new JScrollPane(displayArea));
    		setSize(300,150);
    		setLocation(500,500);
    		setVisible(true);
    	}
     
     
     
    	public void runServer() {
    		try {
    			server = new ServerSocket(50499, 100);
    			if(!server.isClosed()){
    			displayMessage("\n Listening on Port: " + server.getLocalPort() + "\n");
     
    			}
    			while (true) {
    				try {
    					waitForCommunication();
    					getStreams();
    					processConnection();
    				} catch (EOFException eofException) {
    					displayMessage("\n Server terminated connection ");
    				} finally {
    					closeConnection();
    					++counter;
    				}
    			}
    		} catch (IOException ioException) {
    			ioException.printStackTrace();
    		}
    	}
     
    	private void closeConnection() {
    		displayMessage("\nTerminating connection\n");
    		setTextFieldEditable(false);
    		try {
    			output.close();
    			input.close();
    			connection.close();
    		} catch (IOException ioException) {
    			ioException.printStackTrace();
    		}
    		runServer();
     
    	}
     
    	private void displayMessage(final String string) {
    		SwingUtilities.invokeLater(new Runnable(){
     
    			@Override
    			public void run() {
    				displayArea.append(string);
    			}
     
    		});
     
    	}
     
    	private void processConnection() throws IOException {
    		String message = "Connection Sucessful";
    		sendData(message);
     
    		setTextFieldEditable(true);
     
    		do {
    			try {
    				message = (String) input.readObject();
    				displayMessage("\n" + message);
    			} catch (ClassNotFoundException classNotFoundException) {
    				displayMessage("\nUnknown object type recieved");
    			}
    		} while (!message.endsWith(">>> TERMINATE"));
    	}
     
    	private void setTextFieldEditable(final boolean editable) {
    		SwingUtilities.invokeLater(new Runnable(){
     
    			@Override
    			public void run() {
    				enterField.setEditable(editable);
     
    			}
     
    		});
     
    	}
     
    	private void getStreams() throws IOException {
    		output = new ObjectOutputStream(connection.getOutputStream());
    		output.flush();
     
    		input = new ObjectInputStream(connection.getInputStream());
     
    		displayMessage("\nGOt I/O stream \n");
     
    	}
     
    	private void waitForCommunication() throws IOException {
    		displayMessage("Waiting for cennection \n");
    		connection = (server.accept());
    		displayMessage("Connection" + counter + " received from: "
    				+ connection.getInetAddress().getHostName());
    	}
     
    	private void sendData(String message) {
    		try {
    			output.writeObject("SERVER>>> " + message);
    			output.flush();
    			displayMessage("\nSERVER>>> " + message);
    		} catch (IOException ioException){
    			displayArea.append("\nError Writing Object");
    		}
    	}
    }


  2. #2
    Member angstrem's Avatar
    Join Date
    Mar 2013
    Location
    Ukraine
    Posts
    200
    My Mood
    Happy
    Thanks
    9
    Thanked 31 Times in 29 Posts

    Default Re: Problems with sockets on simple server and client programs.

    This exception is thrown, when you try to bind a ServerSocket to a port, on which another application (perhaps, another ServerSocket is bonded). Here, when you close connection, your server continue to work. Hence, if you try opening another ServerSocket at the same port, you get an exception. I suggest you to close client connection at the processConnection() method (i.e., at the end of the processing). The server should be alive all the time.

  3. #3
    Junior Member
    Join Date
    May 2013
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Problems with sockets on simple server and client programs.

    I tried setting the running the closeConnection() method and setting the ServerSocket variable to null. After that I still can't create a new ServerSocket.

    Edit: Fixed I was setting the ServerSocket to null, not the Socket. Once I set the socket to null it was fixed.
    Could anyone help me with setting up threads so I can have multiple clients?
    Last edited by gongshow20; May 24th, 2013 at 09:33 PM. Reason: Fixed

Similar Threads

  1. my encrypting simple client to server program unable to get the key from client
    By Paytheprice in forum What's Wrong With My Code?
    Replies: 11
    Last Post: February 3rd, 2013, 07:15 AM
  2. Problems With Client/Server Operations
    By bgroenks96 in forum Java Theory & Questions
    Replies: 2
    Last Post: July 26th, 2011, 03:48 PM
  3. sending images client/server sockets
    By devett in forum Java Networking
    Replies: 7
    Last Post: June 7th, 2011, 08:38 AM
  4. Replies: 1
    Last Post: March 23rd, 2010, 02:29 AM
  5. Replies: 0
    Last Post: December 12th, 2009, 10:09 PM

Tags for this Thread