Vragen? Wij staan voor je klaar!  +31 85 060 6672

How does socket set timeout () work?

How does the Django ORM generates SQL?

 How does the Django ORM generates SQL?


To set a proper timeout function on a Java socket you need to follow the following example: 

Java socket timeout

Firstly, set the SO_TIMEOUT on your Java Socket, as is shown in the function below: 

String serverName = "localhost";

int port = 8080;

// set the socket SO timeout to 10 seconds

Socket socket = openSocket(serverName, port);


To briefly explain what we are looking at: 

Enable/disable SO_TIMEOUT with the designated timeout, in milliseconds. When this option is arranged on a non-zero timeout, a read() call on the InputStream is affiliated with this the certain Socket will be blocked for only the amount of time that is settled. 

If somehow the timeout expires, the java.net.SocketTimeoutException is boosted, even though the Socket will remain valid. This option has to be activated before the starting of the blocking operation if the user wants it to have some sort of effect. 

The timeout has to be > 0. The timeout of 0 is generally defined as an infinite timeout. 

Another example of settimeout() in Java would be as follows:

defrun_client(server_address,server_port):# Ping a UDP pinger server running at the given address



  # Time out after 1 second


  print("Pinging",str(server_address),"on port",server_port)





    _, _ =client_socket.recvfrom(1024)



    print("Packet lost")


    print("RTT(Round trip time): {:.3f}ms".format((time_stop-time_start)*1000))




Arranging the Java Socket Timeout in Milliseconds:

In the above-given examples, whenever you experience calls like this with the function of the socket timeout setting that includes milliseconds. The best way to go about is to write your coding in this method:


There are many programmers who try to go about the timeout function in Java in a very complicated manner which sometimes causes problems in the programs. This is why this method is great to run a trouble-free function. 


To use the settimeout() function correctly in the Python. you need to follow the guidance and directions written below: 


The user can make a case of a socket object and call a gettimeout() method with which the user can get a default timeout value. The other method of the socket.timeout() would be the settimeout() method which specifies the timeout value. These functions are very important when the user is developing custom server applications. 

The first step of this function is to create a socket article inside a test_socket_timeout() function. Then, the user can either use the gettimeout() function or either the settimeout() function to manage the time out values.  

#!/usr/bin/env python

# Python Network Programming Cookbook -- Chapter - 1

# This program is optimized for Python 2.7. It may run on any   

# other Python version with/without modifications

import socket

def test_socket_timeout():

    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

    print "Default socket timeout: %s" %s.gettimeout()


    print "Current socket timeout: %s" %s.gettimeout()    


if __name__ == '__main__':


After the user has run the given script, then we can observe how this changes the default timeout socket function: 

$ python 1_6_socket_timeout.py 

Default socket timeout: None

Current socket timeout: 100.0

Understand the Coding in Python:

In the above-given code snippets, the user has first made a socket object by giving the socket family and socket types as the first and second cases of the socket constructor. After that, the user can easily get the socket to set the timeout value by calling the gettimeout() or one can change the value by calling the settimeout() method. The timeout rate (value) is transferred to the settimeout() function and can be in second or milliseconds as well as being zero. This method is the best way to manipulate blocking-socket operations. 

This is one of the most vivid examples of set timeout () function in Python:


# some initialization and stuff

old_timeout=socket.gettimeout()# Save

# do your stuff with socket

socket.settimeout(old_timeout)# Restore

# Etc

Delen vinden we top!