![]() |
Confused about simple multi-threaded server
I'm trying to write a multi-threaded server application. The server is just going to be the same computer as the client just because its more convenient to write that way. I have a book, "Absolute Java" to go by, but I can't figure out what is wrong. The amount of code is very small so I'll just post all of it.
Client: :
package stuff;Server: :
package stuff;Its supposed to be fairly easy to set up something like what I've attempted above, so perhaps I haven't gotten the concepts down. But I think I do. One thing I don't understand is how can we initially ask the server which port we should try to connect to (the one we initially connect to before the server spins us off to another port)? Or do we just have to know that in advance? Also, is there something wrong with how I'm trying to make the application multi-threaded? Thats what I thought was wrong at first, but when I run the program, the run method is invoked, it just never reads in the text that the client sends. |
Re: Confused about simple multi-threaded server
Its also throwing an exception every other time I run it, but when I change the port number it no longer throws the exception. But when I run it again, under the port number I changed it to, it once again throws an exception.
|
Re: Confused about simple multi-threaded server
If anybody who reads this has an example of an easy to understand multi-threaded Client-Server type application, I'd appreciate if you could post it. I can make an application with one thread but thats not helpful
|
Re: Confused about simple multi-threaded server
I changed my code to the following, and now it appears to be working. However, I'm sure there's probably still something wrong with it... *sighs*
:
package stuff; |
Re: Confused about simple multi-threaded server
Im new to Java myself but i that changing the way you have threaded your server may be an idea.
For example you are spawning a new tread of the entire program. Perhaps you would be better of having the main class run an infinite loop accepting connection from clients. Once it has recieved a client from socket.accept() the you can pass that to a new Threaded class that deals with all of the input and outputs to that socket. This may help to tidy up the server and make it clearer what is going on. Also it means that multiple clients can connect at the same time if you require this. Cheers, Chris |
Re: Confused about simple multi-threaded server
Quote:
|
Re: Confused about simple multi-threaded server
Quote:
You do have to know it in advance. Some programs are smart though, and know that certain protocols use certain ports. That's why in your browser, you put http://somesite.com/somepage.html instead of being required to specify a port (e.g. http://somesite.com:80/somepage.html). Any port under 1024 is considered a "well known" port and is reserved for a specific protocol (e.g. 80 = HTTP, 443= HTTPS, 25 = SMTP, 666 = Doom (the game)). There are programs that will check what ports are open on a machine (see: port scanning), and from that you can guess what is being hosted on those ports. If you see port 80 open, odds are that it's hosting a web server, but that's not necessarily the case, rather it's simply by convention. |
| All times are GMT -5. The time now is 3:51 PM. |
Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC