Chapter 6- NetworkProgramming
Chapter 6- NetworkProgramming
Networking in Java
1
Introduction
A network is a collection of computers and other devices that can
send data to and receive data from each other.
What a network program do?
Retrieve data
Send data – Once a connection between two machines is
established, Java programs can send data across the connection just
as easily as they can receive from it.
Peer-to-peer interaction
Games
Chat
File sharing
Servers
Searching the web
E-commerce
2
Introduction (cont’d)
When a computer needs to communicate with another
computer, it needs to know the other computer’s
address.
An Internet Protocol (IP) address uniquely identifies
the computer on the Internet.
TCP and UDP
TCP enables two hosts to establish a connection and
exchange streams of data.
TCP guarantees delivery of data and also guarantees that
packets will be delivered in the same order in which they
were sent.
UDP is a connectionless protocol.
3
Client/Server Computing
Networking is tightly integrated in Java.
Java API provides the classes for creating sockets to
facilitate program communications over the Internet.
Sockets are the endpoints of logical connections between
two hosts and can be used to send and receive data.
Java treats socket communications much as it treats I/O
operations; thus programs can read from or write to
sockets as easily as they can read from or write to files.
Network programming usually involves a server and one or
more clients.
4
Client/Server Computing (Cont’d)
The client sends requests to the server, and the server
responds.
The client begins by attempting to establish a connection
to the server.
The server can accept or deny the connection.
Once a connection is established, the client and the server
communicate through sockets.
The server must be running when a client attempts to
connect to the server.
The server waits for a connection request from a client.
5
The Server Socket
To establish a server, you need to create a server socket
and attach it to a port, which is where the server listens for
connections.
Port is a software address of a computer on the network.
The port identifies the TCP service on the socket.
A socket is a communication path to a port.
To communicate program over the network, give a way of
addressing the port. How? Create a socket and attach it to
the port.
Port numbers range from 0 to 65536, but port numbers 0 to
1024 are reserved for privileged services.
For instance, the email server runs on port 25, and the
Web server usually runs on port 80.
6
The Server Socket (cont’d)
You can choose any port number that is not currently
used by any other process.
The following statement creates a server socket
serverSocket:
ServerSocket serverSocket = new ServerSocket(port);
After a server socket is created, the server can use
the following statement to listen for connections:
Socket socket = serverSocket.accept();
7
The Client Socket
The client issues the following statement to request a
connection to a server:
Socket socket = new Socket(serverName, port);
ServerName is the server’s Internet host name or IP address.
8
The Client Socket(cont’d)
9
Data Transmission Through Sockets
10
Data Transmission…
Example: The client sends the radius to the server;
the server computes the area and sends it to the
client.
11
Example: Sever
package examplenetprogramming;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
public class ExampleServer {
public static void main(String args[]) throws IOException
{
ServerSocket server = new ServerSocket(800);
Socket connection = server.accept();
System.out.println("running.....");
DataInputStream input=new DataInputStream(connection.getInputStream()) ;
int num=input.read();
int twice=num*num;
DataOutputStream outPut=new
DataOutputStream(connection.getOutputStream());
outPut.write(twice);
connection.close();
}
}
12
Example: Client
package examplenetprogramming;
import java.io.*;
import java.net.Socket;
import java.util.Scanner;