0% found this document useful (0 votes)
60 views3 pages

Aim: Write A Program To Build Client Server Model On Different Computers Code

The code implements a basic client-server model for communication between programs on different computers. The server program listens for client connections on a specified port, accepts a connection, and exchanges messages with the client over input and output streams. The client program connects to the server, sends messages that are printed by the server, and can terminate the connection by sending an "END" message. The programs demonstrate a simple request-response interaction between a client and server.

Uploaded by

sanjay yadav
Copyright
© © All Rights Reserved
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)
60 views3 pages

Aim: Write A Program To Build Client Server Model On Different Computers Code

The code implements a basic client-server model for communication between programs on different computers. The server program listens for client connections on a specified port, accepts a connection, and exchanges messages with the client over input and output streams. The client program connects to the server, sends messages that are printed by the server, and can terminate the connection by sending an "END" message. The programs demonstrate a simple request-response interaction between a client and server.

Uploaded by

sanjay yadav
Copyright
© © All Rights Reserved
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/ 3

Aim: Write a program to build client server model on different

computers
Code:
//Server Program
import java.io.*;
import java.net.*;
import java.util.*;
class Server {
private ServerSocket server;
public InputStream in;
public OutputStream out;
private Socket socket;
public InetAddress ina;
public byte[] bytes;
int fbytes[]=new int[4];
public Server(int port) {
try {
server=new ServerSocket(port);
System.out.println("\nServer waiting for client");
socket=server.accept();
ina=socket.getInetAddress();
bytes=ina.getAddress();
for(int i=0;i<4;i++)
fbytes[i]=bytes[i]&255;
System.out.println(fbytes[0]+"."+fbytes[1]+"."+fbytes[2]+"."+fbytes[3]);
out=socket.getOutputStream();
in=socket.getInputStream(); }
catch(IOException e) {
System.out.println("IOException: "+e); }}}
public class MyServer {
final static int SERVER_PORT=1200;
public static void main(String args[]) {
String req;
Server server;
BufferedReader br; PrintWriter wr;
String msg;
char ch='y';
BufferedReader dis=new BufferedReader(new InputStreamReader(System.in));
server=new Server(SERVER_PORT);
br=new BufferedReader(new InputStreamReader(server.in));
wr=new PrintWriter(new OutputStreamWriter(server.out));
wr.println("Server date information is "+new Date());
wr.flush();
while(true) {
try {
req=br.readLine();
System.out.println("Client says: "+req+"");
if(req.startsWith("Quit")) {
System.out.println("Disconnecting.....");
System.exit(0); }
if(req.startsWith("END")) {
wr.println("Quitting");
wr.flush(); }
else {

System.out.println("send");
System.out.flush();
msg=dis.readLine();
wr.println(msg);
wr.flush(); }}
catch(SocketException e) {}
catch(IOException e) {}
}}}
Output
Server waiting for client
Client says: Hi
send
Hello
Client says: END
Disconnecting..

//Client Program
import java.io.*;
import java.net.*;
class Client {
public InputStream in;
public OutputStream out;
private Socket client;
public Client(String host,int port) {
try {
client=new Socket(host,port);
System.out.println("Client requesting to the server");
out=client.getOutputStream();
in=client.getInputStream(); }
catch(IOException e) {
System.out.println("IOException: "+e);}}}
public class MyClient {
public static void main(String args[]) {
String start,res;
Client client;
BufferedReader br;
PrintWriter wr;
String msg;
char ch='y';
BufferedReader dis=new BufferedReader(new InputStreamReader(System.in));
client=new Client("",1200);
do {
try {
br=new BufferedReader(new InputStreamReader(client.in));
wr=new PrintWriter(new OutputStreamWriter(client.out));
start=br.readLine();
System.out.println("Please enter the text");
msg=dis.readLine();
System.out.println("I am sending "+msg);
wr.println(msg);
wr.flush();
res=br.readLine();

System.out.println("Server responds: "+res+"");


wr.println("END");
wr.flush(); }
catch(IOException e) {
System.out.println("IO Exception: "+e);}
System.out.println("Do you want to send more information?");
try {
ch=(char)dis.read();}
catch(IOException e) {
System.out.println("IOException: "+e);
System.exit(0);
}}while(ch!='n');
}}
Output:
Client requesting to the server
Please enter the text
Hi
I am sending Hi
Server responds: Hello
Do you want to send more information?
n

You might also like