As you guys might know a server program is one that accepts requests from various clients. A client program connects to server and requests or asks it to perform task for it...
An intermediary between a server and a client is through a socket. Think of socket as an electrical socket in your house... eletrical appliances are plugged in to the socket. For example, A phone line is plugged into a similar socket in order to communicate to person who is on another phone some where else and his phone (in a similar way) is connected to a socket as well.
Both the persons on the phone need not worry about the network connections and the way the data flows through the network... the simple server client program works in a similar way. JAVA makes it look similar to transferring data through files as in File I/O programs... Only an additional socket needs to be created...
Let me demonstrate a basic client program...
First we begin with import the 2 mandatory packages that my program requires -
1. java.io.*;
2. java.net.*;
io package is imported so that I can read and write stuff through network just as I do it in case of file reading and file writing. The net package is imported so that I can create a socket. Socket class is present in net package...
Next i'm gonna create a class MyClient with the main method...(this is easy)
The program may throw some IO Exception and therefore we write "throws IOException" along with main method definition.
The program goes something like this...
import java.io.*;
import java.net.*;
public MyClient{
public static void main(String args[]) throws IOException
{
....
....
....
}
}
Now i'm gonna write some stuff in it....
I'm gonna create a socket now using the Socket class...
Socket s = new Socket("localhost",9999);
Socket constructor accepts Internet address or hostname and port as parameters... I assume that both server and client are on the same box (computer) and therefore I used "localhost" which represents the loopback address 127.0.0.1 and I chose 9999 as port.
Ok... we are done with the hardest part... lolz
Now as we all know... be it files or network everything goes through streams...
So now we create a stream... Since it is through network I'd prefer creating a buffered stream...
BufferedOutputStream bos = new BufferedOutputStream(s.getOutputStream());
Here s is the socket as you know it... the stream is created for the socket and therefore we get the output stream for the socket.
Now I create the data to be passed... lets say a "hi" to the server...
String str = "hi server... wassup?? $";
I put a "$" character at the end... so the server reads data till "$" appears... (server should know when to stop reading stuff :P)
Now, I write stuff to buffered output stream created for the socket using write()
for(int i=0 ; i
I write one character at a time... therefore I put it in a for loop which iterates for the number of characters in the string and so I use length() method to find no. of characters for string.
Since I'm writing one character at time and as string a set of characters I use charAt() method for get character at that particular position...
Thats it we're done...
Just to make sure you know when your data writing ended... i'll put a display statement...
System.out.println("Data writing complete...");
I make sure that I close my socket and bos otherwise the second time I run the program I'll get an exception... I'll have to change the port to something else...
Therefore,
bos.close;
s.close;
Thats it... the complete program goes like this...
import java.io.*;
import java.net.*;
public MyClient{
public static void main(String args[]) throws IOException
{
Socket s = new Socket("localhost",9999);
BufferedOutputStream bos = new BufferedOutputStream(s.getOutputStream());
String str = "hi server... wassup?? $";
for(int i=0 ; i
System.out.println("Data writing complete...");
bos.close;
s.close;
}
}
Now the server program...
Same as in client we import io , net packages and create a MyServer class with main method...
import java.io.*;
import java.net.*;
public MyServer{
public static void main(String args[]) throws IOException
{
....
....
....
}
}
To give an effect of to separate programs interacting I created the server program with main method. But as we know... we cannot run two main methods in the same workspace at the same time...
Therefore , I create a new workspace and launch a new instance of eclipse... We'll I used eclipse which an IDE (Integrated Development Environment) for creating and running java programs.
You can use any other IDE or if you are running programs with java compiler and have been using notepad for writing java programs... launch another dos prompt... that will give the effect of two separate
programs interacting...
Now in main method , I create a Server Socket using ServerSocket class...
ServerSocket ss = new ServerSocket(9999);
We need to give only port... since we are the server.
Now we accept connection from the user... a socket captures connections accepted by the server socket....
Socket s = ss.accept();
Then we create to recieve data through Buffered input stream...
BufferedInputStream bis = new BufferedInputStream(s.getInputStream); // same as in client program
We get the data in integer form one byte at a time... lets declare an integer variable...
int c;
Then we read one byte by read() method till we have reached "$" symbol...and display it.
while((c=bis.read())!='$')
System.out.print((char)c);
We have to type cast variable c to char or else we'll have number displaying on screen.
We write a display to show that we've completed reading...
System.out.println("Data reading complete...");
Then we close the sockets ...
s.close();
ss.close();
Thats our server program....
The whole program goes like this...
import java.io.*;
import java.net.*;
public MyServer{
public static void main(String args[]) throws IOException
{
ServerSocket ss = new ServerSocket(9999);
Socket s = ss.accept();
BufferedInputStream bis = new BufferedInputStream(s.getInputStream);
int c;
while((c=bis.read())!='$')
System.out.print((char)c);
System.out.println("Data reading complete...");
s.close();
ss.close();
}
}
Run the server program first...then the client.
If you run the client first you'll get an exception...
There you go... a server-client program :)
Cheers :)