What Causes Port Mismatches ?

Started by
9 comments, last by JohnnyCode 9 years, 6 months ago

public class Client {
	
	int port;
	String url;
	Socket socket;
	ObjectInputStream input;
	ObjectOutputStream output=null;
	
	public Client(String address,int po){
		url = address; 
		port = po;
		try {
			socket = new Socket (url,port);// use instead Connect function to create a socket that is tcp connected somewhere 
		} 
		catch (Exception e) {
			System.out.println("Client Ini Crash\n" + e);
		}
	
	}
	public void out(Object obj){
		try{
			//socket = new Socket (url,port);
if (output ==null)
			output = new ObjectOutputStream (socket.getOutputStream());
			output.writeObject(obj);
			//socket.close();
		}
		catch(Exception e){
			System.out.println("Client Crash\n" + e);
		}
	}
}

I modified your code to create output stream object only in case it does not exist over socket, and you should not just create a socket through constructor but rather use TcpClient class to correctly connect to a remote tcp server and recieve a functional connected r/w socket (and test it)

This topic is closed to new replies.

Advertisement