logical accept() method issue
Hey everyone,
I've got a probably stupid problem thats been bugging me. I have a server and a client class that can connect with sockets.
As long as its used in eclipse it works as a charm because you can run the java files separataly. But I need to export it into a jar file and that gives me headaches.
The server has the method accept() that waits on a connection by the client. But the code stops at the accept until there is a connection. How is the client ever going to connect if the code stops at accept() ?
I absolutelly see no solution to this. In eclipse you just run the server that will stop and wait when calling the accept() method and you can then click on the client to make the connection. But the client needs to auto connect one way or another when its put in a jar.
I've been working my code in a lot of ways but still nothing. Making a global class where the 2 classes 'meet' isn't really helpfull since again calling accept() stops the whole process.
Synchronizing also hase the same problem, first you need to accept before you can connect but if you accept, this method will hold until connected.
Does anyone have a clue on what to do with this kind off problem?
Greetz
Re: logical accept() method issue
Quote:
But the code stops at the accept until there is a connection. How is the client ever going to connect if the code stops at accept()
It is supposed to wait until a client connects - read the API for the accept method. It sounds like your client and server are in the same programmatic flow (which means for one thing using a Client and Server is not necessary) - you provide not code to verify if this is the case, and without code as an SSCCE we guess, and guesses can often times be incorrect. If somehow I managed to guess correctly, the client must be in a separate Thread than that of the server.
Re: logical accept() method issue
I took this one as an example:
Java Socket Programming Example
This works under eclipse but as you can see it gives trouble when using them in the commandline.
This should be the base of a proxy server with this client/server classes as the proxy.
Re: logical accept() method issue
So, do they both reside in different jar's? Do you starting the server first, then starting the client?
Re: logical accept() method issue
I didn't try this in jar's yet because of the 2 mains. Also in eclipse you can start them manually but in the commandline it should be done with a single command that will make them both running.
Re: logical accept() method issue
If you have both the server and the client running in the same JVM you're running into troubles of course. Run both in separate JVMs and see what happens. The server should block until a socket was accepted and the client should block until its socket was accepted by a server.
kind regards,
Jos
Re: logical accept() method issue
You need 2 threads - either 2 different JVM instances (eg you run them separately) or 2 threads within the application itself.