Share python socket

hongliem579

New member
### Python Socket Programming Tutorial

**What is a socket?**

A socket is an endpoint of a bidirectional communication channel between two programs running on the same or different computers. A socket is created by a **socket()** system call.

**Types of sockets**

There are two types of sockets: **stream sockets** and **datagram sockets**.

* **Stream sockets** are used for reliable, connection-oriented communication. Data is sent in a continuous stream, and each byte of data is guaranteed to be received in the order it was sent.
* **Datagram sockets** are used for unreliable, connectionless communication. Data is sent in individual datagrams, and there is no guarantee that the datagrams will be received in the order they were sent.

**Creating a socket**

To create a socket, you use the **socket()** system call. The **socket()** function takes two arguments: the **socket family** and the **socket type**.

The socket family specifies the type of network protocol that the socket will use. The following are the most common socket families:

* **AF_INET** for IPv4
* **AF_INET6** for IPv6
* **AF_UNIX** for local communication

The socket type specifies the type of communication that the socket will use. The following are the most common socket types:

* **SOCK_STREAM** for stream sockets
* **SOCK_DGRAM** for datagram sockets

For example, the following code creates a stream socket on the IPv4 protocol:

```python
socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
```

**Binding a socket**

Once you have created a socket, you need to bind it to a port. The port number is a 16-bit integer that identifies the service that the socket will provide.

To bind a socket, you use the **bind()** system call. The **bind()** function takes two arguments: the **socket** object and the **port number**.

For example, the following code binds a socket to port 8080:

```python
socket.bind(("localhost", 8080))
```

**Listening for connections**

Once you have bound a socket, you can listen for connections from other programs. To listen for connections, you use the **listen()** system call. The **listen()** function takes one argument: the **maximum number of connections** that the socket will queue.

For example, the following code listens for up to 10 connections:

```python
socket.listen(10)
```

**Accepting a connection**

When a connection is made to your socket, you can accept it using the **accept()** system call. The **accept()** function returns a new socket object that represents the connection.

For example, the following code accepts a connection and prints the address of the remote host:

```python
conn, addr = socket.accept()
print(addr)
```

**Sending data**

Once you have accepted a connection, you can send data to the remote host using the **send()** system call. The **send()** function takes two arguments: the **data** to be sent and the **length** of the data.

For example, the following code sends the string "Hello World!" to the remote host:

```python
conn.send("Hello World!".encode())
```

**Receiving data**

You can receive data from the remote host using the **recv()** system call. The **recv()** function takes two arguments: the **maximum number of bytes** to receive and the **timeout** in seconds.

For example, the following code receives data from the remote host and prints it to the console:

```python
data = conn.recv(1024)
print(data.decode())
```

**Closing a socket**

When you are finished with a socket, you should close it using the **close()** system call. The **close()** function takes no arguments.

For example
 
Join Telegram ToolsKiemTrieuDoGroup
Back
Top