Win a copy of Android Programming: The Big Nerd Ranch Guide this week in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Server doesn't work with more than one connection  RSS feed

 
Georgi Stoyanov
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all!
There is very complex problem with my simple java client-server application( I'm not sure that this is even related with threads): When there is only one connection to the server, broadcast function works fine, but when there is another client connected to server and I try to put some message, broadcast doesn't work, and server throws following exceptions -
java.lang.IndexOutOfBoundsException: end
at java.util.regex.Matcher.region(Matcher.java:1038)
at java.util.Scanner.findPatternInBuffer(Scanner.java:1010)
at java.util.Scanner.findWithinHorizon(Scanner.java:1679)
at java.util.Scanner.nextLine(Scanner.java:1538)
at Server$NewClient.run(Server.java:66)
at java.lang.Thread.run(Thread.java:745)

Here is the code:

Server side:



Client side:




Do you have any suggestions what can be wrong with the code? Maybe something with threads that I don't understand.
PS I'm very new to threads and sockets, so any explanations and suggestions to the code will be very helpful for me!
Thanks in advance!
 
Henry Wong
author
Sheriff
Posts: 23193
124
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Added this topic discussion to the socket and networking forum.

Henry
 
Henry Wong
author
Sheriff
Posts: 23193
124
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The NewClient class is an inner class of the Server class. And all instances of this class are created with the same instance of the outer class.

Furthermore, the class does not define the in, out, and sdf instance variables. This means that all of your client handling classes are using the same instance variables (defined in the outer class). Is this your intention?

Henry
 
Georgi Stoyanov
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:The NewClient class is an inner class of the Server class. And all instances of this class are created with the same instance of the outer class.

Furthermore, the class does not define the in, out, and sdf instance variables. This means that all of your client handling classes are using the same instance variables (defined in the outer class). Is this your intention?

Henry


Great! Now I change the constructor to:

and it works perfectly.

Do you have any suggestions how to make input and output to work on separate threads and read/write when there is something to read/write?
 
Henry Wong
author
Sheriff
Posts: 23193
124
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Georgi Stoyanov wrote:
Do you have any suggestions how to make input and output to work on separate threads and read/write when there is something to read/write?


I don't think the issue is really related to threads... basically, you have multiple clients, each with its own sockets, and hence, its own streams. And you only have one common set of variables that is used to store these different streams.

Henry
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!