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".
Related
I am completely new to creating a network connection in java so I apologize if this is a stupid question.
I am trying to create a D&D companion in java that will allow a player to create their character and then send it to the DM so that they can view it and make changes and send it back to the player. I want to be able to make it so that any time a field is changed on one computer it will also be changed on the other computer.
After a bunch of research online I have been able to create a socket connection between the DM(server) and the player(client) and pass a message between the two but I am not sure how a socket connection works after this initial connection is made. My research has not been very clear on this. I have found many resources that have said that java closes the socket after a message has been passed and many that say that the socket stays open.
If java closes the socket then my problem is easy enough to solve because then I will just have to open a new socket every time I need to pass data making sure that I pass the IP address of the client to the server the first time I make a connection.
My real questions come in when a socket stays open.
If the socket stays open and multiple clients connect to the server, will the server just shout over the network whenever it transmits a message so that all clients receive the message? (If this is the case then I know I can just attach a username to the front of the message so that the client can determine if the server is talking to it.)
If the server does not shout then how do I specify which client I want the server to talk to?
Will I have to add a loop to my receive methods so that the client/server is constantly listening for a transmission from the server/client or will java automatically do so after I run the method the first time?
I have found many resources that have said that java closes the socket after a message has been passed
You found them where?
and many that say that the socket stays open.
All those are correct. Java never closes connections. The application closes connections.
If java closes the socket then my problem is easy enough to solve because then I will just have to open a new socket every time I need to pass data making sure that I pass the IP address of the client to the server the first time I make a connection.
It doesn't.
My real questions come in when a socket stays open.
If the socket stays open and multiple clients connect to the server, will the server just shout over the network whenever it transmits a message so that all clients receive the message?
No. It will respond via the socket that is connected to the corresponding client.
(If this is the case then I know I can just attach a username to the front of the message so that the client can determine if the server is talking to it.)
Unnecessary.
If the server does not shout then how do I specify which client I want the server to talk to?
The server responds via the same socket it read the request from.
Will I have to add a loop to my receive methods so that the client/server is constantly listening for a transmission from the server/client
No, you will have to add a thread per accepted socket, that loops reading requests until end of stream.
or will java automatically do so after I run the method the first time?
No.
You seem to have been reading some truly appalling drivel. Take a look at the Custom Networking section of the Java Tutorial.
Adding to EJP's wise answer, it might be worth clarifying:
Sounds like you (wisely) use TCP, so your Socket represents a connection between 1 server and 1 client. No "shouting". In examples such as this , when connection is established (namely, client obtains a Socket by calling "new Socket" and server obtains a Socket by calling "accept"), those Sockets are dedicated to those 2 specific endpoints. So if 10 clients connect to 1 server, the server will keep 10 Sockets and won't mix them up. A bit like a poor secretary that has 10 phones on his desk and answers them all - despite the mess, each earpiece is clearly connected to 1 customer.
The connection can hold for a while & serve several messages. It will terminate when either one of the sides calls 'socket.close', or it can be terminated by underlying 3rd parties (operating system, proxies, firewalls).
For your first version, or for simple business requirements, it's probably enough to converse over this 1 simple connection. However, for commercial critical data that requires 'assurance of delivery', you might need to invest some careful thought & possibly tools such as RabbitMQ.
Good luck:)
I have a simple TCP server which I wish to connect with a TCP client. This is all programmed in java. See Here.
I used the ServerSocket.accept() method to return a socket with a port allocated by java. (I set it to 0, thus randomly allocating the first available port). However, when I wish to connect to my server via client, I can not find the port, because it is not saved anywhere or displayed on the server page. How can I know what port to connect to?
And for future reference when I wish to connect multiple clients, or when I am unable to see the server running in a terminal, how can I view the display the port to the public?
Thank you!
If you need any more detail, please ask. I am new to all of this, and I find it hard to put these concepts into words when I am only fairly comfortable with them.
I wrote a little java program that establishes a socket connection (port 23456) over TCP between a server (pong.java) and a client (ping.java). I start the server and then the client which sends ping and the server responds with pong. This happens 50 times.
This works all fine but now i want to shut this down using a SYN Flood DoS attack with hping3, but i can't get it to work. I can easily stop a file transfer running between the client and the server over SMB with the same DoS program. The server definitely gets the SYN packages- When i attack the same port the java socket connection uses it just shuts the attack down and the java program happily finishes the 50 loops. i can't figure out why. is there some protection in java or do sockets in java work different than a TCP exchange over SMB?
I do this for a network class and i just can't figure it out. I just attack myself between 3 VMs so no one will get harmed.
I can provide the source code or further information if needed.
Thanks a lot if anyone can help.
I don't know whether it fits or not, however I wrote a server-client app and more clients were trying to connect. As long as I used the Sun JRE, it worked. With the OpenJava, I was unable to get it work - only the first client connected, the others had to wait. I was unable to figure out why, and didn't really care - installed Sun (nowadays Oracle) JRE on the server and it run smooth. So if you use Linux and OpenJava, I suggest give it a try with Oracle's JRE.
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.
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.