2. Lecture focuses on:
TCP and UDP
Network programming basics
Identifying a machine
Servers and Clients
Ports and sockets
Data transfer using sockets
3. The term network
programming refers to writing
programs that execute across
multiple devices (computers), in
which the devices are all connected
to each other using a network.
4. Client – Server Architecture
programs running on client machines make
requests to a program (often called as server
program) running on a server machine. They
involve networking services provided by the
transport layer, which is part of the Internet
software stack, often called TCP/IP (Transport
Control Protocol/Internet Protocol) stack.
TCP is a connection-oriented protocol that
provides a reliable flow of data between two
computers.
5. the Internet software stack
The transport layer comprises two
types of protocols, TCP and UDP.
TCP − TCP stands for Transmission
Control Protocol, which allows for
reliable communication between two
applications. TCP is typically used
over the Internet Protocol, which is
referred to as TCP/IP.
UDP − UDP stands for User
Datagram Protocol, a connection-less
protocol that allows for packets of
data to be transmitted between
applications.
The most widely used programming
interfaces for these protocols are
sockets.
6. java.net Package
The classes in java.net: Java programs can use
TCP or UDP to communicate over the Internet.
The URL, URLConnection (for Web applications),
Socket, and ServerSocket (client-server
applications) classes all use TCP to communicate
over the network.
The DatagramPacket, DatagramSocket, and
MulticastSocket classes are for use with UDP.
9. What is a Socket?
A socket is defined as an endpoint for
communication.
It is a concatenation of IP address and port
The socket 161.25.19.8:1625 refers to port
1625 on host 161.25.19.8
Communication consists between a pair of
sockets.
Considered a low-level form of communication
between distributed processes.
Sockets allow only an unstructured stream of bytes
to be exchanged. It is the responsibility of the client
or server application to impose a structure on the
10. Server has
Server IP Address – by default localhost or 127.0.0.1
Port Number, use any integer >1024.
11. Programming for Sockets
Steps: For Server Side Programs
Create a class by extending Thread
public class GreetingServer extends Thread {}
Write a constructor to initialize value of ‘port’. In the
constructor create a Server Socket by using the port
number given by user. Wait for Client Request.
public GreetingServer(int port) throws IOException
{
serverSocket = new ServerSocket(port); // Opens a
Server Socket
serverSocket.setSoTimeout(20000); // Assigns wait
time
}
12. When the request from client comes, create a new
Socket by accepting that request.
Socket server = serverSocket.accept();
To Communicate with client create IO Streams. You
can read or write the data using DataInputStream and
DataOutputStream,
DataInputStream in = new
DataInputStream(server.getInputStream());
String str = in.readUTF();
DataOutputStream out = new
DataOutputStream(server.getOutputStream());
In main(), create the Thread,
Thread t = new GreetingServer(port); //Start the
13. Steps for Client Side Program
Create a Socket by giving ServerName and
Port number.
Socket client = new Socket(serverName,
port);
Create Input and Output Streams.
OutputStream outToServer =
client.getOutputStream();
DataOutputStream out = new
DataOutputStream(outToServer);
InputStream inFromServer =
client.getInputStream();
DataInputStream in = new
DataInputStream(inFromServer);
Read or Write Data using
14. Running the Programs
While Running the Programs
Write Two Different Programs for Server and
Client.
Compile both the programs by javac.
Run Server Side Program. By using
java ProgName port number
In another terminal / window run client program
java ProgName servername port number