Programming Forums
User Name Password Register
 

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

 
 
Thread Tools Display Modes
Prev Previous Post in Thread   Next Post in Thread Next
Old Apr 19th, 2008, 5:32 PM   #1
Fall Back Son
Hobbyist Programmer
 
Join Date: Oct 2006
Posts: 203
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
 

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 3:55 PM.

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