• 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

My way for multi-client failed?

 
Ranch Hand
Posts: 239
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ranchers,
In my URLyBird assignment, my choice of corresponding between client and server is Socket.The probelm come out when i try to realize one server against multi clients.My strategy is as following:in main of Server.java using multithead, one thread connect to one client.Because ServerSocket has a parameter of portNumber, so i made an originPortNumber which is the same in Server.java and Client.java and via the originPortNumber I send a new portNumber to both Client and the new thread.Then i let my client continuely send Socket with the new portNumber to server and meanwhile my new thread create new ServerSocket with new portNumber.
Unfortunately in my practise only one client can connect to the server and when multi client try to do that, server will send unknown error:java.lang.NumberFormatException .
Is such strategy executable?
 
author and jackaroo
Posts: 12200
280
Mac IntelliJ IDE Firefox Browser Oracle C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Zhixiong,

You should not need to do this. When your client connects, the server should automagically use a different port number for the connection, allowing it to continue listening on the original port number. There should be no need for you to manually change port numbers.

Regards, Andrew
 
Zhixiong Pan
Ranch Hand
Posts: 239
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Andrew,
I tried again directed by your advice but failed once more.In client i use the socket.getLocalPort() for tracing, and the results were like what you said, that is the client socket port changed.I show you some of my code hoping such act not breaking the spirit of certification.
My Data.java extends Thread and in its main codes are as following:
Data da;
ServerSocket ss;
int portNumber = 1777;
try{
ss = new ServerSocket(portNumber);
while(true){
da = new Data();
da.s = ss.accept();
da.start();
}
}
catch(Exception e){
System.out.println(e);
}

Would you help me again?
 
Zhixiong Pan
Ranch Hand
Posts: 239
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i tried many many ways, but the error is the same.My first client can successfully get data from server, but clients after the first can not, and the server will paint:

java.lang.NumberFormatException: For input string: "le"
at java.lang.NumberFormatException.forInputString(NumberFormatException.
java:48)
at java.lang.Integer.parseInt(Integer.java:468)
at java.lang.Integer.parseInt(Integer.java:518)
at KKMultiServerThread.getHotel(KKMultiServerThread.java:36)
at KKMultiServerThread.run(KKMultiServerThread.java:75)

Can anyone help me?
 
Andrew Monkhouse
author and jackaroo
Posts: 12200
280
Mac IntelliJ IDE Firefox Browser Oracle C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Zhixiong,

The problem is not in that short snippet of code you posted. I cannot tell from that what the problem is. The following two lines from your stack trace are important though:Now line 36 of KKMultiServerThread.java is almost certainly just calling Integer.parseInt() but the question is: why is it being passed the value "le"? Whatever is creating that value of "le" is probably your problem.

To demonstrate that the code you posted is not the problem, I created a sample server and client. The code I am presenting here is deliberately not production quality: it is the simplest bit of code that will demonstrate the code snippet you provided works. The code you provided is limited to the main() method of my Data class - the run() method just exists to show that something is happening. So here is my server:Here is my client:And here is the output from when it runs:So, as you can see, I had four clients simultaneously connected. I am never attempting to change the port number - that is not something I need worry about.

Hopefully you can use this as a roadmap to work out where your code is going faulty.

Regards, Andrew
 
Zhixiong Pan
Ranch Hand
Posts: 239
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Andrew,
I should say thanks and sorry to you.Thanks for your passion on helping us unexpeirenced.Sorry for my ignoring to reflect after i had got the answer.Yer, the exception is caused from my ConnectDB.class which is used to read db file and store the result to a StringBuffer.The StringBuffer append() a line from db file.When client connect to server first time, the append() is normal, but if againt StringBuffer append() again.So that is the key mistake.My solution is to create a new StringBuffer per every time reading the file.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok Andrew I like your code but is there a way to do this on a GUI, I have tried to implement your code but came up short kept reloading the program and ended up with multiple windows for my server, if you ever re look at this please help
 
reply
    Bookmark Topic Watch Topic
  • New Topic