You are on page 1of 13

Client-Server

Programming Using
TCP
Prepared by :
- Vivek Patel (130050131077)
CSE-2S

Basics
Network: The Network represents intercommunication
of computers and devices either by using a cable or
satellite link where there is no cable needed.
Advantage of Java Networking:
sharing resources
centralize software management

Network Terminology
IP Address: IP address is a unique number assigned to
a node of a network e.g. 192.168.0.1. It is a logical
address that can be changed.
Protocol: A protocol is a set of rules basically that is
followed for communication.
Eg. : HTTP, TCP, FTP, SMTP
Port Number: A 16-bit no. used to identify a process
on a machine.

Network Terminology
Socket: Socket is logical endpoint between client and
server that allows communication between two host
over a network.
It contains 2 parts :
1. Port Number: identifies the process (service) on
the machine.
2. IP Address: identifies the machine in the
network.

TCP/IP Suite
TCP(Transfer Control Protocol)/IP(Internet Protocol)
Defines rules governing transmission of data over the
network.
Consists of 5 Layers:
1.
2.
3.
4.
5.

Application
TCP(Transport)
IP(Network)
Data Link Layer
Physical

TCP/IP Suite
Application layer receives data from application and
formats the data and sends it to next layer (TCP) in form
of continuous stream of bytes.
Transport layer divides this stream in segments called
TCP Segments and sends it to IP layer
IP layer puts these segments within envelopes called
Datagram and transfers it to DLL.
DLL forms the packets and sends it to Physical layer.
Physical layer transmits data on network using
appropriate Hardware.

Client-Server Programming

Client: Receives the data and services provided by


server.
Server: Provides the data and services to client.

Client-Server Programming
Steps for developing a Server that sends
data:
1. Create Server Socket with some port number.
ServerSocket ss = new ServerSocket(777);
2. Make Server wait till Client accepts Connection.
Socket s = ss.accept();
3. Attach Output Stream to server using
getOutoutStream() method.
OutputStream obj = s.getOutputStream();

Client-Server Programming
4. Take another stream Printstream to send data to
client.
PrintStream ps = new PrintStream(obj);
5. Send data using print() or println() method.
ps.println(str);
6. Close the Connection.
ss.close();
s.close();
Ps.close();

Client-Server Programming
Example of Server Program: MyServer.java
Import java.io.*;
Import java.net.*;
Public class MyServer{
Public static void main(String[] args){
try{
ServerSocketss=new ServerSocket(777);
Sockets=ss.accept();//establishesconnection
DataInputStream dis=new DataInputStream(s.getInputStream());
String str=(String)dis.readUTF();
System.out.println("message="+str);
ss.close();
}catch(Exceptione){System.out.println(e);}
}
}

Client-Server Programming
Steps for creating a client application:
1. Create a Socket using Socket Class.
Socket s =new Socket(IPAddress,Port No.);
2. Add InputStream to the socket so socket can receive data.
InputStream obj = s.getInputStream();
3. Create BufferedReader object to read the data.
BufferedReader br = new BufferedReader(new
InputStreamReader(obj));

Client-Server Programming
4. Read the data using read() and readLine() method
of BufferedReader object.
Str = br.readLine();
5. Close the Connection.
br.close();
s.close();

Client-Server Programming
Example Of Client Application: MyClient.java
Import java.io.*;
Import java.net.*;
Public class MyClient{
Public static voidmain(String[]args){
try{
Sockets=newSocket("localhost",6666);
DataOutputStream dout=new DataOutputStream(s.getOutputStream());
dout.writeUTF("HelloServer");
dout.flush();
dout.close();
s.close();
}catch(Exceptione){System.out.println(e);}
}
}

You might also like