http://www.tutorialspoint.com/java/java_networking.htm
The t erm network programming refers t o writ ing programs t hat execut e across mult iple devices
(comput ers), in which t he devices are all connect ed t o each ot her using a net work.
The java.net package of t he J2SE APIs cont ains a collect ion of classes and int erfaces t hat provide
t he low-level communicat ion det ails, allowing you t o writ e programs t hat focus on solving t he
problem at hand.
The java.net package provides support for t he t wo common net work prot ocols:
T CP: TCP st ands for Transmission Cont rol Prot ocol, which allows for reliable communicat ion
bet ween t wo applicat ions. TCP is t ypically used over t he Int ernet Prot ocol, which is referred t o
as TCP/IP.
UDP: UDP st ands for User Dat agram Prot ocol, a connect ion-less prot ocol t hat allows for
packet s of dat a t o be t ransmit t ed bet ween applicat ions.
This t ut orial gives good underst anding on t he following t wo subject s:
So cket Pro gramming: This is most widely used concept in Net working and it has been
explained in very det ail.
URL Pro cessing: This would be covered separat ely. Click here t o learn about URL Processing
in Java language.
Socket Programming:
Socket s provide t he communicat ion mechanism bet ween t wo comput ers using TCP. A client
program creat es a socket on it s end of t he communicat ion and at t empt s t o connect t hat socket t o
a server.
When t he connect ion is made, t he server creat es a socket object on it s end of t he communicat ion.
The client and server can now communicat e by writ ing t o and reading from t he socket .
The java.net .Socket class represent s a socket , and t he java.net .ServerSocket class provides a
mechanism for t he server program t o list en for client s and est ablish connect ions wit h t hem.
The following st eps occur when est ablishing a TCP connect ion bet ween t wo comput ers using
socket s:
The server inst ant iat es a ServerSocket object , denot ing which port number communicat ion is
t o occur on.
The server invokes t he accept () met hod of t he ServerSocket class. This met hod wait s unt il a
client connect s t o t he server on t he given port .
Aft er t he server is wait ing, a client inst ant iat es a Socket object , specifying t he server name
and port number t o connect t o.
The const ruct or of t he Socket class at t empt s t o connect t he client t o t he specified server
and port number. If communicat ion is est ablished, t he client now has a Socket object capable
of communicat ing wit h t he server.
On t he server side, t he accept () met hod ret urns a reference t o a new socket on t he server
t hat is connect ed t o t he client 's socket .
Aft er t he connect ions are est ablished, communicat ion can occur using I/O st reams. Each socket has
bot h an Out put St ream and an Input St ream. The client 's Out put St ream is connect ed t o t he server's
Input St ream, and t he client 's Input St ream is connect ed t o t he server's Out put St ream.
TCP is a t woway communicat ion prot ocol, so dat a can be sent across bot h st reams at t he same
t ime. There are following usefull classes providing complet e set of met hods t o implement socket s.
If t he ServerSocket const ruct or does not t hrow an except ion, it means t hat your applicat ion has
successfully bound t o t he specified port and is ready for client request s.
Here are some of t he common met hods of t he ServerSocket class:
SN
1
When t he ServerSocket invokes accept (), t he met hod does not ret urn unt il a client connect s. Aft er
a client does connect , t he ServerSocket creat es a new Socket on an unspecified port and ret urns a
reference t o t his new Socket . A TCP connect ion now exist s bet ween t he client and server, and
communicat ion can begin.
public So cket(String ho st, int po rt, InetAddress lo calAddress, int lo calPo rt)
thro ws IOExceptio n.
Connect s t o t he specified host and port , creat ing a socket on t he local host at t he
specified address and port .
public So cket()
Creat es an unconnect ed socket . Use t he connect () met hod t o connect t his socket t o a
server.
When t he Socket const ruct or ret urns, it does not simply inst ant iat e a Socket object but it act ually
at t empt s t o connect t o t he specified server and port .
Some met hods of int erest in t he Socket class are list ed here. Not ice t hat bot h t he client and server
have a Socket object , so t hese met hods can be invoked by bot h t he client and server.
SN
1
String to String()
Convert s t his IP address t o a St ring.
{
serverSocket = new ServerSocket(port);
serverSocket.setSoTimeout(10000);
}
public void run()
{
while(true)
{
try
{
System.out.println("Waiting for client on port " +
serverSocket.getLocalPort() + "...");
Socket server = serverSocket.accept();
System.out.println("Just connected to "
+ server.getRemoteSocketAddress());
DataInputStream in =
new DataInputStream(server.getInputStream());
System.out.println(in.readUTF());
DataOutputStream out =
new DataOutputStream(server.getOutputStream());
out.writeUTF("Thank you for connecting to "
+ server.getLocalSocketAddress() + "\nGoodbye!");
server.close();
}catch(SocketTimeoutException s)
{
System.out.println("Socket timed out!");
break;
}catch(IOException e)
{
e.printStackTrace();
break;
}
}
}
public static void main(String [] args)
{
int port = Integer.parseInt(args[0]);
try
{
Thread t = new GreetingServer(port);
t.start();
}catch(IOException e)
{
e.printStackTrace();
}
}
}