Socket Programming in Java: Advantage of Network Programming
Socket Programming in Java: Advantage of Network Programming
Networking is a concept of connecting two or more computing devices together so that we can
share resources like printer, scanner, memory.In Networking application mainly two programs are
running one is Client program and another is Server program. In Core java Client
program can
be
design
and Server
program can
be
design
Centralize software management, Software install on only one system and used in
multiple system.
Socket class
Socket class are used for design a client program, it have some constructor and methods which
are used in designing client program.
Constructor: Socket class is having a constructor through this Client program can request to
server to get connection.
getInputStream()
This method take the permission to write the data from client program to server program and
server program to client program which returns OutputStream class object.
Syntax
Socket s=new Socket("localhost", 8080);
OutputStream os=new s.getOutputStream();
DataOutputStream dos=new DataOutputStream(os);
getOutputStream()
This method is used to take the permission to read data from client system by the server or from
the server system by the client which returns InputStream class object.
Syntax
Socket s=new Socket("localhost", 8080);
InputStream is=new s.getInputStream();
DataInputStream dis=new DataInputStream(is);
close()
This method is used to request for closing or terminating an object of socket class or it is used to
close client request.
Syntax
Socket s=new Socket("localhost", 8080);
s.close();
ServerSocket class
The ServerSocket class can be used to create a server socket. ServerSocket object is used to
establish the communication with clients.
ServerSocket class are used for design a server program, it have some constructor and methods
which are used in designing server program.
Constructor: ServerSocket class contain a constructor used to create a separate port number to
run the server program.
Syntax to call ServerSocket() Constructor
ServerSocket ss=new ServerSocket(8080);
// 8080 -- port number
Every server program should run accepted port number (any 4 digit +ve numeric
value) It can set by relating an object for server socket class (In any used defined
java program).
Syntax
ServerSocket ss=new ServerSocket(8080);
Send response (writing output data) back to client using OutputStream class.
Obtain connection to the server from the client program (any user defined class)
by passing port number and port name in the socket class.
Syntax
Send request (writing input data) to the server using OutputStream class.
Server Code
// Saved by Server.java
import java.net.*;
import java.io.*;
class Server
{
public static void main(String[] args)
{
try
{
int pno=Integer.parseInt(args[0]);
ServerSocket ss=new ServerSocket(pno);
System.out.println("server is ready to accept clint request");
Socket s1=ss.accept();
InputStream is=s1.getInputStream();
DataInputStream dis=new DataInputStream(is);
int n=dis.readInt();
System.out.println("Value from client : "+n);
int res=n*n;
OutputStream os=s1.getOutputStream();
DataOutputStream dos=new DataOutputStream(os);
dos.writeInt(res);
s1.close();
}
catch (Exception e)
{
System.out.println(e);
}
}
}
Client Code
// Saved by Client.java
import java.net.*;
import java.io.*;
import java.util.*;
class Client
{
public static void main(String[] args)
{
try
{
String pname=args[0];
int pno=Integer.parseInt(args[1]);
Socket s=new Socket(pname,pno);
System.out.println("clint obtailed connection from server");
System.out.println("Enter a number ");
Scanner sn=new Scanner(System.in);
int data=sn.nextInt();
OutputStream os=s.getOutputStream();
DataOutputStream dos=new DataOutputStream(os);
dos.writeInt(data);
InputStream is=s.getInputStream();
DataInputStream dis=new DataInputStream(is);
int res=dis.readInt();
System.out.println("Result from server : "+res);
}
catch (Exception e)
{
System.out.println(e);
}
}
}
Compile Client program on one command prompt and compile server program on
other command prompt.
Second run client program and pass valid port name and post number (which is
already passed at server).
Finally give the request to server from client (from client command prompt).
Using this concept you can share resource locally but not globally.
Using this concept you can not develop internet based application.
Using this concept you can not get service from universal protocols like http, ftp
etc...
Using this concept you can not get services from universal server software like
tomcat, weblogic etc..
TASK# 01:
From the below link download chat application and run it, also make a similar type of application by
your own by using GUI.
https://www.dropbox.com/s/2ryuy4696f6zdmt/chatMulti.rar?dl=0
Note: you can refer below Multi User GUI based chat application as reference.
https://www.dropbox.com/s/93xk4csak0ohqf3/chatserver.zip?dl=0