Chat server stops working - java

I have created a normal chat program which has just a server and a client class. I run the server at my end. The chat clients are run from different machines. In my program, I've specified a random port number which all the clients use a socket connection to connect to the server that runs on my machine. The first issue is that I've to disable firewall to get this working (probably, the firewall blocks the port I give). How to specify a port number that firewall can accept? Do I HAVE to open a port myself?
Secondly, after disabling firewall, everything works but all of a sudden, the connection is lost. None of the clients can send messages. What could possible be the reason for this? Not sure if it is caused due to the port I select.

You need to set Socket connection timeout properly using this.socket.setSoTimeout(timeOut);
to prevent timeout which must be causing connection loss.
In order to allow socket comunication through firewall go through this document : http://windows.microsoft.com/en-US/windows7/Allow-a-program-to-communicate-through-Windows-Firewall

Related

TCP socket on server is getting closed/not showing in LISTEN state

I am facing the problem regarding socket close. I am able to create the socket and everything is working fine for some extent of time. When I try to connect to server, my clients are connecting and performing all the operations well. But after some hours the state of the socket is not showing in LISTEN mode. So that all the clients are stopped performing their functions and getting error as "could not connect to "server_ip:port_no"".
I am not getting what exactly is happening. I am using jacorb API for this.
Without calling close() on socket it is getting terminated. Why the port is not showing in listen mode? I have checked the status of the port using command "netstat -anp | gerp port_no".
Normally, a server's listening port has a finite number of allowable connections, and when all of them have been granted to connection requests coming from the clients, the listen port will be pulled so that any new connection request will be denied. It looks like that was what have happened to your server. To "see" the listen port again, the client needs to release the connections to the listen port when they are no longer needed.

Java + ServerSocket + Firewall

I've got a server written on Java with ServerSocket.
And I have a client that is over a corporative firewall that is blocking everything except common ports.
I've started server on SMTP port (#25).
The user with firewall connects to it and so far everything is ok.
Then the server processes ServerSocket.accept(). And as far as I understand it creates a socket on a random port (every time the port number is different). And fails because of a firewall.
My question is - how can I make ServerSocket.accept() to choose a port for a socket from my white-list? I understand that it will not suite for massive online, but I want to make one my friend to be able to connect to my server.
Is it possible? And how?
server socket doesn't choose random port. it is the client socket which chose the random port. my guess is that your fire wall is smart and it knows to detect if the connection is approved by some sort of dpi (deep packet inspection).
if you want to mislead it, you can try to run data which looks like smtp in the first 2-3 packets and then switch to your protocol.
Another option it to use a kind of a connector outside the system, in this case both machines are clients which are connected through a 3rd client (there are many such proxies)

Multiple java socket connections

I have created this game bot where it connects to the game, and starts playing.
My problem is that i can't start more than one of these as the other then won't work.
Is is possible that if i run 2 instances of the same program the sockets are interfering with each other ? After all, they do connect to the same IP with the same port ?
And sometimes after i close(just closing cmd) the program is unable to connect again. Is that cause i didn't close the connections right ?.
I hope this is enough else i'll just have to post my source code
Best regards.
It's possible to connect to the same socket/port several times. Actually a socket is a double peer: {client ip/ client port}{server ip/server port}. When you connect to a server, your client port is assigned dynamically. You will have a new and different client port per client. So it should work unless the server side forbid it.
You should have a server that listens for multiple connections. A server is bound to a port and once that port is in use another application cannot use it. So for the server just have one instance. Multiple clients can connect to this IP/Port as long as the Server accepts multiple connections.
If a client connects to the server and the other clients stop working this may be because the server does not support multiple clients. To do this you need to use multi threading in the server. The server should accept a client socket and create a new instance of a client with it's own StreamReader/Writer objects.
http://tutorials.jenkov.com/java-multithreaded-servers/multithreaded-server.html
if you are working with a specific TCP port, then there is a close-wait period that this port cannot be claimed temporariliy for some time. also multiple programs cannot listen the same TCP port. Use threads.

Java - How to keep a live socket connection

Ok guys im not that experienced so take it easy on me.
Ok so i have 2 programs, one for the server (my pc) and one for the client(other pcs)
and this is the setup
server listens/accepts > client connects > server sends command to client > client executes.
and thats it, after that it disconnects BUT i need the client to stay connected so the server can keep sending commands as needed.
How can i achieve this?
I can provide more info if need just ask!
thanks for all the help guys.
The server will receive a socket when accept() returns, and as long as that socket does not get closed then the connection remains open. The client will then run in a loop of read/execute-command until the server closes the connection. I can provide more details if needed.
There's a few official examples to get you started with Java sockets. Plus since its Java, getting this setup is pretty easy.
As Kevin already said, you'll open up a ServerSocket on some open port. Then call accept() which will return when a client connects with a Socket on the same port as the ServerSocket.
Also, make sure you pay attention to the specified host if you want the client program to connect from a computer different from the host. You might run into trouble with the loopback interface if you specify the host as "null" or "127.0.0.1".

Checking a local TCP port is not open in Java

Is there a simple way to make sure that a local port is not already open. Some TCP socket servers (eg Grizzly) don't seem to do this check by default. When this check is missing, the server appears to start and respond, but the client code is just connecting to an old server that wasn't shutdown. This can be very bad!
Is there a simple line of Java code that could check to be sure that port isn't already used by another process?
I see two obvious ways to do it:
try to connect to that port on localhost, if you get accepted, the the port is being used
try to open a ServerSocket in listen mode in that port. If you get "already bound" exception, then the port is being used
Hope it helps.

Categories