I've wrote these two classes, one for client and the other for server. When I run both of them I get the following error:
java.net.BindException: Address already in use: JVM_Bind...
What is the problem? Also I use TCPview software and there were just two java.exe
that use the same port. These two java.exe
processes belong to the apps.
Here is the code:
Server Code
/**
*
* @author casinoroyal
*/
public class server {
public static ServerSocket socket1;
public static void main(String[] args) {
try {
socket1 = new ServerSocket(1254);
String request="";
Socket mylink=socket1.accept();
System.out.println("server feels=====");
DataInputStream input= new DataInputStream(mylink.getInputStream());
DataOutputStream output=new DataOutputStream(mylink.getOutputStream());
Scanner chat=new Scanner(System.in);
while(!request.equals("QUIT")){
request=input.readUTF();
output.writeUTF(chat.next());
}
socket1.close();
} catch (IOException ex) {
System.out.println(ex);
}
}
}
Client Code
package javaapplication9;
import java.net.*;
import java.io.*;
import java.util.*;
public class client {
//main
public static void main(String[] args) {
System.out.println("client want to be connected");
try {
Socket mysock = new Socket(InetAddress.getLocalHost(),1254);
System.out.println("client has been connected");
DataInputStream input = new DataInputStream(mysock.getInputStream());
DataOutputStream output = new DataOutputStream(mysock.getOutputStream());
String reque="";
Scanner scan1=new Scanner(System.in);
String sendmsg=scan1.next();
while(!reque.equals("QUIT")){
output.writeUTF (sendmsg);
reque=input.readUTF();
}
mysock.close();
} catch (IOException ex) {
System.out.println("client rejected"+ex);
}
}
}