![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Hobbyist Programmer
Join Date: Oct 2006
Posts: 204
Rep Power: 2
![]() |
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;
import java.net.*;
import java.io.*;
public class Client1 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
try{
int port = 4444;
String hostname = "localhost";
Socket connectionSock = new Socket(hostname, port);
BufferedReader serverInput =
new BufferedReader(new InputStreamReader(connectionSock.getInputStream()));
DataOutputStream serverOutput =
new DataOutputStream(connectionSock.getOutputStream());
serverOutput.writeBytes("Print this message Mr. server");
}catch (Exception e){}
}
}Server: package stuff;
import java.net.*;
import java.io.*;
public class Server extends Thread {
/**
* @param args
*/
Socket connection;
public static void main(String[] args) {
// TODO Auto-generated method stub
try{
ServerSocket serverSock = new ServerSocket(4444);
while(true)
{
Socket connectionSock = serverSock.accept();
System.out.println("Client connection accepted.");
Server handler = new Server(connectionSock);
handler.start();
}
}catch(Exception e){}
}
public Server(Socket connectionSocket)
{
connection = connectionSocket;
}
public void run()
{
try{
BufferedReader clientInput =
new BufferedReader(new InputStreamReader(connection.getInputStream()));
DataOutputStream clientOutput = new DataOutputStream(connection.getOutputStream());
String test = clientInput.readLine();
System.out.println("read in " + test);
}catch(Exception e){}
}
}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. |
|
|
|
|
|
#2 |
|
Hobbyist Programmer
Join Date: Oct 2006
Posts: 204
Rep Power: 2
![]() |
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.
|
|
|
|
|
|
#3 |
|
Hobbyist Programmer
Join Date: Oct 2006
Posts: 204
Rep Power: 2
![]() |
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
|
|
|
|
|
|
#4 |
|
Hobbyist Programmer
Join Date: Oct 2006
Posts: 204
Rep Power: 2
![]() |
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;
import java.net.*;
import java.io.*;
public class Client1 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Socket echoSocket = null;
PrintWriter out = null;
BufferedReader in = null;
try{
int port = 1235;
String hostname = "localhost";
Socket connectionSock = new Socket(hostname, port);
out = new PrintWriter(connectionSock.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(
connectionSock.getInputStream()));
out.println("Print this message Mr. server");
out.println("Print this other message Mr. server");
}catch (Exception e){}
}
} |
|
|
|
|
|
#5 |
|
Hobbyist Programmer
|
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
__________________
Who said i couldn't program sarcasm = raw_input('Type in a sarcastic remark: ')
|
|
|
|
|
|
#6 | |
|
Hobbyist Programmer
Join Date: Oct 2006
Posts: 204
Rep Power: 2
![]() |
Re: Confused about simple multi-threaded server
Quote:
|
|
|
|
|
|
|
#7 | |
|
Battle Programmer
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 751
Rep Power: 3
![]() |
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.
__________________
<insert disclaimer here> <insert shameless plug for Visual Studio here> |
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Need source code analysis tool-race condition in multi threaded application | Michael_24 | Java | 4 | Apr 22nd, 2008 11:08 AM |
| simple echo server | cwl157 | C++ | 3 | Jun 22nd, 2007 9:26 AM |
| Help with C# TCP/IP Server | csrocker101 | C# | 3 | Mar 15th, 2007 12:32 AM |
| FTP Server in Python | Sane | Python | 1 | Mar 31st, 2006 10:25 PM |
| send() problem w/ http server | kch_86 | C | 2 | Nov 25th, 2005 12:53 AM |