Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
43 views

Server Side Programming

This document discusses the steps to create a basic web server: 1. Create a ServerSocket object, specifying the port number to listen on for connections. 2. Use the ServerSocket's accept method to retrieve a Socket object for each incoming connection. 3. Create input and output streams connected to the Socket to read from and write to the client. 4. Use the input stream to read data sent by the client and the output stream to send responses, then close the Socket to finish the request.

Uploaded by

Vanshita Mittal
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views

Server Side Programming

This document discusses the steps to create a basic web server: 1. Create a ServerSocket object, specifying the port number to listen on for connections. 2. Use the ServerSocket's accept method to retrieve a Socket object for each incoming connection. 3. Create input and output streams connected to the Socket to read from and write to the client. 4. Use the input stream to read data sent by the client and the output stream to send responses, then close the Socket to finish the request.

Uploaded by

Vanshita Mittal
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

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...

You might also like