Client and server communication program

Here is an example, demonstrating how to connect two programs in Java networking



package network;

import java.io.*;
import java.net.*;
import java.util.*;
public class Server1{
    public static void main(String ar[])throws Exception{
        ServerSocket ss=new ServerSocket(1234);
        Socket clientSocket=ss.accept();
        InputStream in=clientSocket.getInputStream();
        OutputStream out=clientSocket.getOutputStream();
        
        Scanner scan=new Scanner(in);
        PrintStream ps=new PrintStream(out);
        
        while(true){
            String s=scan.nextLine();
            System.out.println("Client sent : "+s);
            String str=System.console().readLine("Entr Something : ");
            ps.println(str);
        }
        
    }
}



package network;

import java.io.*;
import java.net.*;
import java.util.*;
public class Client1{
    public static void main(String ar[])throws Exception{
        Socket clientSocket=new Socket("192.168.0.1",1234);
        InputStream in=clientSocket.getInputStream();
        OutputStream out=clientSocket.getOutputStream();
        Scanner scan=new Scanner(in);
        PrintStream ps=new PrintStream(out);
        
        while(true){
            String str=System.console().readLine("Entr Something : ");
            ps.println(str);
            String s=scan.nextLine();
            System.out.println("server reply : "+s);
        }
        
    }
}



No comments:

Popular Posts