Client Server Chat Program in java using TCP/IP


In Internet the traffic is control by strict protocol . There are two types of Internet Protocol (IP).

Types of Internet Protocol(IP)

1. TCP (Transmission Control Protocol ) and
2.UDP (User or Universal  Datagram Protocol)

TCP is connection oriented – reliable protocol which transfer the data in continues streams.once a connection is established, data can be sent bidirectional. It is a two way communication.

UDP is a  connection less ,independent , self contained protocol. Bulk Data  are send and receive  as packets from using UDP.

whether you are sending a packet via TCP or UDP, the packet is sent to an IP address. These packets are forwarded from your computer  to the destination via on Internet router.

Process of Communication

First when client send request to server means that when you load a web page, your computer sends TCP (bytes forms)packets to the web server’s address, then server  send the web page to you as response.

The web server sending a stream of TCP packets as response , which your web browser receive the web pages  and display it to you.

 


How to make Client Server Chat Program in java using TCP/IP

First you Open any Editor like Notepad++
write first program Server1.java and save it.

now  open new File
write second  program Client1.java and save it.

 

Example of Client Server Chat Program in java using TCP/IP

Source Code for  Server

 

Source Code for  Client

 

Before Compiling and Running the Chat Program in Java using TCP/IP

we open cmd(Command Prompt) in two different Windows.

now we seen two different cmd window screen  on monitor now do this step

step-1   e:>javac Server1.java on cmd screen-1

step-2  e:>javac Client1.java on cmd screen-1

step-3  e:>java Server1  on cmd screen-1

step-4  e:>java Client1 on cmd screen-2
Hello

For Better Understanding see on the below Screenshot

Output of Chat Program for TCP/IP

chat program for tcp-ip
chat program for tcp-ip

Program Explain

The java.net package is required for socket programming.and The java.io package is required to support i/o operations. and

java.net package provide two class
ServerSocket class and socket class

ServerSocket class creates stream socket and connect with specific port ,ip address,host name.
where socket class which is known listen Socket which class creates another socket for listen to the client

readLine() method in BufferedReader object is created to read the input from keyboard (write on console)

GetInputStream() method in InputStreamReader is for receive(read) the input stream where System.in as a parameter to InputStreamReader.
getOutputStream() method in DataOutputStream which sent(write)  output stream  data to other side of  computer.

(Visited 6,494 times, 1 visits today)

Leave a Reply

Your email address will not be published. Required fields are marked *


CAPTCHA Image
Reload Image