Network Programming With Java
Network Programming With Java
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>]]
http://www.oracle.com/technetwork/java/javase/downloads/index.html
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 java.net
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
InetAddress.getLocalHost() 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 = serverSocket.accept();
3. Set up input and output streams.
Scanner input = new Scanner(link.getInputStream());
PrintWriter output = new PrintWriter(link.getOutputStream(),true);
4. Send and receive data.
output.println("Awaiting data…");
String input = input.nextLine();
5. Close the connection (after completion of the dialogue).
link.close();
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.getInputStream());
PrintWriter output = new PrintWriter(link.getOutputStream(),true);
3. Send and receive data.
output.println("Awaiting data…");
String input = input.nextLine();
4. Close the connection.
link.close();
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, buffer.length);
4. Accept an incoming datagram.
datagramSocket.receive(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 = inPacket.getAddress();
int clientPort = inPacket.getPort();
6. Retrieve the data from the buffer
String message = new String(inPacket.getData(), 0,inPacket.getLength());
7. Create the response datagram.
DatagramPacket outPacket = new DatagramPacket(response.getBytes(), response.length(),
clientAddress, clientPort);
8. Send the response datagram.
datagramSocket.send(outPacket);
9. Close the DatagramSocket
datagramSocket.close();
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(message.getBytes(), message.length(),
host, PORT);
3. Send the datagram message
datagramSocket.send(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, buffer.length);
6. Accept an incoming datagram.
datagramSocket.receive(inPacket);
7. Retrieve the data from the buffer.
String response = new String(inPacket.getData(), 0, inPacket.getLength());
8. Close the DatagramSocket.
datagramSocket.close();
Network Programming with Java GANESH PAI, DEPT. OF CSE, NMAMIT, NITTE 13