wood burning stoves 2.0*
The moose likes Sockets and Internet Protocols and the fly likes Chatting Applet  Problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "Chatting Applet  Problem" Watch "Chatting Applet  Problem" New topic
Author

Chatting Applet Problem

Ali Hassaan
Ranch Hand

Joined: May 16, 2001
Posts: 103
can anyone pls tell me wats wrong in it.
its works fine for first time when i send data to client recieve
next client hangs. And when i close server client gives the error Read Failed
server)
import java.io.*;
import java.util.*;
import java.net.*;
public class server extends Thread{
ServerSocket server_socket;
Socket client;BufferedReader in;
PrintWriter out;
Thread t;
String msg;
public server() {
try{
server_socket=new ServerSocket(4444);
while (true)
{ try{
client=server_socket.accept();
in = new BufferedReader(new InputStreamReader(client.getInputStream()));
out = new PrintWriter(client.getOutputStream(),true);
msg=in.readLine();
System.out.print(msg);
out.println(msg);
} catch(Exception error){
System.out.print(error);
}
}
}
catch(Exception error){
System.out.print(error);
}
}
public static void main(String[] args) {
server vServer = new server();
}
}
Ryan Langley
Ranch Hand

Joined: May 10, 2001
Posts: 46
Hello,
Without knowing anything else about the program you are writing other than the server it is difficult to determine what you are trying to do and why exactly something is going wrong. It appears though that there are a few things wrong with the server code for accepting multiple connections and processing multiple clients. It appears that when you accept a connection from a second client, your code loses it's i/o stream connections with the other client. To rememdy this you have to either keep the i/o streams to a particular client in an array, or send a copy of the i/o connections to a separate thread to worry about processing them. Here is the server modifications I made to get multiple clients to connect to and talk to the server. Note that this does not care about sending data from one client to another, just to the server. I also am including the test client I created to help you get a better understanding as to how the server is working.
Server:
<pre>
import java.io.*;
import java.net.*;
public class server extends Thread{
ServerSocket server_socket;
Socket client;
public server() {
try {
server_socket=new ServerSocket(4444);
while (true) {
try {
client = server_socket.accept();
ProcessClient pc = new ProcessClient(client.getInputStream(), client.getOutputStream());
pc.start();
} catch(Exception error) {
System.out.println("Error while accepting sockets: " + error.getMessage());
System.exit(0);
}
}
} catch(Exception error) {
System.out.println("Error while creating server socket: " + error.getMessage());
System.exit(0);
}
}
public static void main(String[] args) {
server vServer = new server();
}
}
class ProcessClient extends Thread {
BufferedReader in;
PrintWriter out;
String msg;
public ProcessClient(InputStream is, OutputStream os) {
super();
in = new BufferedReader(new InputStreamReader(is));
out = new PrintWriter(os,true);
}
public void run() {
while (true) {
try {
msg=in.readLine();
System.out.println(msg);
out.println(msg);
} catch(Exception error) {
System.out.println("Error processing clients: " + error.getMessage());
System.exit(0);
}
}
}
}
</pre>
Client:
<pre>
import java.io.*;
import java.net.*;
public class client extends Thread {
Socket client;
BufferedReader keyin;
BufferedReader in;
PrintWriter out;
String keyinput;
String msg;
public client() {
try {
client=new Socket("127.0.0.1",4444);
keyin = new BufferedReader(new InputStreamReader(System.in));
in = new BufferedReader(new InputStreamReader(client.getInputStream()));
out = new PrintWriter(client.getOutputStream(),true);
while (true) {
try {
keyinput = keyin.readLine();
out.println(keyinput);
msg=in.readLine();
System.out.println(msg);
} catch(Exception error) {
System.out.println("Error with client I/O to server: " + error.getMessage());
}
}
} catch(Exception error) {
System.out.println("Error initializing client: " + error.getMessage());
}
}
public static void main(String[] args) {
client vClient = new client();
}
}
</pre>


Cheers,<P>RL
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Chatting Applet Problem
 
Similar Threads
simple chat Applet, please see the code inside.
Null pointer exception probably due to synchronisation issues; problem in starting activity
Java Server limit
applet
simple chat Applet. kindly see the code.