The new forums will be named Coin Return (based on the most recent vote)! You can check on the status and timeline of the transition to the new forums here.
The Guiding Principles and New Rules document is now in effect.

Connecting to my Simple Server app

GrundlterrorGrundlterror Registered User regular
edited April 2009 in Help / Advice Forum
So I'm in networking class and we have an assignment to design a simple client/server application in java. Right now all my application does (or rather, is supposed to do) is to take in the HTTP request line and spit it back out. I'm 99% sure I have the code set up right but I can't connect to the server!

I assumed that want I needed to do was connect to my host name and the port number I specified in the program. I also assumed that my machines host name would be the Connection-Specific DNS Suffix in my ipconfig. If nothing else I could go to a website that tells me my computers IP. Neither have worked when I put in [url]http://my.com.cast.net:portnumber[/url] or [url]http://my.ip.add.ress:portnumber[/url]. Anyone have any idea where I get this information? Just in case I've made an error in my code I'll include it also

WebServer.java:
import java.io.* ;
import java.net.* ;

public final class WebServer
{
	public static void main(String argv[]) throws Exception
	{
		//set port number
		int port = 5203;
		//establish the listen socket
		ServerSocket listenSocket = new ServerSocket(port); 
		
		//listen for HTTP request in infinite loop
		while(true)
		{
			
			//listen for TCP connection request
			Socket connectionSocket = listenSocket.accept();
			
			//create httpRequest object to process message
			HttpRequest request = new HttpRequest(connectionSocket);
			
			// Create a new thread to process the request.
			Thread thread = new Thread(request);
			
			//start the thread
			thread.start();
			
			
		}
	}
}

HttpRequest.java:
import java.io.* ;
import java.net.* ;

final class HttpRequest implements Runnable
{
	final static String CRLF = "\r\n";
	Socket socket;

	// Constructor
	public HttpRequest(Socket socket) throws Exception 
	{
		this.socket = socket;
	}

	// Implement the run() method of the Runnable interface.
	public void run()
	{
		try {
			processRequest();
		} catch (Exception e) {
			System.out.println(e);
		}
	}

	private void processRequest() throws Exception
	{
		// Get a reference to the socket's input and output streams.
		InputStream is = this.socket.getInputStream();
		DataOutputStream os = new DataOutputStream(this.socket.getOutputStream());

		// Set up input stream filters.
		BufferedReader br = new BufferedReader(new InputStreamReader(is));	
		
		// Get the request line of the HTTP request message.
		String requestLine = br.readLine();

		// Display the request line.
		System.out.println();
		System.out.println(requestLine);
		
		// Get and display the header lines.
		String headerLine = null;
		while ((headerLine = br.readLine()).length() != 0) {
			System.out.println(headerLine);
		}
		
		// Close streams and socket.
		os.close();
		br.close();
		socket.close();
	}
}

Thank you!

edit - interesting, apparently I have to type in the router address instead of my machine. It's catching the request but still displays 'This webpage is not available.' when I go to http://192.168.1.102:5203

steam_sig.png
Grundlterror on

Posts

  • exoplasmexoplasm Gainfully Employed Near Blizzard HQRegistered User regular
    edited April 2009
    Assuming you're running both on the same PC, try connecting to 127.0.0.1:5203

    exoplasm on
    1029386-1.png
    SC2 NA: exoplasm.519 | PA SC2 Mumble Server | My Website | My Stream
  • GrundlterrorGrundlterror Registered User regular
    edited April 2009
    Hey, that worked too! Why did that work?

    Grundlterror on
    steam_sig.png
  • SpamSpam Registered User regular
    edited April 2009
    I suspect you're getting your ip addresses mixed up. The ip addy you get off a site like www.whatismyip.com is your external internet ip - if you try to connect using that address it'll more than likely get blocked by your router.

    127.0.0.1 is the localhost ip address - essentially it ignores the network and points to the local computer, which is why that works.

    The address you should be using would be something like http://192.168.1.10x:5203 or http://GrundlsPC:5203

    Spam on
  • SpamSpam Registered User regular
    edited April 2009
    do an 'ipconfig /all' to get your PC Host Name (first line of results) which you can use instead of an ip btw.

    Spam on
  • GrundlterrorGrundlterror Registered User regular
    edited April 2009
    Yeah definitely figured that out. For some reason I thought I'd be able to access it through the external internet. This is all good information to know. Thanks you guys!

    Grundlterror on
    steam_sig.png
Sign In or Register to comment.