• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Client server communication programme

 
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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();
}}
}





 
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 128
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the suggestion joe.....i will try with RMI...
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic