Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Apr 19th, 2008, 5:32 PM   #1
Fall Back Son
Hobbyist Programmer
 
Join Date: Oct 2006
Posts: 188
Rep Power: 2 Fall Back Son is on a distinguished road
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.
Fall Back Son is offline   Reply With Quote
Old Apr 19th, 2008, 6:07 PM   #2
Fall Back Son
Hobbyist Programmer
 
Join Date: Oct 2006
Posts: 188
Rep Power: 2 Fall Back Son is on a distinguished road
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.
Fall Back Son is offline   Reply With Quote
Old Apr 19th, 2008, 6:12 PM   #3
Fall Back Son
Hobbyist Programmer
 
Join Date: Oct 2006
Posts: 188
Rep Power: 2 Fall Back Son is on a distinguished road
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
Fall Back Son is offline   Reply With Quote
Old Apr 19th, 2008, 6:44 PM   #4
Fall Back Son
Hobbyist Programmer
 
Join Date: Oct 2006
Posts: 188
Rep Power: 2 Fall Back Son is on a distinguished road
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){}
		
		
	}

}
Fall Back Son is offline   Reply With Quote
Old Apr 22nd, 2008, 3:41 AM   #5
Freaky Chris
Hobbyist Programmer
 
Freaky Chris's Avatar
 
Join Date: Dec 2007
Location: England
Posts: 169
Rep Power: 1 Freaky Chris is on a distinguished road
Send a message via MSN to Freaky Chris
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: ')
print sarcasm
Freaky Chris is offline   Reply With Quote
Old May 11th, 2008, 7:24 PM   #6
Fall Back Son
Hobbyist Programmer
 
Join Date: Oct 2006
Posts: 188
Rep Power: 2 Fall Back Son is on a distinguished road
Re: Confused about simple multi-threaded server

Quote:
Originally Posted by Freaky Chris View Post
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.
It is already doing that lol. Recheck the code. Main has a while loop that is always true so that it can connect to multiple clients. A new thread is spun off for each client and the run method is invoked.
Fall Back Son is offline   Reply With Quote
Old May 12th, 2008, 8:32 PM   #7
Jimbo
Battle Programmer
 
Jimbo's Avatar
 
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 742
Rep Power: 3 Jimbo is on a distinguished road
Re: Confused about simple multi-threaded server

Quote:
Originally Posted by Fall Back Son View Post
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?

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>
Jimbo is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 6:51 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC