Results 1 to 19 of 19

Thread: Socket help

  1. #1
    jobud9 is offline Member
    Join Date
    Dec 2011
    Posts
    34
    Rep Power
    0

    Default 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.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Socket help

    What URL are you entering into the browser?

  3. #3
    jobud9 is offline Member
    Join Date
    Dec 2011
    Posts
    34
    Rep Power
    0

    Default Re: Socket help


  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default 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()

  5. #5
    jobud9 is offline Member
    Join Date
    Dec 2011
    Posts
    34
    Rep Power
    0

    Default Re: Socket help

    I am still not getting any errors. is there anything wrong with my threading?

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Socket help

    What was printed out when you added the printlns to your code?

  7. #7
    jobud9 is offline Member
    Join Date
    Dec 2011
    Posts
    34
    Rep Power
    0

    Default Re: Socket help

    in the loop, it sends out messages, but not inside the server class.

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Socket help

    How is the code in the server class called? I don't see a constructor.

  9. #9
    jobud9 is offline Member
    Join Date
    Dec 2011
    Posts
    34
    Rep Power
    0

    Default Re: Socket help

    the server class is on line 16

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default 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.

  11. #11
    jobud9 is offline Member
    Join Date
    Dec 2011
    Posts
    34
    Rep Power
    0

    Default 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?

  12. #12
    jobud9 is offline Member
    Join Date
    Dec 2011
    Posts
    34
    Rep Power
    0

    Default Re: Socket help

    apparently now. I'm just getting a Illegal thread state error..

  13. #13
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Socket help

    I'm just getting a Illegal thread state error..
    Where? I don't see any Thread classes in your code.

    how should I call it
    You are calling the constructor with this:
    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.

  14. #14
    jobud9 is offline Member
    Join Date
    Dec 2011
    Posts
    34
    Rep Power
    0

    Default 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

  15. #15
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default 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

  16. #16
    jobud9 is offline Member
    Join Date
    Dec 2011
    Posts
    34
    Rep Power
    0

    Default 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.

  17. #17
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default 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.

  18. #18
    jobud9 is offline Member
    Join Date
    Dec 2011
    Posts
    34
    Rep Power
    0

    Default Re: Socket help

    you see! this is proof that i'm just rubbish at Java:
    my life as a Java developer.. - YouTube

  19. #19
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Socket help

    Its useful to design your program before writing it.
    Write a simple version first before trying to make the complicated one.
    Learn to walk before trying to run.

Similar Threads

  1. Socket Programming ERROR : Socket not connected
    By vishrut_n_shah in forum Networking
    Replies: 0
    Last Post: 11-04-2011, 09:32 AM
  2. Replies: 1
    Last Post: 07-02-2010, 01:18 PM
  3. swt socket
    By cuiran in forum SWT / JFace
    Replies: 2
    Last Post: 01-22-2010, 01:34 AM
  4. append response to the request from Socket and write to another socket
    By vaibhav_singh_vs@yahoo.co in forum Networking
    Replies: 3
    Last Post: 04-17-2009, 07:02 PM
  5. Socket ?
    By barusk in forum Networking
    Replies: 0
    Last Post: 03-05-2009, 04:51 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •