aspose file tools*
The moose likes Sockets and Internet Protocols and the fly likes Client server communication programme Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "Client server communication programme" Watch "Client server communication programme" New topic
Author

Client server communication programme

Ravi kapa
Ranch Hand

Joined: Jan 05, 2012
Posts: 87
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

Joined: Oct 29, 2001
Posts: 8873
    
    8

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.


"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
Ravi kapa
Ranch Hand

Joined: Jan 05, 2012
Posts: 87
Thanks for the suggestion joe.....i will try with RMI...
 
jQuery in Action, 2nd edition
 
subject: Client server communication programme