wood burning stoves 2.0*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes My way for multi-client failed? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "My way for multi-client failed?" Watch "My way for multi-client failed?" New topic
Author

My way for multi-client failed?

Zhixiong Pan
Ranch Hand

Joined: Jan 25, 2006
Posts: 239
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?


SCJP 1.4 SCJD
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11404
    
  81

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


The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
Zhixiong Pan
Ranch Hand

Joined: Jan 25, 2006
Posts: 239
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

Joined: Jan 25, 2006
Posts: 239
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
Marshal Commander

Joined: Mar 28, 2003
Posts: 11404
    
  81

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

Joined: Jan 25, 2006
Posts: 239
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.
Brian Hoover
Greenhorn

Joined: Sep 20, 2008
Posts: 1
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: My way for multi-client failed?
 
Similar Threads
how to connect to other machine rather than localhost.
How to realize multi-client ?
Help(Client Fails to read from Server)
Problem with echo server and client.
How to get selected radioButton?