File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Sockets and Internet Protocols and the fly likes little chat program Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "little chat program" Watch "little chat program" New topic
Author

little chat program

reehan ishaque
Ranch Hand

Joined: Mar 25, 2001
Posts: 54
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

Joined: May 10, 2001
Posts: 46
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>


Cheers,<P>RL
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: little chat program
 
Similar Threads
client-serer...plz help
Chat At Server Part Both write and read at the same time?
chat program....need help
accessing a server using socket connection
Problem in file transfer