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.
Here, we are going to make one-way client and server
communication. In this application, client sends a message to the server,
server reads the message and prints it. Here, two classes are being used:
Socket and ServerSocket. The Socket class is used to communicate client and
server. Through this class, we can read and write message. The ServerSocket
class is used at server-side. The accept() method of ServerSocket class blocks
the console until the client is connected. After the successful connection of
client, it returns the instance of Socket at server-side.
1.
SERVERSIDECODE
2.
public static void main(String[] args){
3.
try{
4.
ServerSocket ss=new ServerSocket(6666);
5.
Socket s=ss.accept();//establishes connection
6.
DataInputStream dis=new DataInputStream(s.getInputStream());
7.
String str=(String)dis.readUTF();
8.
System.out.println("message= "+str);
9.
ss.close();
10. }catch(Exception e){System.out.println(e);}
11. }
12. }
1.
CLIENT SIDE CODE
2.
try{
3.
Socket s=new Socket("localhost",6666);
4.
DataOutputStream dout=new DataOutputStream(s.getOutputStream());
5.
dout.writeUTF("Hello Server");
6.
dout.flush();
7.
dout.close();
8.
s.close();
9.
}catch(Exception e){System.out.println(e);}
10. }
Rif if you run the above program client send some message to server which will be displayed at server side
Example of Java Socket Programming
(Read-Write both side)
In this example, client will
write first to the server then server will receive and print the text. Then
server will write to the client and client will receive and print the text. The
step goes on.
1.
import java.net.*;
2.
import java.io.*;
3.
class MyServer{
4.
public static void main(String args[])throws Exception{
5.
ServerSocket ss=new ServerSocket(3333);
6.
Socket s=ss.accept();
7.
DataInputStream din=new DataInputStream(s.getInputStream());
8.
DataOutputStream dout=new DataOutputStream(s.getOutputStream());
9.
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
10.
11. String str="",str2="";
12. while(!str.equals("stop")){
13. str=din.readUTF();
14. System.out.println("client says: "+str);
15. str2=br.readLine();
16. dout.writeUTF(str2);
17. dout.flush();
18. }
19. din.close();
20. s.close();
21. ss.close();
22. }}
import java.net.*;
import java.io.*;
class MyClient{
public static void main(String args[])throws Exception{
Socket s=new Socket("localhost",3333);
DataInputStream din=new DataInputStream(s.getInputStream());
DataOutputStream dout=new DataOutputStream(s.getOutputStream());
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String str="",str2="";
while(!str.equals("stop")){
str=br.readLine();
dout.writeUTF(str);
dout.flush();
str2=din.readUTF();
System.out.println("Server says: "+str2);
}
dout.close();
s.close();
}}
CONNECTION LESS SERVICE IMPLEMENTATION IN JAVA
Java DatagramSocket
and DatagramPacket
Java DatagramSocket and DatagramPacket classes are used for
connection-less socket programming using the UDP instead of TCP.
Datagram
Datagrams are collection of information sent from one device to
another device via the established network. When the datagram is sent to the
targeted device, there is no assurance that it will reach to the target device
safely and completely. It may get damaged or lost in between. Likewise, the
receiving device also never know if the datagram received is damaged or not.
The UDP protocol is used to implement the datagrams in Java.
Java DatagramSocket class
Java DatagramSocket class represents a connection-less socket for sending and
receiving datagram packets. It is a mechanism used for transmitting datagram
packets over network.`
A datagram is basically an information but there is no guarantee
of its content, arrival or arrival time.
Example of Sending DatagramPacket by
DatagramSocket
1.
//DSender.java
2.
import java.net.*;
3.
public class DSender{
4.
public static void main(String[] args) throws Exception {
5.
DatagramSocket ds = new DatagramSocket();
6.
String str = "Welcome java";
7.
InetAddress ip = InetAddress.getByName("127.0.0.1");
8.
9.
DatagramPacket dp = new DatagramPacket(str.getBytes(), str.length(), ip, 3000);
10. ds.send(dp);
11. ds.close();
12. }
13. }
Example of Receiving DatagramPacket by
DatagramSocket
1.
//DReceiver.java
2.
import java.net.*;
3.
public class DReceiver{
4.
public static void main(String[] args) throws Exception {
5.
DatagramSocket ds = new DatagramSocket(3000);
6.
byte[] buf = new byte[1024];
7.
DatagramPacket dp = new DatagramPacket(buf, 1024);
8.
ds.receive(dp);
9.
String str = new String(dp.getData(), 0, dp.getLength());
10. System.out.println(str);
11. ds.close();
12. }
13. }
కామెంట్లు లేవు:
కామెంట్ను పోస్ట్ చేయండి