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.
Related
How can a Server open a TCP connection through a Client which was connected to server before? Let say we have simple server like the code shows below; server waits for client connections and a handler thread serves connected clients. Client as below, every time wants to do an operation with the server, reestablish the connection, communicate with server and then close the connection. So, if server would like to open a TCP connection through the client(when there is no an active connection from client to server), how it can be done?
Server Code
public class DServer {
public static void main(String[] args) {
ServerSocket serverSocket = new ServerSocket(7800);
while(true) {
Socket socket = serverSocket.accept();
System.out.println("Server established a new connection");
HandleConnection handler = new HandleConnection(socket);
handler.start();
}
}
}
Client Code
public class DClient{
public static void main(String[] args) {
Socket socket = new Socket("localhost", 7800);
ObjectOutputStream out = new ObjectOutputStream(socket.getOutputStream());
ObjectInputStream in = new ObjectInputStream(socket.getInputStream());
//for every operation you do, first reestablish the connection,
//communicate with server, then close the connection.
}
}
I think you have some fundamental misunderstandings about what a client is and what a server is in a networking sense.
The server is the application listening for connections. The client is the application creating connections to the server. If you want both of your applications to be able to initiate communication at will, then your applications will need to be both clients and servers.
So your "DClient" application will need to have a ServerSocket. You "DClient" application can then inform your "DServer" application of it's host and port for when "DServer" wants to create a connection (note that things like NATing can make this sort of thing problematic if both applications are not in the same network).
Alternately, depending on your use case, you could just leave the connection from client to server open. This is the more usual approach to letting the server push data.
There is solution to create proxy server which is manage the both request client and server which called middleware for both client request.
In network programming it must required to fist server start and then client request because client server architecture.
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
I'm developing a personal android application used at home for my own functionality.
I am trying to make it so that if the Server at home is either down or non-functional, it will disconnect after 3 seconds.
Here's my code:
Socket socket = new Socket("10.0.2.2", 26753);
socket.setSoTimeout(3000);
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
out.println("y" + e.getText().toString());
out.println("u" + a.getText().toString());
out.println("EOF");
socket.close();
Ignore the EOF at the end. That is there so that my server knows when to close the socket
So I've put setSoTimeout to 3000 (3 seconds), but android application doesn't respond. Can I put it in a thread, how would I do it?
You may want to try using a connect timeout:
Socket socket = new Socket();
socket.setSoTimeout(3000);
socket.connect(new InetSocketAddress("10.0.2.2", 26753), 3000);
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.
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);