0% found this document useful (1 vote)
131 views13 pages

Java Network Programming Guide

The document discusses network programming with Java and covers topics like clients, servers, IP addresses, ports, sockets, TCP, UDP, and how to set up TCP and UDP sockets for both clients and servers. It provides step-by-step explanations for creating server and client sockets using TCP and UDP and handling sending and receiving data across the network connections. The document is presented as part of a textbook on network programming with Java written by Ganesh Pai from the Computer Science department of NMAMIT, NITTE.

Uploaded by

Rishi Phal
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (1 vote)
131 views13 pages

Java Network Programming Guide

The document discusses network programming with Java and covers topics like clients, servers, IP addresses, ports, sockets, TCP, UDP, and how to set up TCP and UDP sockets for both clients and servers. It provides step-by-step explanations for creating server and client sockets using TCP and UDP and handling sending and receiving data across the network connections. The document is presented as part of a textbook on network programming with Java written by Ganesh Pai from the Computer Science department of NMAMIT, NITTE.

Uploaded by

Rishi Phal
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
  • Introduction
  • Overview of Topics
  • Internet Services
  • URL and DNS
  • TCP & UDP
  • InetAddress Class
  • TCP Sockets
  • UDP Sockets

Network Programming

with Java
G A N E S H PA I
A S S T. P R O F E S S O R G D I I I
D E PA RT M E N T O F C S E
N M A M I T, N I T T E
Textbook

Network Programming with Java GANESH PAI, DEPT. OF CSE, NMAMIT, NITTE 2
Overview of topics
 Clients, Servers and Peers
 The Internet
 IP Addresses, Port numbers
 Sockets
 Internet Services, URLs and DNS
 TCP & UDP
 InetAddress class
 Using TCP Sockets
 Using UDP Sockets

Network Programming with Java GANESH PAI, DEPT. OF CSE, NMAMIT, NITTE 3
Internet Services
 Click to edit Master text styles
 Second level
 Third level
◦ Fourth level
◦ Fifth level

Network Programming with Java GANESH PAI, DEPT. OF CSE, NMAMIT, NITTE 4
URL (Uniform Resource Locator) Format &
DNS
<protocol>://<hostname>[:<port>][/<pathname>] [/<filename>[#<section>]]

 [Link]

Network Programming with Java GANESH PAI, DEPT. OF CSE, NMAMIT, NITTE 5
TCP & UDP

Network Programming with Java GANESH PAI, DEPT. OF CSE, NMAMIT, NITTE 6
InetAddress class
 Available in [Link]
 Handles Internet addresses as host names
and as IP addresses.
 Static method getByName() uses DNS
(Domain Name System) to return the
Internet address of a specified host name
as an InetAddress object.
 getByName() throws the checked exception
UnknownHostException if the host name is
not recognized
 [Link]() returns IP
address of current machine

Network Programming with Java GANESH PAI, DEPT. OF CSE, NMAMIT, NITTE 7
TCP Sockets - Steps
Setting up Server Socket
 1. Create a ServerSocket object.
ServerSocket serverSocket = new ServerSocket(1234);
 2. Put the server into a waiting state.
Socket link = [Link]();
 3. Set up input and output streams.
Scanner input = new Scanner([Link]());
PrintWriter output = new PrintWriter([Link](),true);
 4. Send and receive data.
[Link]("Awaiting data…");
String input = [Link]();
 5. Close the connection (after completion of the dialogue).
[Link]();

Network Programming with Java GANESH PAI, DEPT. OF CSE, NMAMIT, NITTE 8
TCP Sockets - Steps
Setting up Client Socket
 1. Establish a connection to the server
Socket link = new Socket(SERVER_IP, SERVER_PORT);
 2. Set up input and output streams
Scanner input = new Scanner([Link]());
PrintWriter output = new PrintWriter([Link](),true);
 3. Send and receive data.
[Link]("Awaiting data…");
String input = [Link]();
 4. Close the connection.
[Link]();

Network Programming with Java GANESH PAI, DEPT. OF CSE, NMAMIT, NITTE 9
UDP Sockets - Steps
Setting up Server Socket
 1. Create a DatagramSocket object
DatagramSocket datagramSocket = new DatagramSocket(1234);
 2. Create a buffer for incoming datagrams
byte[] buffer = new byte[256];
 3. Create a DatagramPacket object for the incoming datagrams
DatagramPacket inPacket = new DatagramPacket(buffer, [Link]);
 4. Accept an incoming datagram.
[Link](inPacket);

Network Programming with Java GANESH PAI, DEPT. OF CSE, NMAMIT, NITTE 10
UDP Sockets - Steps
 5. Accept the sender’s address and port from the packet.
InetAddress clientAddress = [Link]();
int clientPort = [Link]();
 6. Retrieve the data from the buffer
String message = new String([Link](), 0,[Link]());
 7. Create the response datagram.
DatagramPacket outPacket = new DatagramPacket([Link](), [Link](),
clientAddress, clientPort);
 8. Send the response datagram.
[Link](outPacket);
 9. Close the DatagramSocket
[Link]();

Network Programming with Java GANESH PAI, DEPT. OF CSE, NMAMIT, NITTE 11
UDP Sockets - Steps
Setting up Client Socket
 1. Create a DatagramSocket object
DatagramSocket datagramSocket = new DatagramSocket(1234);
 2. Create the outgoing datagram
DatagramPacket outPacket = new DatagramPacket([Link](), [Link](),
host, PORT);
 3. Send the datagram message
[Link](outPacket);
 4. Create a buffer for incoming datagrams
byte[] buffer = new byte[256];

Network Programming with Java GANESH PAI, DEPT. OF CSE, NMAMIT, NITTE 12
UDP Sockets - Steps
 5. Create a DatagramPacket object for the incoming datagrams
DatagramPacket inPacket = new DatagramPacket(buffer, [Link]);
 6. Accept an incoming datagram.
[Link](inPacket);
 7. Retrieve the data from the buffer.
String response = new String([Link](), 0, [Link]());
 8. Close the DatagramSocket.
[Link]();

Network Programming with Java GANESH PAI, DEPT. OF CSE, NMAMIT, NITTE 13

Network Programming 
with Java
G A N E S H  PA I
A S S T.  P R O F E S S O R  G D  I I I
D E PA RT ME N T O F  C S E
N M A M
Textbook
Network Programming with Java
GANESH PAI, DEPT. OF CSE, NMAMIT, NITTE
2
Overview of topics
Network Programming with Java
GANESH PAI, DEPT. OF CSE, NMAMIT, NITTE
3
Clients, Servers and Peers
The I
Internet Services
Network Programming with Java
GANESH PAI, DEPT. OF CSE, NMAMIT, NITTE
4
Click to edit Master text styles

URL (Uniform Resource Locator) Format & 
DNS
Network Programming with Java
GANESH PAI, DEPT. OF CSE, NMAMIT, NITTE
5
<protoco
TCP & UDP
Network Programming with Java
GANESH PAI, DEPT. OF CSE, NMAMIT, NITTE
6
InetAddress class
Network Programming with Java
GANESH PAI, DEPT. OF CSE, NMAMIT, NITTE
7
Available in java.net
Handles Int
TCP Sockets - Steps
Network Programming with Java
GANESH PAI, DEPT. OF CSE, NMAMIT, NITTE
8
Setting up Server Socket
1. Crea
TCP Sockets - Steps
Network Programming with Java
GANESH PAI, DEPT. OF CSE, NMAMIT, NITTE
9
Setting up Client Socket
1. Esta
UDP Sockets - Steps
Network Programming with Java
GANESH PAI, DEPT. OF CSE, NMAMIT, NITTE
10
Setting up Server Socket
1. Cre

You might also like