Report of Client and Server
Report of Client and Server
INSTITUTE OF TECHNOLOGY
Prepared by:
Arbaminch, Ethiopia
Create a chat application with java socket
Java socket programming between Client and Server socket code report
Server: is used by the server to declare a server socket object which the server needs to listen to
connection requests from the clients. Socket this class is used by the server to declare a socket
object, which the server uses to send and receive data from the client.
1. Server.Jave
- Final serversocket: this line means we declared an object called “serversocket” which is an
object of the class serversocket. It is declared “final” which means it is a constant.
- Final socket clientsocket: this line means we declared an object called “clientsocket” which
is an object of the class socket. It is declared “final” which means it is a constant.
- Final Buffered Reader in: this line means we declared an object called “in” which is an
object of the class Buffered Reader. It is used to read data from the clientsockent object.
- Final print writer out: this line means we declared and object called “out” which is an object
of the class print writer. It used to write data into the clientsocket object.
try{
Socket s = ss.accept();
As you see, the constructor of serversocket class must take a port number that the server will use
to listen to client’s requests, also the instantiating should be try/catch.
try{
ServerSocket ss = new ServerSocket(1201);
Socket s = ss.accept();
String msgin="",msgout="";
while(!msgin.equals("end")){
msgin = din.readUTF();
System.out.println(msgin);
msgout = br.readLine();
dout.writeUTF(msgout);
dout.flush();
s.close();
The server to listen to connection requests from client, it uses the method accept() to wait for a
request from the client, once it receives one it accept it and create an instance of the socket class
which in our case will be the “cientsocket” object.
try{
Socket s = ss.accept();
To instance out we use the constructor of dataoutstream class, this constructor takes one parameter
which is an output stream for the socket, notice that the socket used here is client socket, because
it is responsible for sending data to the client.
Sender: it contains the code the server will used to send messages to client
Receive: it contains the code the server will to receive message from client.
Sender:
Now in this part, we will read data from user and we send this data to the client.
String msgin="",msgout="";
while(!msgin.equals("end")){
msgin = din.readUTF();
System.out.println(msgin);
msgout = br.readLine();
dout.writeUTF(msgout);
dout.flush();
Receive:
This method reads line by line the message sent by the client, if this method returns null that means
that the client is not connected anyone. It doesn’t mean that the client didn’t send anything
because the client can be connected to the server and still doesn’t send anything.
try{
String msgin="",msgout="";
while(!msgin.equals("end")){
msgout = br.readLine();
dout.writeUTF(msgout);
msgin = din.readUTF();
System.out.println(msgin);
s.close();
}catch(Exception e){
Host: which is the IP address of server, in our case the server and client are both implemented on
the same machine so we use the address localhost which is: 127.0.0.1
Port: a port number which is the port number defined by the server in the server.java.