Server Side
Programming
Creating a Server
CONTENTS
Accepting Connection
from browsers
CREATING A WEB SERVER
Create a ServerSocket object.
Create a Socket object from the ServerSocket.
STEPS
Create an i/p stream to read i/p from the client.
Create an o/p stream to send information .
Do I/O with input and output streams.
Close the Socket when done.
CREATING A SERVER
STEP 1 STEP 2 STEP 3
Create a ServerSocket Create a Socket object Create an i/p stream to
object. Create a ServerSocket
from the ServerSocket. read i/p from the client.
object.
STEP 4 STEP 5 STEP 6
Create an o/p stream to Do I/O with input and Close the Socket when
send information . Create a ServerSocket
output streams. done.
object.
1.CREATE A SERVERSOCKET
OBJECT
With a client socket, you actively go out and connect to a
particular system. With a server, however, you passively sit and
wait for someone to come to you. So, creation requires only a
port number, not a host, as follows:
ServerSocket listenSocket =
new ServerSocket(portNumber);
2. CREATE A SOCKET OBJECT FROM THE
SERVER SOCKET
Many servers allow multiple connections, continuing to accept
connections until some termination condition is reached. The
Server Socket accept method blocks until a connection is
established, then returns a normal Socket object. Here is the
basic idea:
while(someCondition)
{ Socket server listenSocket.accept();
doSomethingWith(server);
}
3. CREATE AN I/P STREAM TO READ I/P
FROM THE CLIENT.
Once you have a Socket, you can use the socket in the same
way as with the client code
In the client section, a BufferedReader is more efficient to use
underneath the InputStreamReader, as follows:
BufferedReader in = new BufferedReader
(new
InputStreamReader(server.getInputStream()))
4.CREATE AN O/P STREAM TO
SEND INFORMATION
You can use a generic OutputStream if you want to send binary
data. If you want to use the familiar print and println commands,
create a Print_Writer. Here is an example of creating a
PrintWriter:
PrintWriter out = new
PrintWriter(server.getOutputStream());
5.DO I/O WITH INPUT AND
OUTPUT STREAM
The BufferedReader, DataInputStream, and PrintWriter classes can be
used in the same ways as discussed in the client section earlier in this
chapter. BufferedReader provides read and readLine methods for
reading characters or strings. DataInputStream has readByte and
readFully methods for reading a single byte or a byte array. Use print
and println for sending high-level data through a PrintWriter; use write
to send a byte or byte array.
6.CLOSE THE SOCKET WHEN
DONE.
When finished, close the socket:
server.close();
ACCEPTING THE CONNECTION
FROM BROWSER
Suppose the test program is started on port 8088 of
system1.com:
system1> java NetworkServerTest
Then, a standard Web browser (Netscape in this case)
on system2.com requests url yielding the following
back on system1.com:
Generic Network Server:
got connection from system2.com
with first line 'GET /foo/ HTTP/1.0'
Vanshita Mittal B. Supriya Nidhi Chourasiya
309802218002 309802218069 309802218039
THANK YOU
DO LET US KNOW YOUR DOUBT...