Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

little chat program

 
reehan ishaque
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi!
im new to this client/server programing using socket/serversockt i developed a little chat program using BufferedReader n PrtinWriter streams. im using readLine for reading both from keyboard n port. here is the program, its not working. plz tell me wat kinda modification need to be done here.
server)
import java.net.*;
import java.io.*;
public class Server {
public static void main(String args[])throws Exception{
ServerSocket ss=new ServerSocket(2000);
Socket s;
BufferedReader r;
BufferedReader r1;
System.out.println("i am server........ waiting for client......");
s=ss.accept();
r1=new BufferedReader(new InputStreamReader(s.getInputStream()));
r=new BufferedReader(new InputStreamReader(System.in));
PrintWriter p = new PrintWriter(new BufferedWriter(
new OutputStreamWriter(s.getOutputStream())),true);
while(true)
{
String s1=r.readLine();
String s2=r1.readLine();
if(s1.equals("stop"))
break;
System.out.println(s2);
p.println(s1);
}

s.close();
p.close();
}
}
client)
import java.net.*;
import java.io.*;
public class Client{
public static void main(String args[])throws Exception{
BufferedReader r;
BufferedReader r1;
Socket s = new Socket("127.0.0.1",2000);

PrintWriter p = new PrintWriter(new BufferedWriter(
new OutputStreamWriter(s.getOutputStream())),true);

r1=new BufferedReader(new InputStreamReader(s.getInputStream()));
r=new BufferedReader(new InputStreamReader(System.in));
while(true)
{
String s1=r.readLine();
String s2=r1.readLine();

if(s1.equals("stop"))
break;

System.out.println(s2);
p.println(s1);
}

s.close();
p.close();
}
}
 
Ryan Langley
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
Your main problem in this code is that you are attempting to read from both ends of the socket at the same time. Nothing is ever written to the socket in your code.. Here's a simplistic chat program I made from your code example. This is a "CB" style chat.. In other words, only one person can chat at a time, and they must alternate turns. You will probably have to use threading to update all clients connected to the server whenever someone chats. Good luck!
Cheers,
RL
Here is server:
<pre>
import java.net.*;
import java.io.*;
public class Server {
public static void main(String args[])throws Exception{
ServerSocket ss = new ServerSocket(2000);
Socket s;

System.out.println("i am server........ waiting for client......");

s = ss.accept();

ObjectOutputStream sockwrite = new ObjectOutputStream(s.getOutputStream());
ObjectInputStream sockread = new ObjectInputStream(s.getInputStream());

BufferedReader keyread = new BufferedReader(new InputStreamReader(System.in));
System.out.println("CONNECT");
String sockIn;
String keyIn;
while(true) {
sockIn = (String)sockread.readObject();

if(sockIn.equals("stop")) {
System.out.println("CLIENT LEFT");
break;
}
System.out.println(sockIn);

keyIn = keyread.readLine();
sockwrite.writeObject(keyIn);

if(keyIn.equals("stop")) break;
}

s.close();
}
}
</pre>
Here is client:
<pre>
import java.net.*;
import java.io.*;
public class Client{
public static void main(String args[])throws Exception{
Socket s = new Socket("127.0.0.1",2000);
ObjectOutputStream sockwrite = new ObjectOutputStream(s.getOutputStream());
ObjectInputStream sockread = new ObjectInputStream(s.getInputStream());

BufferedReader keyread = new BufferedReader(new InputStreamReader(System.in));

System.out.println("CONNECT");

String keyIn;
String sockIn;
while(true) {
keyIn = keyread.readLine();
sockwrite.writeObject(keyIn);

if(keyIn.equals("stop")) break;

sockIn = (String)sockread.readObject();

if(sockIn.equals("stop")) {
System.out.println("SERVER LEFT");
break;
}
System.out.println(sockIn);
}

s.close();
}
}
</pre>
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic