X

Play AlgoQuiz

Rate Us :



Share with Friends :

Menu

Networking :

Networking is a logical or physical link between one or more than one system. Networking allows us to share hardware and software resources. Java is a distributed language which allows us to develop an application whose resources can be shared by more than one client. One Java program (client) communicates with another Java program (server) using networking. One Java program share resources with another Java program using multithreading.


Basic concepts of networking :


Internet :-

The global network of computers connected together in various ways which remain functional despite of diversity of hardware and software connected together through communication standards and guarantee compatibility and reliability of communication.


For networking three things are required :

  • IP address




  • Protocols




  • Port number




IP address :-

It is logically similar to traditional mailing address and this address uniquely identifies a particular object. Each computer connected with Internet has an unique IP address. It is a 32-bit number is used to uniquely identify each computer connected to the Internet.

Examples :
192.172.64.1

Hostname :-

It is a wrapper of IP address because remembering IP address is difficult so to avoid it we uses hostname.


Protocols :-

Different types of communication occurring over the Internet. Each type of communication require a specific and unique protocol. Protocols are defined as a set of rules and standards that define a certain type of internet connection.

Examples :
http , FTP -> Application Layer
TCP , UDP -> Transport Layer
IP -> Network Layer

Client / Server :

Every networking application requires two programs : client and server.

Client is a standalone program which uses the services provided by another program called server.

Server is a distributed program whose services are distributed among number of clients.


Port number :-

Port number is 16-bit unsigned integer number used to identify each application running within server system.

Examples :
80 -> http
70 -> ftp

Sockets :

It is a software abstraction for an input or output medium of communication. Communication channels enable us to transfer data through a particular port and having end point for communication between two machines. A particular type of network communication used mostly in Java program is networking programming.

java.net package :

This package provides classes useful for developing networking applications. Some classes in the package are ServerSocket and Socket classes. These classes uses TCP / IP implementation.


ServerSocket Class :-

It is one of the predefined class used for developing server side programs. It provides basic functionality of the server and has four constructors but generally two are in use.


Constructors for ServerSocket class :


ServerSocket(int port) :-

This constructor is used to instantiate a server that is bound to the specified port. A port of 0 assigns the server to any free port. Maximum queue length for incoming connection is set to 50 by default.


ServerSocket(int port , int backlog) :-

This constructor is used to instantiate a server that is bound to the specified port. Maximum queue length for incoming connection is based on the backlog parameter.


Methods in ServerSocket class :


public Socket accept() :-

This method causes the server to wait for client connection then accept them i.e. obtaining an object of Socket type.


public void close() :-

This method is used to close the server socket , clients can no longer connect to the server unless it is opened again.


public int getLocalPort() :-

This method returns the port on which the socket is bound to work.


public boolean isClose() :-

This method indicates that the socket is close or not.


Example :

import java.net.*; class Server { public static void main(String[ ] args) throws Exception { ServerSocket ss = new ServerSocket(60); System.out.println("Server is running..."); Socket s = ss.accept(); System.out.println("Connection Established."); s.close(); ss.close(); } } //save as : Server.java //compile as : javac Server.java //run as : java Server

Output :

Server is running... Connection Established.

Socket Class :

It is one of the predefined class and whose object is used for developing client side applications. It implements a client socket. It has eight constructors in which two are already deprecated.


Constructors for Socket class :


Socket(String host , int port) :-

This constructor creates a client socket that connects to the given port number on the specified host.


Socket(InetAddress address , int port) :-

This constructor creates a client socket that connects to the given port number at the specified address.


Methods in Socket class :


public void close() :-

This method is used to close the client socket.


public InputStream getInputStream() :-

This method retrieves the input stream associated with the socket.


public OutputStream getOutputStream() :-

This method retrieves the output stream associated with the socket.


public InetAddress getInetAddress() :-

This method returns the IP address to which the socket is connected.


public int getPort() :-

This method returns the remote port to which the socket is connected.


public boolean isClose() :-

This method indicates whether the socket is close or not.


Example 1 :

import java.net.*; class Client { public static void main(String[ ] args) throws Exception { Socket s = new Socket("localhost" , 60); s.close(); } } //save as : Client.java //compile as : javac Client.java //run as : java Client

Output :

In the above program client just connect with server.

Example :

import java.net.*; import java.io.*; class MessageServer { public static void main(String[ ] args) throws Exception { ServerSocket ss = new ServerSocket(60); System.out.print("Server is running..."); Socket s = ss.accept(); System.out.print("Connection is established"); InputStream is = s.getInputStream(); int a; while((a = is.read()) != -1) System.out.printf("%c",a); is.close(); s.close(); ss.close(); } } //save as : MessageServer.java //compile as : javac MessageServer.java //run as : java MessageServer


import java.net.*; import java.io.*; class MessageClient { public static void main(String[ ] args) throws Exception { Socket s = new Socket("localhost" , 60); OutputStream os = s.getOutputStream(); PrintStream ps = new PrintStream(os); ps.println("Hello Server"); } } //save as : MessageClient.java //compile as : javac MessageClient.java //run as : java MessageClient

Example :

import java.net.*; import java.io.*; class ChatServer { public static void main(String[ ] args) throws Exception { ServerSocket ss = new ServerSocket(60); System.out.print("Server is running..."); while(true) { Socket s = ss.accept(); System.out.print("Connection is established"); InputStream is = s.getInputStream(); OutputStream os = s.getOutputStream(); int a; while((a = is.read()) != -1) System.out.printf("%c",a); PrintStream ps = new PrintStream(os); ps.println("Hello Client"); } } } //save as : ChatServer.java //compile as : javac ChatServer.java //run as : java ChatServer


import java.net.*; import java.io.*; class ChatClient { public static void main(String[ ] args) throws Exception { Socket s = new Socket("localhost" , 60); while(true) { InputStream is = s.getInputStream(); OutputStream os = s.getOutputStream(); PrintStream ps = new PrintStream(os); ps.println("Hello Server"); int a; while((a = is.read()) != -1) System.out.printf("%c",a); if(a == -1) break; } } } //save as : ChatClient.java //compile as : javac ChatClient.java //run as : java ChatClient



X

MiniDoll :

MiniDoll is an application to learn with fun. You can play it to solve some tricky questions related to Java Programming. Either enjoy playing it alone or discuss with your friends to solve it. It is a simple approach to learn by solving questions. This application has different topics of Java and each topic is divided into three phases. Start solving from initial topic to final topic and by the end of every topic you can boost your concept of that topic.