Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Java
  4. Client can't connect to server when client/server are different projects

Client can't connect to server when client/server are different projects

Scheduled Pinned Locked Moved Java
helpquestioncsharpjavasysadmin
7 Posts 3 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    Valentinor
    wrote on last edited by
    #1

    I was doing some Socket tests, and while using localhost everything ran as I wanted. But then I tried to switch it to IP address, and well then things got weird. While the code was in the same class (Code_1 bellow), the simplified test from below was running fine, but as soon as I moved the server code in another project, even another package or class in same package, then when I'm running client code I'm getting java.net.ConnectException: Connection timed out: connect. If when the server is in another project, and for client I'm switching to localhost, then this time it is working. It doesn't want to work when they are in different projects/packages, and the client is using IP address and not localhost, but they work if both are in same project even if client has IP address or localhost.

    public static void main(String\[\] args) throws Exception {
    	Thread thread = new Thread(new Runnable() {
    		@Override
    		public void run() {
    			try {
    				ServerSocket server = new ServerSocket(port);
    				Socket socket = server.accept();
    				System.out.println(new ObjectInputStream(socket.getInputStream()).readObject());
    				socket.close();
    				server.close();
    			} catch (IOException e) {
    				// TODO Auto-generated catch block
    				e.printStackTrace();
    			} catch (ClassNotFoundException e) {
    				// TODO Auto-generated catch block
    				e.printStackTrace();
    			}
    		}
    	});
    	thread.start();
    
    	Socket socket = new Socket("IP\_ADDRESS", port);
    	new ObjectOutputStream(socket.getOutputStream()).writeObject("Test");
    	socket.close();
    }
    

    What is the problem, and what can I do to fix it?

    V L 2 Replies Last reply
    0
    • V Valentinor

      I was doing some Socket tests, and while using localhost everything ran as I wanted. But then I tried to switch it to IP address, and well then things got weird. While the code was in the same class (Code_1 bellow), the simplified test from below was running fine, but as soon as I moved the server code in another project, even another package or class in same package, then when I'm running client code I'm getting java.net.ConnectException: Connection timed out: connect. If when the server is in another project, and for client I'm switching to localhost, then this time it is working. It doesn't want to work when they are in different projects/packages, and the client is using IP address and not localhost, but they work if both are in same project even if client has IP address or localhost.

      public static void main(String\[\] args) throws Exception {
      	Thread thread = new Thread(new Runnable() {
      		@Override
      		public void run() {
      			try {
      				ServerSocket server = new ServerSocket(port);
      				Socket socket = server.accept();
      				System.out.println(new ObjectInputStream(socket.getInputStream()).readObject());
      				socket.close();
      				server.close();
      			} catch (IOException e) {
      				// TODO Auto-generated catch block
      				e.printStackTrace();
      			} catch (ClassNotFoundException e) {
      				// TODO Auto-generated catch block
      				e.printStackTrace();
      			}
      		}
      	});
      	thread.start();
      
      	Socket socket = new Socket("IP\_ADDRESS", port);
      	new ObjectOutputStream(socket.getOutputStream()).writeObject("Test");
      	socket.close();
      }
      

      What is the problem, and what can I do to fix it?

      V Offline
      V Offline
      Valentinor
      wrote on last edited by
      #2

      I found the problem, and it was because the antivirus was blocking network not filtered. I reinstalled it and forgat to add the rule again. But that being said, why was it working when it was in the same class? Was it automatically using localhost in that situation even tho it had a specific IP given?

      E 1 Reply Last reply
      0
      • V Valentinor

        I was doing some Socket tests, and while using localhost everything ran as I wanted. But then I tried to switch it to IP address, and well then things got weird. While the code was in the same class (Code_1 bellow), the simplified test from below was running fine, but as soon as I moved the server code in another project, even another package or class in same package, then when I'm running client code I'm getting java.net.ConnectException: Connection timed out: connect. If when the server is in another project, and for client I'm switching to localhost, then this time it is working. It doesn't want to work when they are in different projects/packages, and the client is using IP address and not localhost, but they work if both are in same project even if client has IP address or localhost.

        public static void main(String\[\] args) throws Exception {
        	Thread thread = new Thread(new Runnable() {
        		@Override
        		public void run() {
        			try {
        				ServerSocket server = new ServerSocket(port);
        				Socket socket = server.accept();
        				System.out.println(new ObjectInputStream(socket.getInputStream()).readObject());
        				socket.close();
        				server.close();
        			} catch (IOException e) {
        				// TODO Auto-generated catch block
        				e.printStackTrace();
        			} catch (ClassNotFoundException e) {
        				// TODO Auto-generated catch block
        				e.printStackTrace();
        			}
        		}
        	});
        	thread.start();
        
        	Socket socket = new Socket("IP\_ADDRESS", port);
        	new ObjectOutputStream(socket.getOutputStream()).writeObject("Test");
        	socket.close();
        }
        

        What is the problem, and what can I do to fix it?

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        Your client creates a new socket, writes some data to it, and closes it, but that is not how sockets should work. You need to establish a connection in the same way you make a telephone call: call - wait for an answer - talk - say goodbye - close. So both sides should establish the connection first, then run a loop that exchanges data. Only when both sides agree should you close the sockets.

        V 1 Reply Last reply
        0
        • L Lost User

          Your client creates a new socket, writes some data to it, and closes it, but that is not how sockets should work. You need to establish a connection in the same way you make a telephone call: call - wait for an answer - talk - say goodbye - close. So both sides should establish the connection first, then run a loop that exchanges data. Only when both sides agree should you close the sockets.

          V Offline
          V Offline
          Valentinor
          wrote on last edited by
          #4

          Then how this signalling should be done? In that simple example the server was only expecting to receive a single object and then it knew it could close because that was all. And on client side the same thing, it knew it only had to send an object and it wasn't goin to receive anything back, so it could close it as that was all.

          L 1 Reply Last reply
          0
          • V Valentinor

            Then how this signalling should be done? In that simple example the server was only expecting to receive a single object and then it knew it could close because that was all. And on client side the same thing, it knew it only had to send an object and it wasn't goin to receive anything back, so it could close it as that was all.

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            Most systems have a protocol that both ends use. So each message has some indicator or flag to tell the peer what type of message is contained in the payload. For example transferring a file between two systems then the receiving station needs to know when the last block is received.

            V 1 Reply Last reply
            0
            • L Lost User

              Most systems have a protocol that both ends use. So each message has some indicator or flag to tell the peer what type of message is contained in the payload. For example transferring a file between two systems then the receiving station needs to know when the last block is received.

              V Offline
              V Offline
              Valentinor
              wrote on last edited by
              #6

              OK, so there isn't something to specifically tell it, like socket.ImDone(), but in the context they speak to each other. In that case, I'm using something like that in main project. The example in main thread was something simple, to only address java.net.ConnectException: Connection timed out problem, which was in fact caused by antivirus firewall.

              1 Reply Last reply
              0
              • V Valentinor

                I found the problem, and it was because the antivirus was blocking network not filtered. I reinstalled it and forgat to add the rule again. But that being said, why was it working when it was in the same class? Was it automatically using localhost in that situation even tho it had a specific IP given?

                E Offline
                E Offline
                englebart
                wrote on last edited by
                #7

                I think you are on the right track here. In your ServerSocket, you did not bind to a specific address, so it likely defaulted to localhost on IPv4. This should also be addressable as 127.0.0.1 which is likely different than the IP used for your local network. Try “127.0.0.1” for your IP_ADDRESS and see if it works. There should be a variation of ServerSocket that will let you specify your local network IP address. Using the same IP for server and client should make it work. Some important commands you can learn: > route print > netstat > ipconfig

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups