Exp-8 Name: Tirth Govani Roll No:N-235 Class: Mbatech Branch: Cs Part A
Exp-8 Name: Tirth Govani Roll No:N-235 Class: Mbatech Branch: Cs Part A
Theory:
A socket is one end-point of a two-way communication link between two programs running
on the network. Socket classes are used to represent the connection between a client program
and a server program.
The chat system consists of two distributed components: chat server and chat client, which
may run on different hosts in the network. We provide you the server side of the system. We
also provide a skeleton of the chat client that includes a simple user interface to allow users of
the chat client to type in control commands and chat messages. You must add client support
for all communication, including requesting chat server location information from a location
server, the exchange of control messages with the server, and the display of received chat
messages. Finally, your chat client must detect chat server failures and attempt a simple form
of recovery.
The chat server conducts a chat session in chat rooms. At any given time there may be
multiple chat clients in a chat session. The chat server is responsible for managing all the chat
clients in the session and distributing chat messages. A chat client starts by requesting the
communication parameters (server name and port numbers) for the chat server from a location
server.
When the connection is made, the server creates a socket object on its end of the
communication. The client and server can now communicate by writing to and reading from
the socket.
The following steps occur when establishing a TCP connection between two computers using
sockets:
The server instantiates a ServerSocket object, denoting which port number
communication is to occur on.
The server invokes the accept() method of the ServerSocket class. This method waits
until a client connects to the server on the given port.
After the server is waiting, a client instantiates a Socket object, specifying the server
name and port number to connect to.
The constructor of the Socket class attempts to connect the client to the specified
server and port number. If communication is established, the client now has a Socket
object capable of communicating with the server.
On the server side, the accept() method returns a reference to a new socket on the
server that is connected to the client's socket.
After the connections are established, communication can occur using I/O streams. Each
socket has both an OutputStream and an InputStream. The client's OutputStream is connected
to the server's InputStream, and the client's InputStream is connected to the server's
OutputStream.
TCP is a two way communication protocol, so data can be sent across both streams at the
same time. There are following useful classes providing complete set of methods to
implement sockets.
Procedure:
Step 1: A simple server that will accept a single client connection and display
everything the client says on the screen. If the client user types ".bye", the client and
the server will both quit.
Step 2: A server as before, but this time it will remain 'open' for additional connection
once a client has quit. The server can handle at most one connection at a time.
Step 3: A server as before, but this time it can handle multiple clients simultaneously.
The output from all connected clients will appear on the server's screen.
Step 4: A server as before, but this time it sends all text received from any of the
connected clients to all clients. This means that the server has to receive and send, and
the client has to send as well as receive
Step 5: Wrapping the client from step 4 into a very simple GUI interface but not
changing the functionality of either server or client.
Instructions:
1. Write program in Java.
2. Use inbuilt packages for socket programming.
Part B
Name:
Roll no:
Class: MBATech A Div
Sap ID:-
Code:
FOR SERVER:
import java.io.*;
import java.net. *;
public class Myserver {
public static void main(String[] args){
try{
ServerSocket ss=new ServerSocket (6666);
Socket s=ss.accept(); //establishes connection
DataInputStream dis=new DataInputStream(s.getInputStream());
String str = (String)dis.readUTF();
System.out.println("message= "+str);
ss.close();
}catch(Exception e) {System.out.println(e);}
}
}
Output:
Conclusion:
Write statement of conclusion here