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

small server problem

 
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
I have a problem with this small server im writing. It accepts one person, but when it accpets another person, nothing happends. Its a chat client. The first person to login gets in and is able to type text. The next person isint able to do anything. This could be a problem with the threading but im not sure. can anyone help me out?

here is the code, I think it has something to do with the threading.
import java.io.*;
import java.net.*;
public class SimpleThreadedServer3
{
static final int portNumber = 5095;
private ServerSocket ss;
private InetAddress addr;
private PrintWriter writer;
private String messageIn,messageOut;
private BufferedReader reader;
public static void main(String [] args)
{
new SimpleThreadedServer3().initiateStartUp();
}
public void initiateStartUp()
{
try{
addr = InetAddress.getLocalHost();

System.out.println("Creating Server Socket at:"+portNumber+" . . . ");
System.out.println("At Host:"+ addr.getHostName()+" . . .");
System.out.println("At Address:"+ addr.getHostAddress()+" . . .");
ss = new ServerSocket(portNumber);

System.out.println("System Online ...");
while(true)
{
Socket sock = ss.accept();
writer = new PrintWriter(sock.getOutputStream());
System.out.println(sock+ "I am a white man");
String m = "stinky";
writer.println(m);

new Thread(new ClientHandler(sock)).start();
}

}catch(IOException e)
{
System.out.println(e);
}
}
class ClientHandler implements Runnable
{
private Socket sock;
public ClientHandler(Socket s)
{
try{
sock = s;

reader = new BufferedReader(new InputStreamReader(sock.getInputStream()));
System.out.println(reader+"stinky bear");
String messageIn;
while((messageIn = reader.readLine())!=null)
{
System.out.println(messageIn);
writer.println(messageIn);
}
}catch(Exception e)
{
System.out.println(e);
}
}
public void run()
{
String messageOut;
try{
while((messageOut = reader.readLine())!= null)
{
System.out.println(messageOut);
sendToAll(messageOut);
}
}catch(Exception e)
{
System.out.println(e);
}
}
}
public void sendToAll(String messageOut)
{
try{
writer.println(messageIn);
writer.println(messageOut);
writer.flush();
}catch(Exception e)
{
System.out.println(e);
}
}
}



can anyone help me out?
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Please do not cross-post the same question in multiple forums -- especially forums that really have no relevance to the topic. It wastes people's time when multiple redundant conversations take place.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    Bookmark Topic Watch Topic
  • New Topic