Categories

Main
Database
Java
Microsoft.NET
Adabas
DB2
Informix
Microsoft SQL Server
MySQL
Oracle
Pervasive.SQL
PostgreSQL
Sybase
Other
ASP
ColdFusion
Crystal Reports
Delphi, C etc
JAVA
Microsoft.NET
Perl and the DBI
PHP
ANSI SQL
Unix Shell Scripts
Visual Basic
XML & XSLT
Corel Paradox
FileMaker
Microsoft Access
Microsoft Excel
Other PC Databases
Applications & Tools
Database Concepts & Design
EJB programming & troubleshooting
EJB design
General J2EE
XML & Web services
Web tier: servlets, JSP, Web frameworks
Performance and scalability
Industry news
TSS feedback
Mobicents Contributors
Mobicents Users
JSLEE Resource Adaptor Types
Planning JavaOne 2008
Sun Tech Days
Other Java conferences
Binary Web Services and XML
Metro and JAXB
GlassFish
GlassFish Plugins
Project jMaki
GlassFish WebTier
Mural
Java Development Tools
Java WS & XML Community News
JAXP
Java SE
6uN Early Access
Java Quick Starter
Java SE Snapshots: Project Feedback
JCK Forum
Feedback and Suggestions
JDK Distros
JDK Open Source
General JSR Discussion
JCP 2.6
JXTA Community Forum
ME Interest
ME Cool Apps
ME General Help
ME Feedback & Suggestions
ME Application Developer Interest
Blu-ray Disc Java
ME Developer Days
Squawk
Mobile Developer Alliance
OpenCable
LWUIT
JavaFX Script Language Discussion
OpenJFX General Discussion
Scene Graph
General Performance Discussion
Your Java Career
NetBeans 6.0
Servlets
JSP
JSF
Portals and Portlets
EJB and Other Java EE Technologies
Distributed Java
Object Relational Mapping
JDBC
Web Services
Swing / AWT / SWT / JFace
JNLP and Web Start
Java Micro Edition
Sockets and Internet Protocols
Threads and Synchronization
Performance
Applets
I/O and Streams
Other Java APIs
Game Development
Java in General (beginner)
Java in General (intermediate)
Java in General (advanced)
Programmer Certification (SCJP)
Developer Certification (SCJD)
Associate Certification (SCJA)
Web Component Certification (SCWCD)
EJB Certification (SCBCD)
Mobile Application Certification (SCMAD)
Architect Certification (SCEA)
Web Services Certification (SCDJWS)
XML Certification
Product and Other Certifications
Mock Exam Errata
Sun Certification Results
Authors' Corral
Book Reviews
Events
Bunkhouse Porch
Teachers' Lounge
Testing
OO, Patterns, UML and Refactoring
IDEs, Version Control and other tools
Ant, Maven and Other Build Tools
Linux / UNIX
Mac OS
HTML and JavaScript
XML and Related Technologies
Agile and Other Processes
General Computing
Security
Groovy
Scala
Other Languages
Struts
Application Frameworks
Other Open Source Projects
BEA/Weblogic
IBM/Websphere
Oracle/OAS
Apache/Tomcat
JBoss
Other Java Products and Servers
JavaRanch
Cattle Drive (java college)
Moderators Only
Trash Can
Jobs Offered
Jobs Wanted
Jobs Discussion
Meaningless Drivel
Programming Diversions
Blatant Advertising
Java Announcements
New To Java
Advanced Java
Java Applets
Networking
Threads and Synchronization
Java 2D
AWT / Swing
SWT / JFace
CLDC and MIDP
CDC and Personal Profile
Sun Java Wireless Toolkit
Enterprise JavaBeans
JavaServer Pages (JSP) and JSTL
Java Servlet
JavaServer Faces
Web Frameworks
Database
XML
Lucene
NetBeans
Eclipse
IntelliJ IDEA
JCreator
Other IDEs
Java Tutorials
Java Tips
Jobs Discussion
Jobs Offered
Jobs Wanted
Professional Certification
Forum Lobby
Java Blogs
Introductions
Reviews / Advertising
Suggestions & Feedback

Resources

Java Database
Linux
Coding
Mobile
Hardware
Software Development
Software Development
iOS,OS X
iOS,OS X
ORACLE
IBM DEVELOPER
IBM DEVELOPER
MSDN
MSDN


Tags

Sockets and Internet Protocols


Java net BindException:Address already in use: JVM_Bind


Hello,I've written a client/server program to start matlab on the server and its working fine and also written another client/server  program to transfer files from client machine to server.Both these programs are working fine when they are executed separetely.Now, I combined these 2 server programs into one and when i try to run the application, it throws me with the following error:Java.net.BindException:Address already in use: JVM_Bind.I guess this is because both the server programs are listening on port 1234.If I change the port in the MatlabRCServer program, I'm able to transfer a file and when I try to start matlab, the client application hangs up.I had to quit forcibly.Any ideas where the code is messed up???Thanks in advance.code:import java.net.*;import java.io.*;public class tcpServer {	public static MatlabRCServer serverrc;	public MyField myf;	//public String uploadfile;	//private javax.swing.JTextField errortext;	public static void main(String args[]) {		  serverrc = new MatlabRCServer();          int port;          ServerSocket server_socket;try {                port = Integer.parseInt(args[0]);          }          catch (Exception e) {               System.out.println("port = 1234 (default)");               port = 1234;          }try {server_socket = new ServerSocket(port);               System.out.println("Server waiting for client on port " + server_socket.getLocalPort());			   // server infinite loop               while(true) {                    Socket socket = server_socket.accept();                    System.out.println("New connection accepted "+socket.getInetAddress()+":"+socket.getPort());try {                          byte[] b = new byte[1024];                         int len=0;                         int bytcount=1024;                         String FileName="iluvchinni.txt"; //writing to a file named ftptest.txt                         FileOutputStream inFile = new FileOutputStream(FileName);                         InputStream is = socket.getInputStream();                         BufferedInputStream in2 = new BufferedInputStream(is,1024);while( (len = in2.read(b,0,1024)) != -1 ) {                              bytcount=bytcount+1024;                              inFile.write(b,0,1024);                         }                         in2.close();                         inFile.close();                          System.out.println("Bytes Writen : "+bytcount);						//errortext.setText("Upload Successful");                    }                    catch(IOException e) {                         System.out.println("Unable to open file" + e);                         return;                    }                    try {                         socket.close();                         System.out.println("Connection closed by client");                    }                    catch (IOException e) {                         System.out.println(e);                    }}}catch (IOException e) {               System.out.println(e);          }     }}The following are the 2 separate server programs:server program to start matlabcode:import java.io.DataInputStream;import java.io.DataOutputStream;import java.io.IOException;import java.net.ServerSocket;import java.net.Socket;class MatlabRCServer extends Thread {	private MatEng engine;	//	int port = 1234;	DataInputStream in;	DataOutputStream out;	Socket MatlabRCSocket;	ServerSocket MatlabRCServerSocket;public MatlabRCServer()	{		System.out.println("server rc started");		try {			MatlabRCServerSocket = new ServerSocket(7777);            } catch (IOException e) {			System.out.println("ServerSocket-Error" + e.getMessage());		}	}public void run ()	{		while (true) {			try {				MatlabRCSocket = MatlabRCServerSocket.accept();				in = new DataInputStream(MatlabRCSocket.getInputStream());				out = new DataOutputStream(MatlabRCSocket.getOutputStream());				engine = new MatEng();				engine.setOutputBuffer(4096);				out.writeUTF("Matlab Engine started");				out.flush();while (true) {					engine.evalString(in.readUTF());					out.writeUTF(engine.getOutput());					out.flush();				}			} catch (IOException e1) {			} finally {				System.out.println("Server: Connection ended");				engine.close();				try{					in.close();					out.close();					MatlabRCSocket.close();				} catch (IOException e2){					System.out.println("Exception in finally in MatlabRCServer");				}			}	 }	}    public static void main (String[] args)	{		MatlabRCServer ser = new MatlabRCServer();	}}server program to send a filecode:import java.net.*;import java.io.*;public class tcpServer {public static void main(String args[]) {int port;          ServerSocket server_socket;try {                port = Integer.parseInt(args[0]);          }          catch (Exception e) {               System.out.println("port = 1234 (default)");               port = 1234;          }try {server_socket = new ServerSocket(port);               System.out.println("Server waiting for client on port " + server_socket.getLocalPort());// server infinite loop               while(true) {                    Socket socket = server_socket.accept();                    System.out.println("New connection accepted "+socket.getInetAddress()+":"+socket.getPort());try {                          byte[] b = new byte[1024];                         int len=0;                         int bytcount=1024;                         String FileName="C://sentfile.txt"; //writing to a file named ftptest.txt                         FileOutputStream inFile = new FileOutputStream(FileName);                         InputStream is = socket.getInputStream();                         BufferedInputStream in2 = new BufferedInputStream(is,1024);while( (len = in2.read(b,0,1024)) != -1 ) {                              bytcount=bytcount+1024;                              inFile.write(b,0,1024);                         }                         in2.close();                         inFile.close();                          System.out.println("Bytes Writen : "+bytcount);                    }                    catch(IOException e) {                         System.out.println("Unable to open file" + e);                         return;                    }                    try {                         socket.close();                         System.out.println("Connection closed by client");                    }                    catch (IOException e) {                         System.out.println(e);                    }}}catch (IOException e) {               System.out.println(e);          }     }}[ February 12, 2004: Message edited by: kimi lynn ]

   
   

 
   
      quote:Originally posted by kimi lynn:I guess this is because both the server programs are listening on port 1234.You are correct.  As for your other problem, I think you posted the same code twice.--------------------"blabbing like a narcissistic fool with a superiority complex" ~ N.A.[How To Ask Questions On JavaRanch]

   
   

   
   
      Joe, sorry as I posted the same code twice.Now, I'm listening on different ports and now the service is waiting for the client to connect.But, Unfortunately when i try to run the matlab on the server, it does'nt respond and the client application also hangs up.As I said in my previous post, If i run alone matlabrcserver program and try to start the matlab on the server, it works fine.BUt, when i run the tcp server which is a mix of both the server programs, the application hangs up.Any help??I edited my first post.Thanks in advance everybody.

   
   

 
   
      I am detecting a bit of confusion about how to use sockets.  Are you trying to set any of your programs to use the same socket as the matlab server?  That would cause the same bind problem you experienced before.  If you are trying to connect to the matlab server, you should use a java.net.Socket.Are you attempting to connect one of your classes to another via a socket?  If that is the case, the "server" should declare a java.net.ServerSocket, which listens on a port and the "client" should use a java.net.Socket to connect to the server's port.Also it may help those of us in the peanut gallery if you would name your classes with more expressive names.  Two of them are named "tcpServer" and while they have small differences, I cannot divine the purpose of those classes from the code.Maybe a look at theCustom Networkingtutorial would help?--------------------"blabbing like a narcissistic fool with a superiority complex" ~ N.A.[How To Ask Questions On JavaRanch]

   
   

   
   
      Joe,let me explain you clearly what exactly am trying to do.At first, I run the matlabserver program and then when i try to run the matlab on the server, it works fine.NOw, I run the file server program and try to send a file from the client.The file is getting through.So,when I test these 2 server programs separately, both are working fine for me.Now, all I want to do is to make "ONE" server program so that either I can start matlab on the server or I can send files from client to server using my GUI.Here are the 2 server programs:Matlab Server Programcode:import java.io.DataInputStream;import java.io.DataOutputStream;import java.io.IOException;import java.net.ServerSocket;import java.net.Socket;class MatlabRCServer extends Thread {	private MatEng engine;	//	int port = 1234;	DataInputStream in;	DataOutputStream out;	Socket MatlabRCSocket;	ServerSocket MatlabRCServerSocket;public MatlabRCServer()	{//		System.out.println("server rc started");		try {			MatlabRCServerSocket = new ServerSocket(7777);            } catch (IOException e) {			System.out.println("ServerSocket-Error" + e.getMessage());		}	}public void run ()	{		while (true) {			System.out.println("server is running");			try {				MatlabRCSocket = MatlabRCServerSocket.accept();				in = new DataInputStream(MatlabRCSocket.getInputStream());				out = new DataOutputStream(MatlabRCSocket.getOutputStream());				engine = new MatEng();				engine.setOutputBuffer(4096);				out.writeUTF("Matlab Engine started");				out.flush();while (true) {					engine.evalString(in.readUTF());					out.writeUTF(engine.getOutput());					out.flush();				}			} catch (IOException e1) {			} finally {				System.out.println("Server: Connection ended");				engine.close();				try{					in.close();					out.close();					MatlabRCSocket.close();				} catch (IOException e2){					System.out.println("Exception in finally in MatlabRCServer");				}			}	 }	}    public static void main (String[] args)	{		MatlabRCServer ser = new MatlabRCServer();		ser.run();	}}File Transfer Server Programcode:import java.net.*;import java.io.*;public class tcpServer {public static void main(String args[]) {int port;          ServerSocket server_socket;try {                port = Integer.parseInt(args[0]);          }          catch (Exception e) {               System.out.println("port = 1234 (default)");               port = 1234;          }try {server_socket = new ServerSocket(port);               System.out.println("Server waiting for client on port " + server_socket.getLocalPort());// server infinite loop               while(true) {                    Socket socket = server_socket.accept();                    System.out.println("New connection accepted "+socket.getInetAddress()+":"+socket.getPort());try {                          byte[] b = new byte[1024];                         int len=0;                         int bytcount=1024;                         String FileName="C://iluvchinni.txt"; //writing to a file named ftptest.txt                         FileOutputStream inFile = new FileOutputStream(FileName);                         InputStream is = socket.getInputStream();                         BufferedInputStream in2 = new BufferedInputStream(is,1024);while( (len = in2.read(b,0,1024)) != -1 ) {                              bytcount=bytcount+1024;                              inFile.write(b,0,1024);                         }                         in2.close();                         inFile.close();                          System.out.println("Bytes Writen : "+bytcount);                    }                    catch(IOException e) {                         System.out.println("Unable to open file" + e);                         return;                    }                    try {                         socket.close();                         System.out.println("Connection closed by client");                    }                    catch (IOException e) {                         System.out.println(e);                    }}}catch (IOException e) {               System.out.println(e);          }     }}and heres my client code:code:public void tcpClient() {int port = 1234;//String server = "10.172.30.33"; // Different PCString server = "131.246.110.43";Socket socket = null;int ERROR = 1;//connect to server			  try {				   socket = new Socket(server, port);				   System.out.println("Connected with server "+socket.getInetAddress()+":"+socket.getPort());			  }			  catch (UnknownHostException e) {				   errortext.setText("Could not connect with server");				   System.out.println(e);				   System.exit(ERROR);			  }			  catch (IOException e) {				   System.out.println(e);				   System.exit(ERROR);			  }try {				   byte[] buf = new byte[1024];				   OutputStream os = socket.getOutputStream();				   BufferedOutputStream out = new BufferedOutputStream(os,1024);				   String file = uploadfile; // the file being read.				   FileInputStream in = new FileInputStream(file);				   int i; 				   int bytecount=1024;while ((i = in.read(buf,0,1024)) != -1) {						bytecount=bytecount+1024; 						out.write(buf,0,1024);						out.flush();				   } 				   in.close();				   out.close();				   System.out.println("Bytes Sent :"+bytecount);				errortext.setText("Sending File");			  }			  catch (IOException e) {				   System.out.println(e);			  }try {				   socket.close();			  }			  catch (IOException e) {				   System.out.println(e);			  }	}}I'd be glad if you/somebody can help me.Thanks once again.

   
   

 
   
      Pardon me if I seem dense, but you are still leaving out some details.  Where is the client that corresponds to MatlabRCServer?  Also, you are showing us the code that works and not your "ONE" server that does not work, right?--------------------"blabbing like a narcissistic fool with a superiority complex" ~ N.A.[How To Ask Questions On JavaRanch]


Related Links

Web Service to Get a Stock Quote?
Error while downloading Image from web site
how to develope tcp protocol in socket connetion
Get the temp file(css and image) from the response
servlets in myeclipse
FTP
Doubt abt Socket
How to communicate between one server and multiple clients
Regarding Idempotent Request
Create post request with apache HttpClient
Custom Proxy Server Problem
setHeader()
UrlConnections versus HttpURLConnection versus Apaches HttpClient
JAXB and TCP/IP
Question on redirects and multiple hops
Send by HttpClient, receive by IE ?