Skip to content

Latest commit

 

History

History

2019_09_03_Java-Class-5

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 

Java Class - 5

Socket Programming

Resources

The following is the code for a basic server.

import java.net.*;
import java.io.*;


public class Server{
    private final static int port = 8888;

    public static void main(String args[]){
        ServerSocket ssock = null;
        try{

            ssock = new ServerSocket(port);

        }catch(Exception e){
            e.printStackTrace();
            System.out.println("Error occured...failed to initialize Server Socket");
        }

        while(true){
            Socket sock = null;
            try{
            	System.out.println("Waiting for connection..!!");
                sock = ssock.accept();
                System.out.println("Connection received...!!!");
               new Thread(new ClientHandler(sock)).start();

            }catch(Exception e){
                e.printStackTrace();
                System.out.println("Failed to connect to client..!!");
            }
        }
    }
}

The following is the code for Client which can connect to the server.

import java.io.*;
import java.net.*;
import java.util.Scanner;


public class Client{
    
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter server IP: ");
        String serverIP = sc.next();
        System.out.print("Enter port: ");
        int port = sc.nextInt();

        Socket sock=null;

        try{
            sock = new Socket(serverIP, port);
            System.out.println(""+sock.getInetAddress());
            System.out.println(""+sock.getPort());

            System.out.println("Connected to the server");

            DataOutputStream dos = new DataOutputStream(sock.getOutputStream());
            DataInputStream dis = new DataInputStream(sock.getInputStream());

            new Thread(new ClientHandler(sock)).start();
            
            while(true){
                String msg = sc.next();
                dos.writeUTF(msg);
            }

        }catch(Exception e){
            e.printStackTrace();
            System.out.println("Cannot connect to server...!!");
        }
    }

}

The following code is the code for ClientHandler

import java.io.*;
import java.net.*;


public class ClientHandler implements Runnable{
    private Socket sock;
    DataInputStream dis;

    ClientHandler(Socket sock){
        this.sock = sock;
    }

    public void run(){

        try{
            dis = new DataInputStream(sock.getInputStream());

            while(true){
                System.out.println("Received message: "+ dis.readUTF());
            }

        }catch(Exception e){
            e.printStackTrace();
            System.out.println("Some error occured");
        }
    }

}

Code

For any queries, feel free to contact us.

  • Abhishek Kumar Singh
  • Surbhit Awasthi
  • Shreyas Dobhal
  • Ashish Gopal
  • Sri Ram Khandelwal
  • Tushar Srivastava
  • Paras Saini
  • Himanshu Purwar
  • Shreshtha Dwivedi
  • Araish Aftab