source file name: Server1.java 

import java.io.*;  

import java.net.*;  

public class Server1

 {  

public static void main(String[] args)

{  

try

{  

ServerSocket ss=new ServerSocket(1111);  

Socket s=ss.accept();//establishes connection   

DataOutputStream dout=new DataOutputStream(s.getOutputStream());  

System.out.println("now server send message to client");

String msg="Good Morning";

dout.writeBytes(msg);  

dout.flush();  

dout.close(); 

ss.close();  

}

catch(Exception e){System.out.println(e);}  

}  

}  


source file name: Client1.java

import java.io.*;  

import java.net.*;  

public class Client1

 {  

public static void main(String[] args) {  

try{      

Socket s=new Socket("localhost",1111);  

DataInputStream dis=new DataInputStream(s.getInputStream());  

String  str=dis.readLine();  

System.out.println(" message from server.. \n"+str);  

s.close();  

}catch(Exception e){System.out.println(e);}  

}  

}  

output:

open two dos windows, one for Server program and second for Client program

use control+C to close session.

Client window output:

D:\JavaPrograms\Networking>javac Client1.java

D:\JavaPrograms\Networking>java Client1

 message from server..

Good Morning


Server window output:

D:\JavaPrograms\Networking>javac Server1.java

D:\JavaPrograms\Networking>java Server1

now server send message to client