0% found this document useful (0 votes)
78 views6 pages

NPM EX123

The document contains code for several Java programs that demonstrate client-server communication: - Dateclient.java and Dateserver.java show a client getting the date from a server and sending its address back. - EchoClient.java and EchoServer.java demonstrate a simple echo client-server that reflects input back to the client. - Clientchat.java and Serverchat.java implement a basic chat client and server that allow two programs to exchange text messages over a network connection.

Uploaded by

vidhyasiva83
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
78 views6 pages

NPM EX123

The document contains code for several Java programs that demonstrate client-server communication: - Dateclient.java and Dateserver.java show a client getting the date from a server and sending its address back. - EchoClient.java and EchoServer.java demonstrate a simple echo client-server that reflects input back to the client. - Clientchat.java and Serverchat.java implement a basic chat client and server that allow two programs to exchange text messages over a network connection.

Uploaded by

vidhyasiva83
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Program: Dateclient.

java
import java.net.*; import java.io.*; class dateclient { public static void main(String args[]) { Socket soc; DataInputStream dis; String sdate; PrintStream ps; try { InetAddress ia=InetAddress.getLocalHost(); soc=new Socket(ia,8020); dis=new DataInputStream(soc.getInputStream()); sdate=dis.readLine(); System.out.println("The date in the server is:"+sdate); ps=new PrintStream(soc.getOutputStream()); ps.println(ia); } catch(IOException e) { System.out.println("THE EXCEPTION is:"+e); } } }

Program: Dateserver.java
import java.net.*; import java.io.*; import java.util.*; class dateserver { public static void main(String args[]) { ServerSocket ss; Socket s; PrintStream ps; DataInputStream dis; String inet; try { ss=new ServerSocket(8020); while(true) { s=ss.accept(); ps=new PrintStream(s.getOutputStream()); Date d=new Date(); ps.println(d); dis=new DataInputStream(s.getInputStream()); inet=dis.readLine(); System.out.println("THE CLIENT SYSTEM ADDRESS IS: "+inet); ps.close(); }} catch(IOException e) { System.out.println("The exception is:"+e); } } }

Program: EchoClient.java
import java.io.*; import java.net.*; public { public static void main(String args[]) { String sockin; try { Socket csok=new Socket(InetAddress.getLocalHost(),2000); BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); BufferedReader br_sock=new BufferedReader(new InputStreamReader(csok.getInputStream())); PrintStream ps=new PrintStream(csok.getOutputStream()); System.out.println("Start Echoing....Type 'end' to terminate"); while((sockin=br.readLine( ))!=null) { ps.println(sockin); if(sockin.equals("end")) break; else System.out.println("Reflected from Server:"+br_sock.readLine()); } } catch(UnknownHostException e) { System.out.println(e.toString()); } catch(IOException ioe){ System.out.println(ioe); } } } class EchoClient

Program: EchoServer.java
import java.io.*; import java.net.*; public class EchoServer { public static void main(String args[]) { String echoin="0"; ServerSocket ssock; Socket csok; BufferedReader br; try { ssock=new ServerSocket(2000); csok=ssock.accept(); br=new BufferedReader(new InputStreamReader(csok.getInputStream())); PrintStream ps=new PrintStream(csok.getOutputStream()); System.out.println("Connected........for echo"); while((echoin=br.readLine( ))!=null) { if(echoin.equals("end")) { System.out.println("Client disconnected"); System.out.println(echoin); br.close(); break; } else ps.println(echoin); } } catch(UnknownHostException e) { System.out.println(e.toString()); } catch(IOException ioe) { System.out.println(ioe); } } }

Program: Clientchat.java
import java.io.*; import java.net.*; public class cchat { public static void main(String[] args) throws Exception { Socket sock = new Socket("127.0.0.1", 3000); // reading from keyboard (keyRead object) BufferedReader keyRead = new BufferedReader(new InputStreamReader(System.in)); // sending to client (pwrite object) OutputStream ostream = sock.getOutputStream(); PrintWriter pwrite = new PrintWriter(ostream, true); // receiving from server ( receiveRead InputStream istream = sock.getInputStream(); BufferedReader receiveRead = new BufferedReader(new InputStreamReader(istream)); System.out.println("Start the chitchat, type and press Enter key"); String receiveMessage, sendMessage; while(true) { sendMessage = keyRead.readLine(); pwrite.println(sendMessage); System.out.flush(); //receive from server { System.out.println(receiveMessage); // displaying at DOS prompt } } } } // keyboard reading // sending to server object)

// flush the data

if((receiveMessage = receiveRead.readLine()) != null)

Program: Serverchat.java
import java.io.*; import java.net.*; public class schat { public static void main(String[] args) throws Exception { ServerSocket sersock = new ServerSocket(3000); System.out.println("Server ready for chatting"); Socket sock = sersock.accept( ); // reading from keyboard (keyRead object) BufferedReader keyRead = new BufferedReader(new InputStreamReader(System.in)); // sending to client (pwrite object) OutputStream ostream = sock.getOutputStream(); PrintWriter pwrite = new PrintWriter(ostream, true); // receiving from server ( receiveRead InputStream istream = sock.getInputStream(); BufferedReader receiveRead = new BufferedReader(new InputStreamReader(istream)); String receiveMessage, sendMessage; while(true) { if((receiveMessage = receiveRead.readLine( )) != null) { System.out.println(receiveMessage); } sendMessage = keyRead.readLine(); pwrite.println(sendMessage); System.out.flush(); } } } object)

You might also like