1.Socket: A socket is an end point in communication link between separate processes.In Java, sockets are objects which provide a way of exchanging information between two process.
2.Socket to Socket
Communication: When two processes want to they can do via
sockets.Each process has a socket which connected to the other's socket.One
process can then write information out to the socket, while the second
process can read information back in from its socket. To achive this,
the streams model is used. Associated with each socket are two streams,
one for input and one for output.Thus to pass information from one
process to another you write that information out to the output stream
of one socket and read it from the input stream of the same socket.This
has the great advantage that, once the network connection has been
made,passing
information between
processing is not significantly different from reading and writing information
with any other stream.
3.Setting up
a Connection:
To set up the connection,
one process must be running a program that is waiting for a connection,
while the other must try to reach the first.The first is referred to as
a server socket while the second is referred to just as a socket. For the
second process to connect to first(the server socket) it must know what
machine the first running on and which port is connected to.A port number
is a logical point of communication on a computer.Generally, port numbers
below 1024 are reserved for pre-defined services.(which means that you
should avoid using them unless you wish to communicate with one of those
services, such as telnet,SMTP mail or ftp).You can use anyone of the port
number from 1024 to 99999.But one of the port number are not used by other
users.Because same port numbers cannot be used by different
user at the same time.Error message exist on the screen.
Class client works
similarly to class Server except that client sends packets only when it
is told to do so by the user typing message in a TextField and pressing
the enter key in the TextField .When this occurs, method actionPerformed
is invoked, the string the user entered in the TextField is converted
into byte. The length of the length of the String that was entered by user
is sent to the server. If an error occurs while sending a packet, IOException
is thrown.Note that the client in this program, must know that the server
is receiving packet at port 9865.Otherwise the packets will not be
received by the server. When the packet arrives, its contend are displayed
in the Server. If an error occurs while receiving packet an IOException
is thrown.The user can type information into the Client window’s TextField
and press enter at any time.
4.Implementation
the Server Application
First, I described
the server application.This is Java application program that will service
requests from client applets for information.In order to do this, it must
provide a server socket for clients to connect to.chatServer class
is the source code for this operation.
chatServer.java
This chatServer is
the server program.It is coded for network multichatting.In this
chat, Up to 30 users can log on to this chat at the same time. chatServer
has three classes which are chatMasterTread,streamManager and chatServerWorker.
chatMasterThread invoked by main method and make a server socket. A socket
object is used to connect a client to server by specifying the
server name and port number.When the socket object is instantiated. A connection
attempt that fails throw an IOException object.In this class, I opened
a server socket and gave a port number which is 9865.
by writing the code
socket= new ServerSocket(9865)
If there is client, it make stream manager and wait next client.StreamManager manage stream and broadcast it.chatServerWorker receive message from client and deliver it to the chatStreamManager. If there is request for disconnecting from client, it call streamRemove in chatStreamManager.
In my program the server socket connect to port 9865.In turn, the client socket connects to the machine on which server executing and then to port number 9865 on the carver machine.Then server listens for clients. Each client connection is managed by a Socket object. Once the ServerSocket is established ,a thread is created through which the server listens indefinitely for an attempt by a client to connect. This is accomplished with a call to the ServerSocket accept method given as:
socketA = servercsocket.accept()
System.out.println("New connection established")
Nothing happens (error
message is seen on the screen) until the server accepts the connection.At
that point the sockets are connected and the socket streams are bound to
each other.
4.Implementation
the Client Application
The client application
is essentially a very simple program that create a link to server application.To
do this it creates a socket that connects to the server's host machine,and
in our program, our port number is 9865.It then uses an object output stream
to pass string to server.Having done that, it waits for the server to provide
response.
chatClient.java
The chatClient is the client program for multichatting.Class chatClient make chatGUI and perform action by input and connect socket to server.ClientWorker receive input from server and broadcast on window.chatGUI make GUI component.
Establishing a simple server in Java require some steps:
1. Create a Server
object.A call to the ServerSocket constructor is
servercsocket = new ServerSocket( 9865)
and I defined
" ServerSocket servercsocket"
in the chatMasterThread class.The port number establishes the port where the server waits for connection from clients.Each client will ask to the server on this port.
2. Each client connection is managed with a Socket object.When the server socket is established by entering user name and click to "click to connect button".This is accomplished with a call to the ServerSocket accept method as in
socketA = servercsocket.accept()
System.out.println("New connection established")
that returns a Socket object when a connection established.
3. to get the OutputStream
and InputStream object enable the server to communicate with the client.The
server sends information to the client via OutputStream object.The server
receive information from the client by an InputStream object.To obtain
the stream, the server invokes method getOutputstream on the socket
to get the reference to the OutputStream associated with the Socket and
invoked method
getInputStream on
the Socket to get a reference to the InputStream associated with the Socket.
DataInputStream
inStream;
DataOutputStream
outStream;
......
......
inStream = new DataInputStream(s.getInputStream())
outStream = new
DataOutputStream(s.getOutputStream()
The OutputStream and InputStream object can be used to send or receive individual bytes with the the OutputStream method write and InputStream method read, respectively.Because I used java 1.1, I used the OutputStream and InputStream.
The good way of using these relationship is whatever the server writes to the DataOutputStream is sent by the OutputStream and whatever the client writes to its OutputSteram is avaliable by the server's InputStream.
4.The processing phase
is which the server and client communicate by InputStream and OutputStream
object.When the transmission is complete,the server closes the connection
by invoking the close method on the Socket.
------------ Server
-------------------
------------- Client------------------
1.Create server socket
servercsocket
= new ServerSocket( 9865)
2.Wait for connection
socketA
= servercsocket.accept()
3.Open a connection
s = new Socket(getCodeBase().getHost(), 9865)
4.Get an output stream
5.Get an input stream
6.Write data on stream
7.Read Data from input stream
8.Close stream and socket
s.close()
4 and 5 are tied.
Program Behaviour:
1.Run the command
line "java chatServer" to start chat program.
2.Type the user name
and click to "click to connect" button in order to enter the chat.
3.Enter the message
you want to write to the "input line".
4.Messages seen on
the chat window.
5.All the user names
listed on the USER list.
6.To exit chat,click
to "click to close". I assigned for this operation "BYE" string in
order to exit from
chat. If the client send "BYE" then socket is disconnected.
NOTES
A multithreaded server
can be implemented to take the Socket returned by each call to accept
create a new thread that would manage network I/O across that Socket, or
multithreaded server can be implemented to maintain a pool threads ready
to manage network I/O across theSockets as they created.
Java's networking capabilities are centralized in the java.net package.
The Server socket object establishes the port where a server waits for connection from a client and returns a socket.
The socket method getOutputStream
gets the reference to the OutputStream associated with a socket.The Socket
method getInputStream
gets a reference to the InputStream associated with the socket.
REFERENCES:
1. Gary Cornell and
Cay S. Horstmann, Core Java (Book/CD-ROM)
* Published
By SunSoft Press/Prentice-Hall
* Copyright (C) 1996 Sun Microsystems Inc.
All Rights Reserved.
ISBN 0-13-565755-5
2. How to Programming
with JAVA
Dietel & Dietel
1999
3. Java Lecture Notes
(CPS 606 ) Given by Geoffrey C. Fox, Nancy McCracken at CPS606 on Fall
Semester.
Lectures
Examples
Tutorials
4.Java for Practitioners John Hunt 1999
5.http://carver.npac.syr.edu:3768/cps606fall98-docs/cu98cju/final.html
6..http://carver.npac.syr.edu:3768/cps606spring98-docs/co98ham/project.html