Results 1 to 8 of 8

Thread: Socket error

  1. #1
    hannes is offline Member
    Join Date
    Nov 2009
    Posts
    54
    Rep Power
    0

    Default Socket error

    Hi,

    I'm very new to socket communication( i started learning yesterday ). I tried to
    make a little program myself. The only thing it does is a socket who tells a serversocket the connection succeeded and otherwise.

    I ran the program on one computer, everything works fine, but when i want to run it on two computers, my program blocks in the Socket constructor, and after 10 seconds i get a ConnectionException.

    Here is the code:

    The server main class:
    Java Code:
    import java.io.IOException;
    
    public class ServerMain {
    	
    	public static void main(String[] args) throws IOException {
    		new ServerSocketClass( ); 
    	}
    
    }
    The server socket class :
    Java Code:
    import java.io.IOException;
    import java.net.ServerSocket;
    import java.net.Socket;
    
    public class ServerSocketClass {
    	protected ServerSocket server;
    	protected int listenPort;
    	
    	public ServerSocketClass( ) throws IOException {
    		super( );
    		listenPort = 5600;
    		server = new ServerSocket( listenPort );
    		acceptConnections( );
    	}
    	
    	public void acceptConnections( ) {
    		new Thread( new Runnable( ) {	
    			@Override
    			public void run() {
    				startAccepting( );
    			}
    		}).start( );
    	}
    	
    	protected void startAccepting( ) {
    		System.out.println( "aanhet wachten" );
    		while( true ) {
    			try {
    				Socket incoming = server.accept( );
    				System.out.println( "Er is een socket binnengekomen" );
    				handleIncomingSocket( incoming ); 
    			} catch (IOException e) {
    				// TODO Auto-generated catch block
    				e.printStackTrace();
    			}
    		}
    	}
    
    	protected void handleIncomingSocket( Socket incoming ) {
    		new Thread( new SocketHandler( incoming ) ).start( );
    	}
    	
    	
    }
    The SocketHandler class:
    Java Code:
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.OutputStream;
    import java.io.PrintWriter;
    import java.net.Socket;
    
    public class SocketHandler implements Runnable {
    
    	private Socket socket;
    	public SocketHandler( Socket socket ) {
    		super( );
    		this.socket = socket;
    	}
    	public void run( ) {
    		OutputStream output;
    		try {
    			output = socket.getOutputStream( );
    		PrintWriter writer = new PrintWriter( output );
    		BufferedReader reader = new BufferedReader( new InputStreamReader( socket.getInputStream( ) ) );
    		System.out.println( reader.readLine( ) );
    		writer.write( "Connectie terug geslaagd" );
    		writer.flush( );
    		output.close( );
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
    	}
    	
    	public Socket getSocket( ) {
    		return socket;
    	}
    
    }
    The ClientMain class :
    Java Code:
    public class ClientMain {
    	public static void main( String[] args) {
    		new SocketConnectionToServerTest( );
    	}
    }
    and finally, the SocketConnectionToServerTest class:
    Java Code:
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.OutputStream;
    import java.io.PrintWriter;
    import java.net.Socket;
    import java.net.UnknownHostException;
    public class SocketConnectionToServerTest {
    	protected Socket connectionSocket;
    	
    	public SocketConnectionToServerTest( ) {
    		super( );
    		try {
    			connectionSocket = new Socket( "127.0.0.1", 5600 );
    		} catch (UnknownHostException e) {
    			e.printStackTrace();
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
    		makeContactToServer( );
    	}
    	
    	public void makeContactToServer( ) {
    		try {
    			OutputStream output = connectionSocket.getOutputStream( );
    			BufferedReader reader = new BufferedReader( new InputStreamReader( connectionSocket.getInputStream( ) ) );
    			PrintWriter writer = new PrintWriter( output );
    			writer.println( "Connectie gelukt" );
    			writer.flush( );
    			System.out.println( reader.readLine( ) );
    			output.close( );
    			writer.close( );
    			reader.close( );
    		} catch (IOException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}  
    	}
    }
    These are the classes. I get the error on line 18 in class SocketConnectionToServerTest( the socket constructor). I've tried some things, but it didn't work.

    Any suggestions?

    Hannes
    Last edited by hannes; 04-16-2010 at 09:04 AM.

  2. #2
    iluxa is offline Senior Member
    Join Date
    Mar 2010
    Posts
    266
    Rep Power
    5

    Default

    for someone who just started learning about sockets your code is surprisingly good.

    and it should work... weird that it doesn't.

    what does the exception say?

  3. #3
    hannes is offline Member
    Join Date
    Nov 2009
    Posts
    54
    Rep Power
    0

    Default

    I've tested the program a couple of times now, and there is something curious about it. I have two computers, one upstairs and one downstairs. When i run my serversocket upstairs i get and my normal socket downstairs, i get an error, but
    when i run my serversocket downstairs and my normal socket upstairs, everything works fine.

    Maybe it is a kind of firewall problem?

    Hannes
    Last edited by hannes; 04-16-2010 at 11:02 PM.

  4. #4
    iluxa is offline Senior Member
    Join Date
    Mar 2010
    Posts
    266
    Rep Power
    5

    Default

    sounds like it is, considering it does work in some scenarios and not others. Check the firewall settings upstairs.

  5. #5
    hannes is offline Member
    Join Date
    Nov 2009
    Posts
    54
    Rep Power
    0

    Default

    I don't know very much about firewall, so i don't know what to do with it. Can you tell me some things that should be on/off for this program?

    Hannes

  6. #6
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    5

    Default

    Quote Originally Posted by hannes View Post
    Maybe it is a kind of firewall problem?

    if you're connected through a router you must anable port-forwarding to the host where the server is running.

  7. #7
    hannes is offline Member
    Join Date
    Nov 2009
    Posts
    54
    Rep Power
    0

    Default

    Thanks for you posts, but i don't think that is the problem. My friend is also a programmer, and he wrote a short program for me to test. His program works fine( i can connect from downstairs to upstairs and from upstairs to downstairs).

    Now, i'm realy confused about what the error could be. Any suggestions about this?

    Hannes

  8. #8
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    5

    Default

    Quote Originally Posted by hannes View Post
    Any suggestions about this?

    i guess the exception is thrown because you try to construct the socket before the server is running. so you get a connection refused. i did the following. in ServerSocketClass i inserted the following code and the end

    Java Code:
    	public static void main(String[] args) {
    		try {
    		new ServerSocketClass();
    		} catch (Exception ex) {
    			ex.printStackTrace();
    		}
    	}

    so that the server can be started with java ServerSocketClass.

    create a new project and create the java file SocketConnectionToServerTest.java with the code but put this code at the end, so that it will start:

    Java Code:
    	public static void main(String[] args) {
    		new SocketConnectionToServerTest();
    	}
    now if the server is running and you start SocketConnectionToServerTest with java SocketConnectionToServerTest you will get

    aanhet wachten
    Er is een socket binnengekomen
    Connectie gelukt

    and no exception is thrown. if you run your server on an other machine then don't forget to change the ip-adress in SocketConnectionToServerTest in the line connectionSocket = new Socket("127.0.0.1", 5600);

    i don't know if the code running corretly, but you get no exception.

Similar Threads

  1. Socket applet programming error?
    By Master Zero in forum Java Applets
    Replies: 6
    Last Post: 10-05-2011, 09:15 PM
  2. Replies: 3
    Last Post: 04-13-2010, 03:43 PM
  3. Replies: 3
    Last Post: 04-05-2010, 02:19 AM
  4. Error in establishing socket connection
    By saso1310 in forum JDBC
    Replies: 2
    Last Post: 05-18-2009, 01:00 PM
  5. Replies: 3
    Last Post: 01-26-2009, 05:30 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
  •