3

I have a TCP Socket running on a C# machine. I need to connect that server socket from Android via Server IP and port as below:

InetAddress serverAddr = InetAddress.getByName(serverIp);
Socket socket = new Socket(serverAddr, serverPort);
socket.setSoTimeout(10*1000);

If c# machine doesn't have socket running on Android it hangs on:

Socket socket = new Socket(serverAddr, serverPort);

I need to implement 5 seconds as timeout like if it doesn't find server socket on this ip it could simply timeout.

Thoughts please..

2 Answers 2

7

May this help you:

Create the socket with the no parameters constructor like this:

Socket socket = new Socket();

Then use

socket.connect(remoteAddress, timeout);

Another Way:

Socket socket= new Socket();   
socket.connect(new InetSocketAddress(hostip,port_num),connection_time_out); 
Sign up to request clarification or add additional context in comments.

1 Comment

Socket socket= new Socket(); socket.connect(new InetSocketAddress(hostip,port_num),connection_time_out); worked for me perfectly. Thanks Bhavin
1
InetAddress serverAddr = InetAddress.getByName(serverIp);
Socket socket = new Socket();
socket.connect(serverAddr, timeout);

1 Comment

java.net.Socket doesn't contain such constructor: new Socket(serverAddr, serverPort, timeout). Any further help please?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.