Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Client server communication programme

 
Ravi kapa
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,i wrote code that coomunicate 2 services(like client server chating )both client and server are run in different dos prompt in the same computer..client and server programmes are compiled and executed and also connected but chating with thease client n server is not getting....can anyone help me??

this is my client pgm code
----------------------------------
import java.io.*;
import java.net.*;
class Client1
{
public static void main(String args[]) throws IOException
{
Socket s=new Socket("localhost",999);

DataOutputStream dos=new DataOutputStream(s.getOutputStream());
BufferedReader br=new BufferedReader(new InputStreamReader(s.getInputStream()));
BufferedReader kb=new BufferedReader(new InputStreamReader(System.in));

String str1,str2;
while(!(str1=kb.readLine()).equals("exit"))
{
dos.writeBytes(str1);
str2=br.readLine();
System.out.println(str2);

}
dos.close();
br.close();
kb.close();
s.close();

}
}

This is my server pgm code
--------------------------------------
import java.io.*;
import java.net.*;
class Server1
{
public static void main(String args[]) throws IOException
{
ServerSocket ss=new ServerSocket(999);
Socket s=ss.accept();
System.out.println("Connection established");
PrintStream ps=new PrintStream(s.getOutputStream());

BufferedReader br=new BufferedReader(new InputStreamReader(s.getInputStream()));
BufferedReader kb=new BufferedReader(new InputStreamReader(System.in));

while(true)
{
String str1,str2;
while((str1=br.readLine())!=null)
{
System.out.println(str1);
str2=kb.readLine();
ps.println(str2);

}


ps.close();
br.close();
kb.close();
s.close();
ss.close();
}}
}





 
Joe Ess
Bartender
Pie
Posts: 9258
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try reading through the Java Tutorial on Sockets. Sockets are tricky to work with because some platform-specific traps and it takes work to get a protocol just right. I did not look at your code closely, but it looks like you would have to execute a specific sequence of input on both the client and the server to get it to work properly. A correct client/server chat pair would use threads to send and receive messages simultaneously.
In my opinion, Remote Method Invocation (RMI) is much easier to work with as it abstracts away the details of working with sockets.
 
Ravi kapa
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the suggestion joe.....i will try with RMI...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic