Download as pdf or txt
Download as pdf or txt
You are on page 1of 18

Net Programming RF Sockets, IPv4, and Simple Client/Server Programming

In this chapter, we will cover the following topics:


 Printing your machine's name and IPv4 address
 Retrieving a remote machine's IP address
 Converting an IPv4 address to different formats
 Finding a service name, given the port and protocol
 Setting and getting the default socket timeout
 Handling socket errors gracefully
 Modifying a socket's send/receive buffer size
 Printing the current time from the internet time server
 Writing an SNTP client
 Writing a simple TCP echo client/server application
 Writing a simple UDP echo client/server application
Net Programming RF Sockets, IPv4, and Simple Client/Server Programming
Net Programming RF Sockets, IPv4, and Simple Client/Server Programming
Net Programming RF Sockets, IPv4, and Simple Client/Server Programming
Net Programming RF Sockets, IPv4, and Simple Client/Server Programming
Net Programming RF Sockets, IPv4, and Simple Client/Server Programming

In this chapter, we will cover the following topics:


 Printing your machine's name and IPv4 address
 Retrieving a remote machine's IP address
 Converting an IPv4 address to different formats
 Finding a service name, given the port and protocol
 Converting integers to and from host to network byte order
 Setting and getting the default socket timeout
 Handling socket errors gracefully
 Modifying a socket's send/receive buffer size
 Changing a socket to the blocking/non-blocking mode
 Reusing socket addresses
 Printing the current time from the internet time server
 Writing an SNTP client
 Writing a simple TCP echo client/server application
 Writing a simple UDP echo client/server application
Net Programming RF Sockets, IPv4, and Simple Client/Server Programming
 Printing your machine's name and IPv4 address
 First, we need to import the Python socket library
 Then, we call the gethostname() method from the socket library and store the result in a variable

 The entire activity can be wrapped in a free-standing function, print_machine_info(), which uses the built-in socket
class methods
Net Programming RF Sockets, IPv4, and Simple Client/Server Programming
 Printing your machine's name and IPv4 address
 Every module in Python has a special attribute called __name__. The value of __name__ attribute is set to '__main__'
when module run as main program. Otherwise, the value of __name__ is set to contain the name of the module.
Net Programming RF Sockets, IPv4, and Simple Client/Server Programming

In this chapter, we will cover the following topics:


 Printing your machine's name and IPv4 address
 Retrieving a remote machine's IP address
 Converting an IPv4 address to different formats
 Finding a service name, given the port and protocol
 Converting integers to and from host to network byte order
 Setting and getting the default socket timeout
 Handling socket errors gracefully
 Modifying a socket's send/receive buffer size
 Changing a socket to the blocking/non-blocking mode
 Reusing socket addresses
 Printing the current time from the internet time server
 Writing an SNTP client
 Writing a simple TCP echo client/server application
 Writing a simple UDP echo client/server application
Net Programming RF Sockets, IPv4, and Simple Client/Server Programming
 Retrieving a remote machine's IP address
 Sometimes, you need to translate a machine's hostname into its corresponding IP address.
 If you need to know the IP address of a remote machine, you can use a built-in library function, gethostbyname(). In
this case, you need to pass the remote hostname as its parameter.

As you can see, we wrapped the main function call inside a try
except block. This means that, if some error occurs during the
execution of this function, this error will be dealt with by this
try-except block.
Net Programming RF Sockets, IPv4, and Simple Client/Server Programming

In this chapter, we will cover the following topics:


 Printing your machine's name and IPv4 address
 Retrieving a remote machine's IP address
 Converting an IPv4 address to different formats
 Finding a service name, given the port and protocol
 Converting integers to and from host to network byte order
 Setting and getting the default socket timeout
 Handling socket errors gracefully
 Modifying a socket's send/receive buffer size
 Changing a socket to the blocking/non-blocking mode
 Reusing socket addresses
 Printing the current time from the internet time server
 Writing an SNTP client
 Writing a simple TCP echo client/server application
 Writing a simple UDP echo client/server application
Net Programming RF Sockets, IPv4, and Simple Client/Server Programming
 Converting an IPv4 address to different formats
 When you would like to deal with low-level network functions, sometimes, the usual string notation of IP addresses are
not very useful. They need to be converted to the packed 32-bit binary formats.
 The Python socket library has utilities to deal with the various IP address formats.
 Here, we will use two of them: inet_aton() and inet_ntoa().
Net Programming RF Sockets, IPv4, and Simple Client/Server Programming

In this chapter, we will cover the following topics:


 Printing your machine's name and IPv4 address
 Retrieving a remote machine's IP address
 Converting an IPv4 address to different formats
 Finding a service name, given the port and protocol
 Setting and getting the default socket timeout
 Handling socket errors gracefully
 Modifying a socket's send/receive buffer size
 Changing a socket to the blocking/non-blocking mode
 Reusing socket addresses
 Printing the current time from the internet time server
 Writing an SNTP client
 Writing a simple TCP echo client/server application
 Writing a simple UDP echo client/server application
Net Programming RF Sockets, IPv4, and Simple Client/Server Programming
 Finding a service name, given the port and protocol
 Determine what network services run on which ports using either the TCP or UDP protocol?
 If you know the port number of a network service, you can find the service name using the getservbyport() socket class
function from the socket library.

port= socket.getservbyname("http")
print (port)
Net Programming RF Sockets, IPv4, and Simple Client/Server Programming

In this chapter, we will cover the following topics:


 Printing your machine's name and IPv4 address
 Retrieving a remote machine's IP address
 Converting an IPv4 address to different formats
 Finding a service name, given the port and protocol
 Setting and getting the default socket timeout
 Handling socket errors gracefully
 Modifying a socket's send/receive buffer size
 Changing a socket to the blocking/non-blocking mode
 Reusing socket addresses
 Printing the current time from the internet time server
 Writing an SNTP client
 Writing a simple TCP echo client/server application
 Writing a simple UDP echo client/server application
Net Programming RF Sockets, IPv4, and Simple Client/Server Programming
Net Programming RF Sockets, IPv4, and Simple Client/Server Programming
 Setting and getting the default socket timeout
 You can make an instance of a socket object and call a gettimeout() method to get the default timeout value and the
settimeout() method to set a specific timeout value.
 This is very useful in developing custom server applications. We first create a socket object inside a
test_socket_timeout() function. Then, we can use the getter/setter instance methods to manipulate timeout values.

You might also like