Here are the steps in this process: The server is just like any ordinary program running in a computer. Each computer is equipped with some ports.
ServerSocket ss = new ServerSocket(1234); /* 1234 is some arbitrary
port number*/
Socket s = ss.accept();Compare this with a typical C code:
char c; c = getchar();The function waits indefinitely for the user to input a character from the keyboard. The moment the user inputs a character the
getchar
function returns and stored the input character into c.
Socket sock = new Socket("www.isical.ac.in",1234);
/* The client knows the number 1234 */
Socket s = ss.accept();now finishes execution. The client socket is now stored in
s.
Socket s = ss.accept();Typically, this line is inside a loop. Everytime a client is found, its Socket is extracted, and the loop again waits for the next client.
Socket sock = new Socket("www.isical.ac.in",1234);
import java.net.*;
import java.io.*;
public class AServer {
public static void main(String args[]) throws Exception {
ServerSocket ss = new ServerSocket(3452);
System.out.println("Listening...");
Socket s = ss.accept();
System.out.println("Client detected "+s);
InputStream in = s.getInputStream();
OutputStream out = s.getOutputStream();
byte[] buffer = new byte[1000];
in.read(buffer);
System.out.println("Client says:\n"+new String(buffer));
out.write("I can hear you!".getBytes());
out.flush();
}
}
We have already explained the first part of this program. We shall now see
what the bold part does. A Socket is like a rope-way that can carry messages in
either direction. For this we need to extract two Streams from a Socket:
an InputStream for reading what comes from the remote end of the rope, and
an OutputStream to send messages to the remote end. We are looking at the
Server program, so the remote end means the client end. The following
code
byte[] buffer = new byte[1000];
in.read(buffer);
System.out.println("Client says:\n"+new String(buffer));
reads the first 1000 bytes (or whatever is available) from the remote end,
and prints it on the screen. All reading and writing takes place in terns
of bytes. Notice how we change an array of bytes to a String
in order to print it on screen.
Similarly, the following code sends the String "I can hear you!" to the client.
out.write("I can hear you!".getBytes());
out.flush();
Notice the use of the getBytes() function of the String class. It converts
a String into an array of bytes suitable to be sent to an
OutputStream. The out.flush() gurantees that the entrie
String arrives at the other end. Sometimes, to optimise performance the
computer waits for more message to accumulate in a temporary buffer before
sending them to the client. The flush() command forces the computer to
send all the buffered contents to the client.
import java.net.*;
import java.io.*;
public class AClient {
public static void main(String args[]) throws Exception {
System.out.println("Connecting...");
String name = args[0];
int port = Integer.parseInt(args[1]);
Socket s = new Socket(name,port);
InputStream in = s.getInputStream();
OutputStream out = s.getOutputStream();
out.write("Hello!\n".getBytes());
out.flush();
byte[] buffer = new byte[1000];
in.read(buffer);
System.out.println("The remote machine says:\n"+new String(buffer));
}
}