Wednesday, April 18, 2018

Socket Programming in Java

Socket Programming in Java:

Steps:
1) Run the Server program.
2) Run the Client program.
3) Type anything on Client to Send. Type type Over to exit.

//Socket for Server
package socket;

import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class SocketServer {
    public static void main(String[] args) throws IOException {
        ServerSocket server = new ServerSocket(1101);  //Server makes a new Socket to communicate with the client and starts Listening.
        System.out.println("Server started.");
        Socket sk = server.accept();  //accept() method blocks(just sits) until a client connects to the server.
        System.out.println("Client connected to Server Socket.");
        
        //Takes input from the client socket
        InputStream is = sk.getInputStream();
        DataInputStream dis = new DataInputStream(new BufferedInputStream(is));
        //DataInputStream dis = new DataInputStream(is);  //This will also work. BufferedInputStream is not required here.

        //String line = dis.readLine();  //readLine() displays some junk too
        String line = dis.readUTF();   //No junk
        System.out.println("Server received: "+line);
        
        
        //Reads message from client until "Over" is sent.
        while (!line.equals("Over")) {
            try {
                line = dis.readUTF();
                System.out.println("Server Received: "+line);
            }
            catch(IOException ioe) {
                ioe.printStackTrace();
            }
        }
        
        //Close connection
        sk.close();
        dis.close();    
    }
}


//Socket for Client
package socket;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;

public class SocketClient {
    public static void main(String[] args) throws IOException {
        Socket sk = new Socket("127.0.0.1", 1101);    //If wrong config: java.net.ConnectException: Connection refused    
        System.out.println("Client got connected to Server.");

        String msg = "Hello Socket";

        DataOutputStream out = new DataOutputStream(sk.getOutputStream());
        out.writeUTF(msg);  //Sends out to the socket

        out.flush();  //This says, I have no more data.
        System.out.println("Client Sent: "+msg);

        String line="";
        DataInputStream dis = new DataInputStream(System.in); 
        while (!line.equals("Over")) {
            try {
                line = dis.readLine();
                out.writeUTF(line);
                System.out.println("Client Sent: "+line);
            }
            catch(IOException i) {
                i.printStackTrace();
            }
        }
        try {
            out.close();
            sk.close();
        } catch(IOException i) {
            System.out.println(i);
            i.printStackTrace();
        }
    }   
}

NOTE:
Run Server, Run Client--> Works fine.
Run Server, Run Multiple Clients..... --> type messages in all clients, Server will receive msges from first Connected Client Only. 
Server will ignore other clients than the first connected clients. This shows Socket is one-to-one connectivity only.

No comments:

Post a Comment