Results 1 to 17 of 17
  1. #1
    adwart is offline Member
    Join Date
    Jul 2011
    Posts
    25
    Rep Power
    0

    Default TCP/IP, firewall problems, http tunneling. Some Help needed.

    I' ve been working on a chat application using the simple low level socket-serverSocket way; every chat client has its own server thread(on the same machine) that manages the incoming messages.
    The chat work perfectly on my local network however the connection over the internet is the problem due to the firewalls/nats.
    What is the best way to make it work without having to open ports on all the machines ? http tunneling, hole punching ? How can i apply these methods to my aplication?

    Thanks for reading.

  2. #2
    adwart is offline Member
    Join Date
    Jul 2011
    Posts
    25
    Rep Power
    0

    Default Re: TCP/IP, firewall problems, http tunneling. Some Help needed.

    I've been trying to send a string of data from a Htpp client to a server on my machine using a "HttpURLConnection" for a while, tried different things and i think i'm going insane. Can someone tell me what's wrong here ?
    Java Code:
    import java.io.DataOutputStream;
    import java.io.IOException;
    import java.net.URL;
    import java.net.HttpURLConnection;
     public class HTTPClient {
    	private HttpURLConnection sURLConn;
    	private DataOutputStream  out;
    	public HTTPClient(){
    		new HTTPServer().start();
    		openConnection();
    	}
    	private void openConnection(){
    		String s = "test";
    		try{
    		sURLConn=(HttpURLConnection) new URL("http://localhost").openConnection();
    		sURLConn.setRequestMethod("POST");
    		sURLConn.setRequestProperty("Content-Type", "text/plain");
    		sURLConn.setRequestProperty("Content-Length",""+s.getBytes().length);
    		sURLConn.setUseCaches(false);
    		sURLConn.setDoOutput(true);
    		sURLConn.connect();
    		out = new DataOutputStream(sURLConn.getOutputStream());
    		System.out.println("Connected to " + sURLConn.getURL());
    			out.writeBytes(s);
    			out.flush();
    		
    		}catch(IOException e){
    			System.err.println("Couldn't connect");
    		}
    	}
    	public static void main(String[] args) {
    		new HTTPClient();
    	}
    }
    Java Code:
    import java.io.BufferedReader;
    import java.io.DataOutputStream;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.net.ServerSocket;
    import java.net.Socket;
    import javax.swing.JOptionPane;
    
    public class HTTPServer extends Thread {
    	private ServerSocket serverSocket = null;
    	protected BufferedReader  in;
    	protected DataOutputStream out;
    	protected Socket clientSock = null;
    	public void run(){
    		try{
    			serverSocket = new ServerSocket(80);
    			System.out.println("Server connected");
    		}
    		catch(IOException e){
    			JOptionPane.showMessageDialog(null,"Could not listen to the port","error",JOptionPane.ERROR_MESSAGE);
    			e.printStackTrace();
    		}
    		while(true){
    			try {
    				clientSock = serverSocket.accept();
    				System.out.println("Client connected: " + clientSock.getInetAddress().getHostAddress());
    			} catch (IOException e) {
    				System.err.println("Accept failed.");
    				e.printStackTrace();
    			}
    			try {
    				in = new BufferedReader (new InputStreamReader(clientSock.getInputStream()));
    				String line=in.readLine();
    				System.out.println(line);
    			}catch (IOException e) {
    				e.printStackTrace();
    			}
    		}
    	}
    }
    Seems like the inputstream is always empty.
    Last edited by adwart; 09-21-2011 at 09:17 PM.

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

    Default Re: TCP/IP, firewall problems, http tunneling. Some Help needed.

    The readLine method waits for the end of the line. Does your output provide that?

  4. #4
    adwart is offline Member
    Join Date
    Jul 2011
    Posts
    25
    Rep Power
    0

    Default Re: TCP/IP, firewall problems, http tunneling. Some Help needed.

    Quote Originally Posted by Norm View Post
    The readLine method waits for the end of the line. Does your output provide that?
    Tried to put "\r\n" at the end of the string but nothing changed.

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

    Default Re: TCP/IP, firewall problems, http tunneling. Some Help needed.

    You left off the import statements necessary to compile and test your code.

  6. #6
    adwart is offline Member
    Join Date
    Jul 2011
    Posts
    25
    Rep Power
    0

    Default Re: TCP/IP, firewall problems, http tunneling. Some Help needed.

    Quote Originally Posted by Norm View Post
    You left off the import statements necessary to compile and test your code.
    My bad. Edited the post.

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

    Default Re: TCP/IP, firewall problems, http tunneling. Some Help needed.

    Try adding these after the call to flush():
    Java Code:
             int res = sURLConn.getResponseCode();         //<<<<<<<<<<<<<< NEED THIS to FLUSH 
             String respM = sURLConn.getResponseMessage();
             System.out.println("Client - res=" + res + ", respM=" + respM);  // NEVER PRINTS

  8. #8
    adwart is offline Member
    Join Date
    Jul 2011
    Posts
    25
    Rep Power
    0

    Default Re: TCP/IP, firewall problems, http tunneling. Some Help needed.

    Quote Originally Posted by Norm View Post
    Try adding these after the call to flush():
    Java Code:
             int res = sURLConn.getResponseCode();         //<<<<<<<<<<<<<< NEED THIS to FLUSH 
             String respM = sURLConn.getResponseMessage();
             System.out.println("Client - res=" + res + ", respM=" + respM);  // NEVER PRINTS
    Now with "int res = sURLConn.getResponseCode();", whether or not I write in the outputstream I always get "POST / HTTP/1.1" as output.

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

    Default Re: TCP/IP, firewall problems, http tunneling. Some Help needed.

    Isn't that what your code says to send? Try changing it to a GET

  10. #10
    adwart is offline Member
    Join Date
    Jul 2011
    Posts
    25
    Rep Power
    0

    Default Re: TCP/IP, firewall problems, http tunneling. Some Help needed.

    Quote Originally Posted by Norm View Post
    Isn't that what your code says to send? Try changing it to a GET
    i did this
    Java Code:
    		sURLConn.setRequestMethod("GET");
    //		sURLConn.setRequestProperty("Content-Type", "text/plain");
    //		sURLConn.setRequestProperty("Content-Length",""+s.getBytes().length);
    but still "POST / HTTP/1.1" as output.

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

    Default Re: TCP/IP, firewall problems, http tunneling. Some Help needed.

    Keep changing your code until you find the combination that works.

  12. #12
    adwart is offline Member
    Join Date
    Jul 2011
    Posts
    25
    Rep Power
    0

    Default Re: TCP/IP, firewall problems, http tunneling. Some Help needed.

    To use the "GET" method i just had to disable the output of the httpconnection (sURLConn.setDoOutput(true))and by the way THANKS Norm as usual for the great help.

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

    Default Re: TCP/IP, firewall problems, http tunneling. Some Help needed.

    Glad to help. I now have some working code for the next OP's question.

  14. #14
    adwart is offline Member
    Join Date
    Jul 2011
    Posts
    25
    Rep Power
    0

    Default Re: TCP/IP, firewall problems, http tunneling. Some Help needed.

    Quote Originally Posted by Norm View Post
    Glad to help. I now have some working code for the next OP's question.
    Alright then!
    Is there a way to flush the streams without a call to the "getResponseCode()" and/or "getResponseMessage()" methods ?
    I don't really like the idea of blocking the client until i receive a response from the server.

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

    Default Re: TCP/IP, firewall problems, http tunneling. Some Help needed.

    Sorry, I have no idea what goes on inside the HTTPURLConnection class. Use Sockets if you want better control.
    Or put the code on its own thread.

  16. #16
    adwart is offline Member
    Join Date
    Jul 2011
    Posts
    25
    Rep Power
    0

    Default Re: TCP/IP, firewall problems, http tunneling. Some Help needed.

    Quote Originally Posted by Norm View Post
    Sorry, I have no idea what goes on inside the HTTPURLConnection class. Use Sockets if you want better control.
    Well, thanks anyway. I wil keep that in mind.

  17. #17
    adwart is offline Member
    Join Date
    Jul 2011
    Posts
    25
    Rep Power
    0

    Default Re: TCP/IP, firewall problems, http tunneling. Some Help needed.

    I tried the application with some of my friends but i can't even connect to them (via port 80). What am i doing wrong ?

Similar Threads

  1. Applet connection to mysql server through firewall
    By Rolf83 in forum Java Applets
    Replies: 0
    Last Post: 07-19-2011, 10:27 PM
  2. Need help with a Firewall Software project...
    By xcesmoni in forum NetBeans
    Replies: 0
    Last Post: 08-07-2010, 02:38 PM
  3. Firewall Using java?
    By koxy in forum Networking
    Replies: 1
    Last Post: 03-26-2010, 03:17 PM
  4. Replies: 7
    Last Post: 11-11-2009, 08:29 PM
  5. Error in compiling Firewall source code
    By ajib85 in forum Networking
    Replies: 8
    Last Post: 10-02-2008, 04:37 AM

Posting Permissions

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