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