Unit 1_Network
Unit 1_Network
Chapter 1
By: Prof. Bijal Gadhia(GECG)
What is network??
•Interconnection of computers and devices
either by using a cable or satellite link where no
cable is needed.
–Client: Receives service
–Server: Provides service.
The OSI Model
TCP/IP and OSI model
Addresses in the TCP/IP protocol suite
TCP and UDP are end-to-end protocols:
Which means they are executed on the end points
(hosts).
The Internet supports 2 transport protocols.
Knowing IP Address:
import java.net.*;
import java.io.*;
System.out.println("Protocol:"+obj.getProtocol());
System.out.println("Host:"+obj.getHost());
System.out.println("File:"+obj.getFile());
System.out.println("Port:"+obj.getPort());
System.out.println("ExternalForm:"+obj.toExternalForm());
}
}
OUTPUT:
Program to Demonstrate URLConnection class.
import java.io.*;
import java.net.*;
import java.util.*;
int ch;
InputStream in= conn.getInputStream();
while((ch=in.read())!=-1)
{
System.out.print((char)ch);
}
}
}
}
OUTPUT:
import java.io.*;
import java.net.*;
public class GetIPAddress
{
public static void main(String[] args){
try{
InetAddress ip=InetAddress.getByName("www.javatpoint.com");
•Socket pair
–Specified the two end points that uniquely identifies each TCP connection
in an internet.
–4-tuple: (client IP address, client port number, server IP address, server
port number)
Client-server applications
•Implementation of a protocol standard defined . (FTP, HTTP, SMTP…)
–Should use the port number associated with the protocol.
•Client
–Client socket
•Initiate a TCP connection to the server by creating a socket object.
(Three-way handshake)
•Specify the address of the server process, namely, the IP address of
the server and the port number of the process.
Socket functional calls
•socket (): Create a socket
•bind(): bind a socket to a local IP address and port #
•listen(): passively waiting for connections
•connect(): initiating connection to another socket
•accept(): accept a new connection
•Write(): write data to a socket
•Read(): read data from a socket
•sendto(): send a datagram to another UDP socket
•recvfrom(): read a datagram from a UDP socket
•close(): close a socket (tear down the connection)
Sockets
recv( )
send( )
close( ) close( )
controlled by
application process
process
developer socket
socket
controlled by TCP with
TCP with
operating buffers, internet
system buffers,
variables
variables
Socket programming with TCP
write reply to
connectionSocket read reply from
clientSocket
close
connectionSocket close
clientSocket
JAVA TCP Sockets
•In Package java.net
–java.net.Socket
•Implements client sockets (also called just “sockets”).
•An endpoint for communication between two machines.
•Constructor and Methods
–ServerSocket(int port)
–Socket Accept(): Listens for a connection to be made to this socket
and accepts it. This method blocks until a connection is made.
TCPClient.java
import java.io.*;
import java.net.*;
class TCPClient {
public static void main(String argv[]) throws Exception
{
String sentence;
String modifiedSentence;
BufferedReader inFromUser =
new BufferedReader(new InputStreamReader(System.in));
Socket clientSocket = new Socket(“localhost", 6789);
DataOutputStream outToServer =
new DataOutputStream(clientSocket.getOutputStream());
TCPClient.java
BufferedReader inFromServer =
new BufferedReader(new
InputStreamReader(clientSocket.getInputStream()));
sentence = inFromUser.readLine();
outToServer.writeBytes(sentence + '\n');
modifiedSentence = inFromServer.readLine();
System.out.println("FROM SERVER: " + modifiedSentence);
clientSocket.close();
}
}
TCPServer.java
import java.io.*;
import java.net.*;
class TCPServer {
public static void main(String argv[]) throws Exception
{
String clientSentence;
String capitalizedSentence;
ServerSocket welcomeSocket = new
ServerSocket(6789);
while(true) {
Socket connectionSocket =
welcomeSocket.accept();
BufferedReader inFromClient = new BufferedReader(new
InputStreamReader(connectionSocket.getInputStream()));
TCPServer.java
DataOutputStream outToClient =
new DataOutputStream(connectionSocket.getOutputStream());
clientSentence = inFromClient.readLine();
capitalizedSentence = clientSentence.toUpperCase() + '\n';
outToClient.writeBytes(capitalizedSentence);
}
}
}
Run Server
Run Client
Write some string
//echo the details of incoming data - client ip : client port - client message
}
}
catch(IOException e)
{
System.err.println("IOException " + e);
}
}
try
{
sock = new DatagramSocket();
while(true)
{
//take input and send the packet
echo("Enter message to send : ");
s = (String)cin.readLine();
byte[] b = s.getBytes();
UDPClient.java
DatagramPacket dp = new DatagramPacket(b , b.length , host , port);
sock.send(dp);
byte[] buffer = new byte[65536];
DatagramPacket reply = new DatagramPacket(buffer, buffer.length);
sock.receive(reply);
byte[] data = reply.getData();
s = new String(data, 0, reply.getLength());
//echo the details of incoming data - client ip : client port - client message
echo(reply.getAddress().getHostAddress() + " : " + reply.getPort() + " - " + s);
}
}
catch(IOException e)
{
System.err.println("IOException " + e);
}
}
//simple function to echo data to terminal
public static void echo(String msg)
{
System.out.println(msg);
}
}
public class udpBaseServer_2
{
public static void main(String[] args) throws IOException
ds.receive(DpReceive);
System.out.println("Client:-" + data(receive));
if (data(receive).toString().equals("bye"))
{
System.out.println("Client sent bye.....EXITING");
break;
}
InetAddress ip = InetAddress.getLocalHost();
byte buf[] = null;
while (true)
{
String inp = sc.nextLine();
buf = inp.getBytes();
if (inp.equals("bye"))
break;
}
}
}