Keywords: Socket, Socket programming, TCP/UDP
Dictionary meaning of a Socket – an electrical device receiving a plug or light bulb to make a connection.
In computer terminology, when a computer program needs to connect to Internet, it uses a software component called a socket. The socket opens the network connection for the program, allowing data to be read and written over the network. Please note that these sockets are software networking sockets, unlike hardware sockets these sockets will not give any shock ☠ 😄.
When two computers or processes want to communicate they can do that through Sockets. Sockets allow processes to communicate with each other using a file descriptor and are commonly used in client-server applications that allow for communication between multiple applications. They are used with different network protocols like HTTP, FTP, telnet, and e-mail, multiple sockets can be opened at a time.
Now hope you understood what a socket is !. Lets go ahead and understand how sockets are created and configured.
Socket = IP Address + Port Number
A Port is a communication endpoint that identifies a specific process or a type of network service. Ports are identified for each protocol and address combination by 16-bit unsigned numbers, commonly known as the Port number. The most common protocols that use port numbers are the TCP and the UDP.
A port number is always associated with an IP address of a host and the protocol type of the communication. It completes the destination or origination network address of a message.
An Internet Protocol Address (IP address) is a numerical label assigned to each device connected to a computer network that uses the Internet Protocol for communication. An IP address serves two main functions:
- Host / Network interface identification
- Location addressing.
So every computer is identified by using an IP Port.
Most inter-process communication uses the client server model. These terms refer to the two processes which will be communicating with each other. One of the two processes, the client, connects to the other process, the server, typically to make a request for information. A good analogy is a person who makes a phone call to another person.
The steps involved in establishing a socket on the client side are as follows:
- Create a socket
- Connect the socket to the address of the server
- Send and receive data
The steps involved in establishing a socket on the server side are as follows:
- Create a socket
- Bind the socket to an address on the host machine.
- Listen for connections
- Accept a connection
- Send and receive data
Lets understand how to download a web page using HTTP GET request in MicroPython.
Prerequisites:
- ESP8266 hardware
- uPyCraftIDE
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
''' Author : Seetha Akella Purpose : Establishing Socket on Client side using HTTP GET request Language : MicroPython Hardware : ESP8266 ''' import socket url = 'https://techawarey.com/programming/virtual-serial-port/' _, _, host, path = url.split('/', 3) addr = socket.getaddrinfo(host, 80)[0][-1] # Create a socket s = socket.socket() # Connect to IP address s.connect(addr) # Send GET request s.send(bytes('GET /%s HTTP/1.0\r\nHost: %s\r\n\r\n' % (path, host), 'utf8')) while True: # Receive data data = s.recv(1024) if data: print(str(data, 'utf8'), end='') else: break # Close the socket s.close() |
This code would retrieve the webpage and print the HTML code to the console in pyCraftIDE.
Hope you understood how to configure sockets using HTTP connection. Next posts would be on TCP and UDP socket connections.
THANK YOU and many more posts coming your way!
Please put your queries in comment section, I will get back to you with answers ASAP.
Please like and share Techawarey. Find and Like Techawarey on Facebook.😄
[…] to🤔socket programming. There are two widely used Socket types, stream sockets, and datagram sockets. Stream sockets […]