Socket Programming using TCP/IP------------ Write an application to send “hello” message from server to client using TCP/TP.

Aim: - Write an application to send “hello” message from server to client using TCP/IP.
Tools: - NetBeans IDE, jdk

Program:-

 

Server.java

package srvr;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
public class Srvr {
    public static void main(String[] args) throws Exception {
        ServerSocket sc=new ServerSocket(777);
        Socket s=sc.accept();
        System.out.println("Server is Ready...");
        BufferedReader br=new BufferedReader(new InputStreamReader(s.getInputStream()));
        String str;
        System.out.print("String  From Client : ");
        while((str=br.readLine())!=null)
        {
            System.out.print(str);
        }
        System.out.println();
        s.close();
        br.close();       
    }
}

Client.java

package srvr;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;

public class clnt {
    public static void main(String[] args) throws Exception {       
        Socket s=new Socket("localhost", 777);
        System.out.print("Enter String : ");
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in) );
        String str=br.readLine();
        PrintStream ps=new PrintStream(s.getOutputStream());       
        ps.println(str);
        System.out.println("String Sended To Server...");      
        ps.close();
        s.close();       

    }    

Output:-


Post a Comment

0 Comments