Simple Client Server Program:
Client Program:
client.java
______________________________________________________________________________
import java.io.*;
import java.net.*;
class client
{
public static void main(String args[]) throws Exception
{
String sentence,modifiedSentence,inp;
BufferedReader inFromUser = new BufferedReader(new
InputStreamReader(System.in));
Socket s=null;
BufferedReader b=null;
try
{
s=new Socket(InetAddress.getLocalHost(),98);
b=new BufferedReader(new InputStreamReader(s.getInputStream()));
}
catch(UnknownHostException u)
{
System.err.println("I don't know host");
System.exit(0);
}
while((inp=b.readLine())!="\n")
{
System.out.println(inp);
}
DataOutputStream outToServer = new DataOutputStream(s.getOutputStream());
BufferedReader inFromServer = new BufferedReader(new
InputStreamReader(s.getInputStream()));
sentence = inFromUser.readLine();
outToServer.writeBytes(sentence + '\n');
modifiedSentence = inFromServer.readLine();
System.out.println(modifiedSentence);
s.close();
b.close();
}
}
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
import java.io.*;
import java.net.*;
class client
{
public static void main(String args[]) throws Exception
{
String sentence,modifiedSentence,inp;
BufferedReader inFromUser = new BufferedReader(new
InputStreamReader(System.in));
Socket s=null;
BufferedReader b=null;
try
{
s=new Socket(InetAddress.getLocalHost(),98);
b=new BufferedReader(new InputStreamReader(s.getInputStream()));
}
catch(UnknownHostException u)
{
System.err.println("I don't know host");
System.exit(0);
}
while((inp=b.readLine())!="\n")
{
System.out.println(inp);
}
DataOutputStream outToServer = new DataOutputStream(s.getOutputStream());
BufferedReader inFromServer = new BufferedReader(new
InputStreamReader(s.getInputStream()));
sentence = inFromUser.readLine();
outToServer.writeBytes(sentence + '\n');
modifiedSentence = inFromServer.readLine();
System.out.println(modifiedSentence);
s.close();
b.close();
}
}
______________________________________________________________________________
Server Program:
server.java______________________________________________________________________________
import java.io.*;
import java.net.*;
class server
{
public static void main(String args[]) throws Exception
{
ServerSocket n1=null;
try
{
n1=new ServerSocket(98);
}
catch(IOException e)
{
System.err.println("Port 98 could not be found");
System.exit(1);
}
Socket c=null;
try
{
c=n1.accept();
System.out.println("Connection from "+c);
}
catch(IOException e)
{
System.out.println("Accept failed");
System.exit(1);
}
PrintWriter out=new PrintWriter(c.getOutputStream(),true);
BufferedReader in=new BufferedReader(new InputStreamReader(c.getInputStream()));
String n;
BufferedReader sin=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Ready to type now");
while((n=sin.readLine())!="\n")
{
out.println(n);
}
out.close();
c.close();
n1.close();
}
}
______________________________________________________________________________
No comments:
Post a Comment