Socket client how setSoTimeout - java

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);

Related

ServerSocket vs Socket -- closing the socket

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

Android connecting server, how do I stop the socket after a certain amount of time?

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);

Set timeout on Socket before it tries to connect

The Socket class documentation is here.
In my code atm, I use a constructor that's like this:
Socket m_Socket = new Socket(m_Address, m_Port);
m_Address is an InetAddress and m_Port is an int.
When this line runs, and the socket cannot be made, the app waits 3 seconds or so before throwing an IOException.
I can see there is no constructor that takes both InetAddress, int, and another int for timeout. I need to wait 250ms and not ~3 seconds as it is now. This means that I need to set the timeout on the socket, but I cannot find any method to do this. I know we have the method setSoTimeout(timeout), but it needs to be called on an instance of the Socket class. I can instantiate a new Socket by doing this: m_Socket = new Socket();, but then I'd need to set the InetAddress and port, and the Socket class does not seem to have any methods for doing this (except for the constructor).
How do I set the timeout before it actually tries to set the socket?
You may create an unconnected Socket with the default constructor and then call connect() with timeout.
Socket m_Socket = new Socket();
m_Socket.connect(addr,1000);
try
Socket sock = new Socket();
sock.connect(new InetSocketAddress(m_Address, m_Port), 250);

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.

Why it cannot find getInputStream?

I have this code:
ServerSocket serverSideSocket = new ServerSocket(1234);
serverSideSocket.accept();
BufferedReader in = new BufferedReader(new InputStreamReader(serverSideSocket.getInputStream()));
And compiler writes me that it cannot find "getInputStream". I do not understand why. In the beginning of my code I do import java.net.*.
Calling of accept returns instance of Socket which has required method getInputStream.
The code might look like this:
ServerSocket serverSideSocket = new ServerSocket(1234);
Socket socket = serverSideSocket.accept();
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
Great tutorial how to work with sockets in java: http://java.sun.com/docs/books/tutorial/networking/sockets/index.html
This because conceptually a ServerSocket doesn't provide a direct connection object that can be used to send and receive data. A ServerSocket is a tool that you can use with the .accept() method to let it listen on the choosen port and generate a new real connection when a client tries to connect.
That's why you can't get an InputStream from a ServerSocket. Since many clients can connect to the same server, every client will make the server socket generate a new Socket (that is an opened TCP connection) that is returned from .accept() through which you can send and receive using its InputStream and OutputStream.

Categories