I am playing around with an object server , and have run onto an issue with the port
My client sends information on port 9999.
My server listens to port 9999.
However, whenever I receive an incoming connection,
System.out.println("" + socket.getRemoteSocketAddress() );
gives me an incrementing port number !
/127.0.0.1:56304
You said: Moo
/127.0.0.1:56305
You said: Baa
/127.0.0.1:56306
You said: Quack
Do you know what is causing this, and how to fix it ?
***
Client
***
public class Client {
int port;
String url;
Socket socket;
ObjectInputStream input;
ObjectOutputStream output;
public Client(String address,int po){url = address; port = po;}
public void out(Object obj){
try{
socket = new Socket (url,port);
output = new ObjectOutputStream (socket.getOutputStream());
output.writeObject(obj);
socket.close();
}
catch(Exception e){
System.out.println("Client Crash\n" + e);
}
}
}
***
Server
***
public class Server implements Runnable {
int port;
ServerSocket ssocket;
Socket socket ;
ObjectInputStream input;
Object dump;
TestObj to;
public Server(int por){port = por;}
@Override
public void run() {
try{
ssocket = new ServerSocket(port);
while (true){
socket = ssocket.accept();
System.out.println("" + socket.getRemoteSocketAddress() );
input = new ObjectInputStream(socket.getInputStream());
dump = input.readObject() ;
socket.close();
to = (TestObj) dump;
to.say();
}
}
catch (Exception e){
System.out.println("Server Crash\n" + e);
}
}
}