Program 9 (Java)


9.    Using TCP/IP sockets, write a client – server program to make the client send the file name and to make the server send back the contents of the requested file if present.

      Socket is an interface which enables the client and the server to communicate and pass on information from one another. Sockets provide the communication mechanism between two computers using TCP. A client program creates a socket on its end of the communication and attempts to connect that socket to a server. When the connection is made, the server creates a socket object on its end of the communication. The client and the server can now communicate by writing to and reading from the socket.
Source Code: Server


Server.java

package server;

import java.net.*;

import java.io.*;



public class Server {

    public static void main(String[] args)throws IOException {

       try

        {

            //The server creates a ServerSocket object, denoting which port number communication is to occur on.

            ServerSocket sersock=new ServerSocket(4000);

            System.out.println("server ready for connection");



            /*The server invokes the accept() method of the ServerSocket class. This method waits until a client   connects to the server on the given port.*/

            Socket sock=sersock.accept();

            System.out.println("connection established waiting for chatting");





            // reading the file name from client

            InputStream istream=sock.getInputStream();

            BufferedReader fileRead=new BufferedReader(new InputStreamReader(istream));

            String fname=fileRead.readLine();

           

            // reading file contents

            BufferedReader ContentRead=new BufferedReader(new FileReader(fname));



            // keeping output stream ready to send the contents

            OutputStream ostream=sock.getOutputStream();

            PrintWriter pwrite=new PrintWriter(ostream,true);

            String str;

            while((str=ContentRead.readLine())!=null) // reading line-by-line from file

            {

                pwrite.println(str); // sending each line to client

            }



            // closing network sockets

            sock.close();

            sersock.close();

            pwrite.close();

            fileRead.close(); 

        }

        catch(Exception e)

        {

            System.out.println("an error occured while opening");

        }

    }

 }

Client.java

package client;

import java.net.*;

import java.io.*;



public class Client {

    public static void main(String[] args)throws Exception {

       //client creates a Socket object, specifying the server name and the port number to connect to.

        Socket sock=new Socket("127.0.0.1",4000);



        System.out.println("enter file name");

        /*reading from keyboard (keyRead object)using input stream  connecting the BufferedReader stream with the InputStreamReader stream for reading the line by line data from the keyboard.*/

        //or as commented below use scanner object

       BufferedReader keyread=new BufferedReader(new InputStreamReader(System.in));

       String fname=keyread.readLine();

      //  Scanner scan=new Scanner(System.in);

      //  String fname=scan.nextLine();

       

try{

             // sending the file name to server. Uses PrintWriter

            /*getOutputStream(). This method returns an OutputStream where the data can be written and throws the appropriate exception if it cannot do so.*/

            OutputStream ostream=sock.getOutputStream();

            PrintWriter pwrite=new PrintWriter(ostream,true);

            pwrite.println(fname);

           

            // receiving the contents from server.  Uses input stream

            /*This method returns an InputStream representing the data and throws the appropriate exception if it cannot do so.*/

            InputStream istream=sock.getInputStream();

            BufferedReader socketRead=new BufferedReader(new InputStreamReader(istream));



            String str;

            // reading line-by-line

            while((str=socketRead.readLine())!=null)

            {

                System.out.println(str);

            }

            // closing network sockets

            pwrite.close();             socketRead.close();             keyread.close();

          }

        catch(Exception e)

        {

            System.out.println("an error occured while opening");

        }

    }

}   

No comments:

Post a Comment