Wednesday 9 March 2016

Java Socket Programming

Java Socket Programming

A socket is one end-point of a two-way communication link between two programs running on the network. Socket classes are used to represent the connection between a client program and a server program, when you are writing a client-server application.

TCP/IP and UDP/IP communications

There are two communication protocols that one can use for socket programming: datagram communication and stream communication.

Datagram communication:

The datagram communication protocol(UDP) is a connection less protocol, 
meaning that each time you send datagrams, you also need to send the local socket descriptor and the receiving socket's address. Means, additional data must be sent each time a communication is made.

Stream communication:

The stream communication protocol is known as TCP (transfer control protocol). 
TCP is a connection-oriented protocol. In order to do communication over the TCP protocol, a connection must first be established between the pair of sockets. Once two sockets have been connected, they can be used to transmit data in both directions. 


Example of Java Socket Programming 

Server:

    class SampleServer{  
    public static void main(String args[])throws Exception{  
    ServerSocket serverSocket=new ServerSocket(1234);  
    Socket socket=serverSocket.accept();  
    DataOutputStream dataOuSt=new DataOutputStream(scoket.getOutputStream());  
DataInputStream dataInSt=new DataInputStream(scoket.getInputStream()); BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); String string="";
    String string2="";  
    while(!string.equals("stop")){  
    string=dataInST.readUTF();  
    System.out.println("Message: "+string);  
    string2=br.readLine();  
    dataOuSt.writeUTF(string2);  
    dataOuSt.flush();  
    }  
    dataInSt.close();  
    socket.close();  
    serverSocket.close();  

    }}  

Client:

    class SampleClient{  
    public static void main(String args[])throws Exception{  
    Socket socket=new Socket("localhost",1234);  
    DataInputStream dataInSt=new DataInputStream(socket.getInputStream());  
    DataOutputStream dataOuSt=new DataOutputStream(socket.getOutputStream());  
    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));  
      
    String string="";
    String string2="";  
    while(!string.equals("stop")){  
    string=br.readLine();  
    dataOuSt.writeUTF(string);  
    dataOuSt.flush();  
    string2=dataInSt.readUTF();  
    System.out.println("Message: "+string2);  
    }  
      
    dataOuSt.close();  
    socket.close();  
    }}  

------------------------------




2 comments:

  1. how to send float values/ an group of float values between client and server (eg. numbers 255.0,34.0..etc) using java socket programming

    ReplyDelete
    Replies
    1. It is bit tricky! You have to use byte[] for better group messaging.

      Here you go with sample program!.

      http://www.j2eeschools.com/2016/04/java-socket-send-float-values.html

      Delete