Skip to Main Content

Java APIs

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

java.net.BindException: Address already in use: JVM_Bind error

809090Oct 28 2010 — edited Oct 29 2010
Hey,
i have the following problem:
There are 2 classes, TestClient and Mainframe, that are supposed to communicate with eachother using TCP. Both are using Swing components to display text and buttons to tell to send or to connect.
The connecting part seems to work and i get no error what so ever, but when i press send, i.e. tell the program to write something into the stream i get a java.net.BindException: Address already in use: JVM_Bind error.
Still, once i refresh the TestClient class, the message that i sent appears. Also, when im telling it to send, I'm not modifiying or creating the socket im using, I'm just writing in the Stream and the error points to a line of code that has already been execuded, without error.


In Netstat, when the 2 are connected, it says the following:
Lokal: 127.0.0.1:2579 Remote: Toni-PC:21000
Lokal: 127.0.0.1:21000 Remote: Toni-PC:2579

The port 21000 was given by me and the 2579 varies each time. Should there not be only one entry where the lokal and the remote uses the same port?

Here are the 2 Classes. The error line is bold and the executed line is bold underlined.
public class TestClient implements ActionListener
{	
	public Socket socket = null;		
	public static BufferedReader in = null;
	public static PrintWriter out = null;
	public boolean isConnected = false;
	
	public TestClient()
	{
               //here is the GUI
	}	
	public static void main(String[]args) throws IOException
	{		
		//EventQueue here for the GUI		
	}
	@Override
	public void actionPerformed(ActionEvent e) 
	{
		if("empfangen".equals(e.getActionCommand()))
		{
			try 
			{
				if (in.ready()) 
				{
					String s = in.readLine();
					display.setText(s);
				}
			}
			catch (IOException e1) 
			{
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}		
		else if(send.equals(e.getActionCommand()))
		{
			out.print(display.getText());
			out.flush();
			display.setText("");
		}		
	}
}
public class Mainframe extends Thread implements ActionListener 
{
	public static ServerSocket hostServer = null;
	public static Socket socket = null;
	public static BufferedReader in = null;
	public static PrintWriter out = null;
	
	public static void main(String[]args) throws SocketException
	{
		EventQueue.invokeLater(new Runnable() 
		{
			public void run() 
			{
				try 
				{
					Mainframe mnfrm = new Mainframe();
					mnfrm.frmMain.setVisible(true);
					
				} catch (Exception e) 
				{
					e.printStackTrace();
				}
			}
		});
		
	}
	public Mainframe() 
	{
		//GUI here
		    
	}
	public static void cleanUp() 
	{
		//resetting all connections here	            
	}
 	
	public void actionPerformed(ActionEvent e) 
	{
		if("send".equals(e.getActionCommand()))   //using this part creates the error
	        {
			status.setText("sending");
			
	       if (send.getText().length() != 0 && isConnected == true && out != null)
	        	{
	        		out.print(incoming.getText());
	                        out.flush();
	                        incoming.setText("");            
	        	}
	        else
	        {
	        	status.setText("nicht verbunden");
	        }
		}		
		else if("connect".equals(e.getActionCommand()));
		{
			try 
			{	
	             // Try to set up a server if host
	            				
	            *hostServer = new ServerSocket(21000);*   //this line is referenced in the stacktrace
	            
	            hostServer.setSoTimeout(10000);
	            socket = hostServer.accept();	               
	              
	            in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
	            out = new PrintWriter(socket.getOutputStream(), true);
	             
	            isConnected = true;
	            status.setText("verbunden");
	            }
	            // If error, clean up and output an error message
	            catch (IOException x) 
	            {
	            	x.printStackTrace();
	            	status.setText("connection failed");
	            	isConnected = false;
	            	cleanUp();	               
	            }
		}		
	}
}
Thanks for help!

Edited by: 806087 on 28.10.2010 11:37

Edited by: Breasler on 28.10.2010 12:28
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Nov 26 2010
Added on Oct 28 2010
5 comments
4,500 views