The Server is a piece of program that controls every operation in the server side.
Java’s abstraction over the socket API is to use a server socket object that automatically listens, then creates a different socket on accept. Java sockets have input streams and output streams built in, which makes programming rather pleasant.
Implementing a server consists of six basic steps as follows:
- Create a ServerSocket object.
- Create a Socket object from the ServerSocket.
- Create an input stream to read input from the client.
- Create an output stream that can be used to send information back to the client.
- Close the Socket when done.
Following is the psuedo code for creating server for LAN based examination.
public class Server {
public static void main(String[] args){
try{
int port=5555;
ServerSocket ss=new ServerSocket(5555);
System.out.println("Server is created...");
while(true)
{
Socket s=ss.accept();//establishes connection
System.out.println("Network is established between client and server...");
String u,pass;
int ID;
DataInputStream in = new DataInputStream(s.getInputStream());
DataOutputStream out = new DataOutputStream(s.getOutputStream());
u = in.readUTF();
pass=in.readUTF();
Database db = new Database();
System.out.println("up"+u+" "+pass);
ID=db.isvaliduser(u,pass);
if( ID!=0)
{
System.out.println("in if");
out.writeUTF("START");
ServerWorker worker=new ServerWorker(s,db,ID);
System.out.println("going into worker");
worker.start();
}else
{
System.out.println("in else");
// errorPage();
out.writeUTF("ABORT");
out.flush();
db.con.close();
in.close();
out.close();
s.close();
}
}
//System.out.println("Server diconnected successfully...");
}catch(Exception e){System.out.println(e);}
}
}
1. Create a ServerSocket object
With a server, you passively sit and wait for someone to come to you. So, creation requires only a port number, not a host.
With a server, you passively sit and wait for someone to come to you. So, creation requires only a port number, not a host.
new ServerSocket(portNumber);
On Unix, if you are a nonprivileged user, this port number must be greater than 1023 (lower numbers are reserved) and should be greater than 5000 (numbers from 1024 to 5000 are more likely to already be in use). In addition, you should check /etc/services to make sure your selected port doesn't conflict with other services running on the same port number. If you try to listen on a socket that is already in use, an IOException will be thrown.
On Unix, if you are a nonprivileged user, this port number must be greater than 1023 (lower numbers are reserved) and should be greater than 5000 (numbers from 1024 to 5000 are more likely to already be in use). In addition, you should check /etc/services to make sure your selected port doesn't conflict with other services running on the same port number. If you try to listen on a socket that is already in use, an IOException will be thrown.
2. Create a Socket object from the ServerSocket
Many servers allow multiple connections, continuing to accept connections until some termination condition is reached. The ServerSocket accept method blocks until a connection is established, then returns a normal Socket object.
Many servers allow multiple connections, continuing to accept connections until some termination condition is reached. The ServerSocket accept method blocks until a connection is established, then returns a normal Socket object.
while(some condition)
{
Socket s=ss.accept();
deSomethingwith(s);
}
We have to pass this socket to a separate thread to create the input/output streams. It efficiently handles multiple clients at once: When a client connects, the server spawns a thread, dedicated to just that client. The server can listen for and serve other clients at the same time, so we have true concurrency.
We have to pass this socket to a separate thread to create the input/output streams. It efficiently handles multiple clients at once: When a client connects, the server spawns a thread, dedicated to just that client. The server can listen for and serve other clients at the same time, so we have true concurrency.
3. Create an input stream to read input from the client
DataInputStream in = new DataInputStream(s.getInputStream());
Above code line shows the creation of an input stream.It is used before an output stream, assuming that most servers will read data before transmitting a reply. You can switch the order of this step and the next if you send data before reading, and even omit this step if your server only transmits information.
Above code line shows the creation of an input stream.It is used before an output stream, assuming that most servers will read data before transmitting a reply. You can switch the order of this step and the next if you send data before reading, and even omit this step if your server only transmits information.
4.Create an output stream that can be used to send information back to the client
DataOutputStream out = new DataOutputStream(s.getOutputStream());
5. Close the Socket when done
When finished, close the socket.
s.close();
This method closes the associated input and output streams but does not terminate any loop that listens for additional incoming connections.
This method closes the associated input and output streams but does not terminate any loop that listens for additional incoming connections.