ServerSocket vs Socket -- closing the socket - java

Both ServerSocket and Socket have the method close() to close the socket.
What is the difference between the two?
Suppose, on the server side,
ServerSocket serverSocket = new ServerSocket(port);
Socket socket = serverSocket.accept();
...
In this case, how is
socket.close();
different from
serverSocket.close();
if any?
TIA.

ServerSocket sets up a listener from which you can accept any number of Socket connections. Closing a Socket closes that one connection; closing the ServerSocket means the listener is closed and can no longer accept connections to that port.

java.net.ServerSocket
This class implements server sockets. A server socket waits for requests to come in over the network. It performs some operation based on that request, and then possibly returns a result to the requester.
java.net.Socket
This class implements client sockets (also called just "sockets"). A socket is an endpoint for communication between two machines.
In simple words, socket is from client side and serversocket is from server side
Check here

Related

Socket client how setSoTimeout

I need to set time for max connection time in a socket client. But if I do like the following code, does not work, because line one open a connection and is a blocking function and never run line two.
How can I setSoTimeout before open connection?
Socket s = new Socket(server.host, server.port);
s.setSoTimeout(server.time);
Socket socket=new Socket();
socket.connect(new InetSocketAddress(host,port),timeout);

What's happening when server.accept()

I'm trying to write a simple Java chat application in Server/Client.
I'm confusing in below method at server.accept() :
private void waitForConnection() throws IOException {
showMessage("Waiting for someone to connect... \n");
// `connection` is an instance of `java.net.Socket`
// `server` is an instance of `java.net.ServerSocket`
connection = server.accept();
showMessage("Now connected to " + connection.getInetAddress().getHostName());
}
Please tell me connection is equal to what?
And also server.accept() returns what?
Any helps would be awesome.
Assuming your server variable is a java.net.ServerSocket then the accept() method returns a java.net.Socket object.
From the returned Socket object, you have access to both the InputStream and OutputStream to read from and write to the connected client.
Your program should halt until a client connects. That's what the line connection = server.accept(); does. Returning type is a type of Socket, too.
That's the "connection" to your client, you can read from and write to.
Check this and that site to read more about network programming in Java.
when you do connections between two systems then you require a socket.
Socket of one system is connected with socket of another system. Both these sockets are connected via I/O stream. you can write to this stream and can read from this stream.
One system serves as server and another system serves as client .
As socket is combination of port no. and IP so server open its port no. and client try to connect with the server's IP and port no.
For connection to be maid the server accepts the incoming socket using accept() function. accept() function returns a local socket which is connected to another socket at the client..
accept() waits until a client socket arrives.

Multithreaded connections handling

I want to write a server which listens on the given port for connections and puts sockets into BlockingLinkedQueue from which the consumer thread will read messages. I accept incoming connections in this way:
ServerSocket serverSocket = new ServerSocket(port);
while (true)
{
Socket socket = null;
socket = serverSocket.accept();
queue.put(socket);
}
When I try to connect in parallel from two separate hosts it occurs that responses to the first one are sent to the second one after establishing the second connection. When I change my code to this listed below the second connection is merely refused:
while (true)
{
ServerSocket serverSocket = new ServerSocket(port);
Socket socket = serverSocket.accept();
queue.put(socket);
}
My questions are:
What's the difference between this two situations? Why in the first situation messages are sent to the second host?
How should I refactor my code in order to create separate connections between my server and both hosts and handle them in parallel?
What's the difference between this two situations?
In the first case, you are using the same port for multiple connections. In the second case, you are discarding the server port so any waiting connections for be refused.
Why in the first situation messages are sent to the second host?
Due to a bug in code, not shown here.
How should I refactor my code in order to create separate connections between my server and both hosts and handle them in parallel?
Create a thread for each connection.

Restart a TCP Server

I have a TCP server in my android application. The server starts running immediately. With one button click, i want to stop the server and with another one, restart the server again.
My server is like this:
serverSocket = new ServerSocket(1292);
while (true) {
Socket client = serverSocket.accept();
ServersClient s = new ServersClient(client);
Thread clientThread = new Thread(s);
clientThread.run();
}
I successfully handle closing with this code:
serverSocket.close();
After this code, no clients can connect.
What should i do to restart it now?
ServerSocket listens for new connections.
Socket is the connection instance that serverSocket.accept() returns.
You want to close the Socket instance to close connection with a particular client.
Closing ServerSocket will unbind the application from the port.
A ServerSocket can not be opened again after it has been closed. Therefore you just execute your first code snippet again, starting with the creation of a new ServerSocket instance.

ServerSocket constructor in laymans terms

What would this statement do:
ServerSocket ss=new ServerSocket(4646);
Please explain in layman terms.
The statement effectively tells the JVM to listen on port specified (4646) for incoming connections. By itself it doesn't mean anything since you will have to take incoming connections to that port and use them to build normal Socket objects that will be then used for ingoing/outgoing data.
You could say that the ServerSocket is the object through which real TCP sockets between clients and the server are created. When you create it, the JVM hooks to the operating system telling it to dispatch connections that arrive on that port to your program.
What you typically do is something like:
public AcceptThread extends Thread {
public void run() {
ServerSocket ss = new ServerSocket(4646);
while (true) {
Socket newConnection = ss.accept();
ClientThread thread = new ClientThread(newConnection);
thread.start();
}
}
}
So that you will accept incoming connections and open a thread for them.
Straight from the ServerSocket Java docs:
Creates a server socket, bound to the specified port.
What's a server socket?
This class implements server sockets. A server socket waits for requests to come in over the network. It performs some operation based on that request, and then possibly returns a result to the requester.
public ServerSocket(int port) throws IOException
documentation:
Creates a server socket, bound to the
specified port. A port of 0 creates a
socket on any free port.
That would bind your ServerSocket to port 4646 on the local machine.
You could then accept sockets on this connection with
// pick up server side of the socket
Socket s = ss.accept();
Now, your client can connect to your server, establishing a socket connection, like this
// pick up client side of the socket, this is in a different program (probably)
Socket connectionToServer = new Socket("myserver",4646);

Categories