[go: up one dir, main page]

0% found this document useful (0 votes)
2 views13 pages

Chapter 6- NetworkProgramming

Chapter 6 discusses networking in Java, explaining how computers communicate over networks using protocols like TCP and UDP. It details client/server computing, including how to create server and client sockets for data transmission. Examples of server and client code illustrate how to send and receive data through sockets.

Uploaded by

herid72018
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 (0 votes)
2 views13 pages

Chapter 6- NetworkProgramming

Chapter 6 discusses networking in Java, explaining how computers communicate over networks using protocols like TCP and UDP. It details client/server computing, including how to create server and client sockets for data transmission. Examples of server and client code illustrate how to send and receive data through sockets.

Uploaded by

herid72018
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
You are on page 1/ 13

Chapter 6

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.

 The following statement creates a socket at port 8000


on the client machine to connect to the host
130.254.204.36:
 Socket socket = new Socket("130.254.204.36", 8000)

8
The Client Socket(cont’d)

The server creates a server socket and, once a


connection to a client is established, connects to the
client with a client socket.

9
Data Transmission Through Sockets

The server and client exchange data


through I/O streams on top of the socket.

10
Data Transmission…
 Example: The client sends the radius to the server;
the server computes the area and sends it to the
client.

(a) The client sends the radius to the server.


(b) The server sends the area 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;

public class ExampleClient {


public static void main(String[] args) throws Exception{
Socket connection = new Socket("localhost",800);
int x,twiceX;
Scanner s=new Scanner(System.in);
System.out.println("enter an integer");
x=s.nextInt();
DataOutputStream output= new
DataOutputStream(connection.getOutputStream());
output.write(x);
DataInputStream inPut=new DataInputStream(connection.getInputStream());
twiceX=inPut.read();
System.out.println("Twice of x is : "+twiceX);
}
}
13

You might also like