• 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

Chatting Applet Problem

 
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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();
}
}
 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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>
 
Die Fledermaus does not fear such a tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic