source file name: MyClient1.java
import java.net.*;
import java.io.*;
class MyClient1{
public static void main(String args[])throws Exception{
Socket s=new Socket("localhost",3333);
DataInputStream din=new DataInputStream(s.getInputStream());
DataOutputStream dout=new DataOutputStream(s.getOutputStream());
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("wait server send message..");
String str="",str2="";
while(!str.equals("stop"))
{
System.out.println("enter message");
str=br.readLine();
dout.writeUTF(str);
dout.flush();
str2=din.readUTF();
System.out.println("Server says: "+str2);
}
dout.close();
s.close();
}
}
source file name: MyServer1.java
import java.net.*;
import java.io.*;
class MyServer1{
public static void main(String args[])throws Exception{
ServerSocket ss=new ServerSocket(3333);
Socket s=ss.accept();
DataInputStream din=new DataInputStream(s.getInputStream());
DataOutputStream dout=new DataOutputStream(s.getOutputStream());
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("wait client send message..");
String str="",str2="";
while(!str.equals("stop"))
{
System.out.println("enter message");
str=din.readUTF();
System.out.println("client says: "+str);
str2=br.readLine();
dout.writeUTF(str2);
dout.flush();
}
din.close();
s.close();
ss.close();
}
}
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 MyClient1.java
D:\JavaPrograms\Networking>java MyClient1
wait server send message..
enter message
hello students
Server says: take care and stay safe
enter message
D:\JavaPrograms\Networking>java MyServer1
wait client send message..
enter message
client says: hello students
take care and stay safe
enter message
0 Comments
Post a Comment