source file name: MyServer.java 

import java.io.*;  

import java.net.*;  

public class MyServer

 {  

public static void main(String[] args)

{  

try

{  

ServerSocket ss=new ServerSocket(6666);  

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

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

String  str=dis.readLine();  

System.out.println(" message from clinet "+str);  

ss.close();  

}

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

}  

}  


source file name: MyClient.java

import java.io.*;  

import java.net.*;  

public class MyClient {  

public static void main(String[] args) {  

try{      

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

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

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

String msg="Have a nice day..";

dout.writeBytes(msg);  

dout.flush();  

dout.close();  

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 MyServer.java

D:\JavaPrograms\Networking>java MyClient

now clients send message to server


server window output

D:\JavaPrograms\Networking>java MyServer

message from clinet Have a nice day..