0
Java sockets
So I have two simple questions about server programming in Java. If I want to run client and server on different system what should I do? And what range of numbers can I set the port number?
1 Answer
+ 1
Essentially, your goal is to connect the client to the server and make an exchange. Here I will talk about the simplest form,
First, you would create a server which would listen on a port X and has the INET address (IPV4) Y;
Your client would attempt to connect (by creating a new Socket object), with the parameters of the portt and INET address of the server. From there you can create IOstreams and using those streams - transfer data.
A client would look like this:
Socket s = new Socket(y, x);
DataOutputStream out = s.getOutputStream();
Scanner sc = new Scanner(System.in);
out.writeUTF(sc.nextLine());
out.close();
s.close();
and the server:
Socket recv = new Socket (y, x)
recv.setKeepalive(5);
//How long should the server listen for? In this case 5 seconds
String in = recv.getInputStream().readUTF();
recv.close();
Ports go from 0 to 65535.