Results 1 to 19 of 19
Thread: Socket help
- 02-03-2012, 06:48 PM #1
Member
- Join Date
- Dec 2011
- Posts
- 34
- Rep Power
- 0
Socket help
When I try to connect to the socket with my browser it says it can't connect, and I don't get any errors.
I am starting my thread, that's just in another file.
code:
Java Code:package jo.hn; import java.io.BufferedReader; import java.io.DataOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.net.ServerSocket; import java.net.Socket; public class httpd implements Runnable{ Thread t1 = new Thread(new server()); private boolean ShouldRun = true; int port = 5152; public void run(){ System.out.println("running!"); while(ShouldRun){ System.out.println("making thread"); t1.start(); } } private class server implements Runnable{ public void run() { try{ System.out.println("thread made!"); ServerSocket Server = new ServerSocket(port); Socket sock = Server.accept(); BufferedReader input = new BufferedReader(new InputStreamReader(sock.getInputStream())); DataOutputStream output = new DataOutputStream(sock.getOutputStream()); Serve(input,output); } catch(IOException e){ e.printStackTrace(); ShouldRun = false; } catch(Exception e){ e.printStackTrace(); } } private void Serve(BufferedReader in,DataOutputStream out){ try { String tmp = in.readLine(); String tmp2 = new String(tmp); tmp.toUpperCase(); int start = 0; int end = 0; for (int a = 0; a < tmp2.length(); a++) { if (tmp2.charAt(a) == ' ' && start != 0) { end = a; break; } if (tmp2.charAt(a) == ' ' && start == 0) { start = a; } } tmp2.substring(start + 2, end); System.out.println("printing!"); String s = "HTTP/1.0 "; s = s + "200 OK"; s = s + "\r\n"; s = s + "Connection: close\r\n"; s = s + "Content-Type: Text/html\r\n"; s = s + "\r\n"; s = s + "<b>hello!</b>"; out.writeChars(s); } catch (Exception e) { e.printStackTrace(); } } } }Last edited by jobud9; 02-04-2012 at 03:51 AM.
- 02-03-2012, 07:18 PM #2
Re: Socket help
What URL are you entering into the browser?
- 02-04-2012, 02:54 AM #3
Member
- Join Date
- Dec 2011
- Posts
- 34
- Rep Power
- 0
Re: Socket help
- 02-04-2012, 03:03 AM #4
Re: Socket help
You need to debug your code to see where and when it is executing. Add printlns in all the methods so you know when they start and when the code exits. Also print out the values of all the variables whose values change. For example: tmp.
Make sure all the catch blocks call printStackTrace()
- 02-04-2012, 03:18 AM #5
Member
- Join Date
- Dec 2011
- Posts
- 34
- Rep Power
- 0
Re: Socket help
I am still not getting any errors. is there anything wrong with my threading?
- 02-04-2012, 03:19 AM #6
Re: Socket help
What was printed out when you added the printlns to your code?
- 02-04-2012, 03:29 AM #7
Member
- Join Date
- Dec 2011
- Posts
- 34
- Rep Power
- 0
Re: Socket help
in the loop, it sends out messages, but not inside the server class.
- 02-04-2012, 03:32 AM #8
Re: Socket help
How is the code in the server class called? I don't see a constructor.
- 02-04-2012, 03:33 AM #9
Member
- Join Date
- Dec 2011
- Posts
- 34
- Rep Power
- 0
Re: Socket help
the server class is on line 16
- 02-04-2012, 03:36 AM #10
Re: Socket help
I see the class definition, but I do not see where any of its methods are being called.
It does not have a constructor which would be one place where you could have calls to the other methods in the class.
- 02-04-2012, 03:39 AM #11
Member
- Join Date
- Dec 2011
- Posts
- 34
- Rep Power
- 0
Re: Socket help
how should I call it? I need to run it every time the loop is called.. just construct it and add server.start() into the loop?
- 02-04-2012, 03:43 AM #12
Member
- Join Date
- Dec 2011
- Posts
- 34
- Rep Power
- 0
Re: Socket help
apparently now. I'm just getting a Illegal thread state error..
- 02-04-2012, 03:50 AM #13
Re: Socket help
Where? I don't see any Thread classes in your code.I'm just getting a Illegal thread state error..
You are calling the constructor with this:how should I call it
new server();
It calls the default constructor which doesn't do much. If you want the constructor to do something you need to code one.
- 02-04-2012, 03:52 AM #14
Member
- Join Date
- Dec 2011
- Posts
- 34
- Rep Power
- 0
Re: Socket help
sorry. iv'e updated my code to show you what I have now. you can see where i'm getting my illegal thread state error
- 02-04-2012, 04:01 AM #15
Re: Socket help
You can only start a Thread once. How many times are you trying to start the thread in the loop?
Why do you want to start so many threads?
try getting rid of the loop
- 02-04-2012, 04:07 AM #16
Member
- Join Date
- Dec 2011
- Posts
- 34
- Rep Power
- 0
Re: Socket help
I need the loop to see when someone connects to the socket and create a unique thread to serve the client. each client needs their own thread so no client has to wait for a response.
- 02-04-2012, 04:12 AM #17
Re: Socket help
For testing remove the thread and see if ONE client can connect.
WHEN you can get one client to connect and work the way you want,
THEN worry about more than one client connecting.
The way you allow more than one client is to create a thread when the accept() method returns with a new socket.
Use the new thread to handle the connection with that socket and then loop back to the accept statement where the code will wait until the next client connects.
You use ONE ServerSocket for all the clients to connect to.
I'm done for tonight.
- 02-04-2012, 06:40 AM #18
Member
- Join Date
- Dec 2011
- Posts
- 34
- Rep Power
- 0
Re: Socket help
you see! this is proof that i'm just rubbish at Java:
my life as a Java developer.. - YouTube
- 02-04-2012, 02:04 PM #19
Similar Threads
-
Socket Programming ERROR : Socket not connected
By vishrut_n_shah in forum NetworkingReplies: 0Last Post: 11-04-2011, 09:32 AM -
Socket and how to launch thread for receiving socket messages
By newbiejava in forum New To JavaReplies: 1Last Post: 07-02-2010, 01:18 PM -
swt socket
By cuiran in forum SWT / JFaceReplies: 2Last Post: 01-22-2010, 01:34 AM -
append response to the request from Socket and write to another socket
By vaibhav_singh_vs@yahoo.co in forum NetworkingReplies: 3Last Post: 04-17-2009, 07:02 PM -
Socket ?
By barusk in forum NetworkingReplies: 0Last Post: 03-05-2009, 04:51 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks