java - Server doesn't work with more than one connection -
i've got working simple client-server app. problem works fine 1 started client, not 2 or more. establish connection, when try enter text in first or second, server breakes. think problem may @ function broadcast()
in server.java
.
server.java
public class server { final int port = 5000; private arraylist<newclient> al = new arraylist<newclient>(); private scanner in; private printwriter out; private simpledateformat sdf; private int uniqueid = 0; private void go(){ try{ serversocket serversocket = new serversocket(port); system.out.println("waiting clients..."); while(true) { socket s = serversocket.accept(); newclient chat = new newclient(s); system.out.println("client number " + chat.getid() + " connected from: " + s.getlocaladdress().gethostname()); al.add(chat); thread t = new thread(chat); t.start(); } }catch (exception e) { system.out.println("problem establishing network connection: "); e.printstacktrace(); } } public static void main(string[] args) { server server = new server(); server.go(); } class newclient implements runnable{ private socket socket; private int id; public newclient(socket s) { this.socket = s; this.id = ++uniqueid; } public int getid() { return this.id; } @override public void run() { try{ in = new scanner(socket.getinputstream()); out = new printwriter(socket.getoutputstream()); sdf = new simpledateformat("hh:mm:ss"); while(true) { string input = in.nextline(); system.out.println("client said: " + input); broadcast(input); } }catch (exception e) { e.printstacktrace(); } } private void writemsg(string input) { string msg = input + " on " + sdf.format(new date()); out.println("you said: " + msg); out.flush(); } private void broadcast(string input) { (int = 0; < al.size(); i++) { newclient t = al.get(i); t.writemsg(input); } } } }
client.java:
public class client { final int port = 5000; final string host = "127.0.0.1"; private scanner stdin; private scanner in; private printwriter out; private void go() { setupnetwork(); } private void setupnetwork(){ try{ socket s = new socket(host, port); system.out.println("you connected " + host); newclient client = new newclient(s); thread t = new thread(client); t.start(); } catch (exception e) { system.out.println("problem connection server: " + e); } } public static void main(string[] args) { client client = new client(); client.go(); } class newclient implements runnable { private socket socket; public newclient(socket s) { this.socket = s; } @override public void run() { try { stdin = new scanner(system.in); in = new scanner(socket.getinputstream()); out = new printwriter(socket.getoutputstream()); while(true) { system.out.print("> "); string input = stdin.nextline(); out.println(input); out.flush(); if(in.hasnext()) { system.out.println(in.nextline()); } } }catch (exception e) { e.printstacktrace(); } } } }
when opens 2 client.java , connect server.java ok. when try send message 2 opened clients server returns these errors:
java.lang.indexoutofboundsexception: end @ java.util.regex.matcher.region(matcher.java:1038) @ java.util.scanner.findpatterninbuffer(scanner.java:1010) client said: sds @ java.util.scanner.findwithinhorizon(scanner.java:1679) @ java.util.scanner.nextline(scanner.java:1538) @ server$newclient.run(server.java:66) @ java.lang.thread.run(thread.java:745)
what's happening code scans first line client ("sds", printed stdout), , loops , tries scan next line client. since client hasn't sent more yet, input stream scanner throws exception.
Comments
Post a Comment