TCP/IP Program in JAVA


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


Differ type of Definition for Client-Server progrgam using TCP/IP

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

write a java program  if one computer (Client)  send a value to  another computer (server) and the second computer  send  square value of which is get from client

Example Program of  java using TCP/IP connection between client & server


Example of Client Server Program in java using TCP/IP

Source Code for  Server

 

Source Code for  Client

Output of Client-Server Program in JAVA using TCP/IP

First two different cmd(command prompt) window open on   screen

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

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

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

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

step-4  e:>java Client on cmd screen-2
Enter value ..

For Better Understanding see on the below Screenshot

tcp-ip Client-Server
tcp-ip Client-Server

 

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 321 times, 1 visits today)

Leave a Reply

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


CAPTCHA Image
Reload Image