Advanced JAVA 6
Java Socket Programming
Java Socket programming is used for communication between the applications running on different JRE.
Java Socket programming can be connection-oriented or connection-less.
Socket and ServerSocket classes are used for connection-oriented socket programming and DatagramSocket and DatagramPacket classes are used for connection-less socket programming.
The client in socket programming must know two information:
1. IP Address of Server, and
2. Port number.
Socket class
Socket is foundation of modern networking, a socket allows single computer to serve many different clients at once.
Socket establishes connection through the use of port, which is a numbered socket on a particular machine.
Socket communication takes place via a protocol. Socket provides communication mechanism between two computers using TCP.
Socket class is for client.
ServerSocket is for servers.
Important methods of Socket class
1) public InputStream getInputStream() : returns the InputStream attached with this socket.
2) public OutputStream getOutputStream() : returns the OutputStream attached with this socket.
3) public synchronized void close() : closes this socket.
ServerSocket Class
ServerSocket is for servers and is used to establish communication with the clients.
Important methods of ServersSocket class
1) public Socket accept( ) : returns the socket and establish a connection between server and client.
2) public synchronized void close( ) : closes the server socket.
Example of Java Socket Programming
Let's see a simple of java socket programming in which client sends a text and server receives it.
//File: MyServer.java
import java.io.*;
import java.net.*;
public class MyServer {
public static void main(String[] args){
try{
ServerSocket ss=new ServerSocket(6666);
Socket s=ss.accept();//establishes connection
DataInputStream dis=new DataInputStream(s.getInputStream());
String str=(String)dis.readUTF();
System.out.println("Message = "+str);
ss.close();
}catch(Exception e){
System.out.println(e);
}
}
}
//File: MyClient.java
import java.io.*;
import java.net.*;
public class MyClient {
public static void main(String[] args) {
try{
Socket s=new Socket("localhost",6666);
DataOutputStream dout=new DataOutputStream(s.getOutputStream());
dout.writeUTF("Hello Server");
dout.flush();
dout.close();
s.close();
}catch(Exception e){
System.out.println(e);
}
}
}
To execute this program open two command prompts and execute each program at each command prompt as,
1) javac MyServer.java
java MyServer
2) javac MyClient.java
java MyClient
After running the client application, a message will be displayed on the server console.
Output: Message = Hello Server
DatagramSocket class
Java DatagramSocket class represents a connection-less socket for sending and receiving datagram packets.
A datagram is basically an information but there is no guarantee of its content, arrival or arrival time.
Commonly used Constructors of DatagramSocket class
1) DatagramSocket() throws SocketEeption : it creates a datagram socket and binds it with the available Port Number on the localhost machine.
2) DatagramSocket(int port) throws SocketEeption : it creates a datagram socket and binds it with the given Port Number.
3) DatagramSocket(int port, InetAddress address) throws SocketEeption : it creates a datagram socket and binds it with the specified port number and host address.
DatagramPacket class
Java DatagramPacket is a message that can be sent or received. If you send multiple packet, it may arrive in any order. Additionally, packet delivery is not guaranteed.
Commonly used Constructors of DatagramPacket class
1) DatagramPacket(byte[] barr, int length) : it creates a datagram packet. This constructor is used to receive the packets.
2)DatagramPacket(byte[] barr, int length, InetAddress address, int port) : it creates a datagram packet. This constructor is used to send the packets.
Example of Sending DatagramPacket by DatagramSocket
//DSender.java
import java.net.*;
public class DSender{
public static void main(String[] args) throws Exception {
DatagramSocket ds = new DatagramSocket();
String str = "Welcome java";
InetAddress ip = InetAddress.getByName("127.0.0.1");
DatagramPacket dp = new DatagramPacket(str.getBytes(), str.length(), ip, 3000);
ds.send(dp);
ds.close();
}
}
Example of Sending DatagramPacket by DatagramSocket
//DReceiver.java
import java.net.*;
public class DReceiver{
public static void main(String[] args) throws Exception {
DatagramSocket ds = new DatagramSocket(3000);
byte[] buf = new byte[1024];
DatagramPacket dp = new DatagramPacket(buf, 1024);
ds.receive(dp);
String str = new String(dp.getData(), 0, dp.getLength());
System.out.println(str);
ds.close();
}
}
Dinesh Kumar S is a 23-year-old System Administrator who enjoys playing games, listening to music and learning new technology. He is friendly and generous, but can also be very lazy and crazy.
Share this
Learn-JAVA