[파이썬] TCP 소켓 프로그래밍

In this blog post, we will explore TCP socket programming in Python and how to create a simple client-server application using TCP sockets.

Introduction to TCP Socket Programming

TCP socket programming allows communication between two devices over a network using TCP/IP protocol. It enables the exchange of data in a reliable and ordered manner between the client and server.

Creating a TCP Server

To create a TCP server in Python, you can make use of the socket module. Below is an example of a simple TCP server that listens for incoming connections and echoes back any received data.

import socket

server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host = '127.0.0.1'
port = 12345
server_socket.bind((host, port))
server_socket.listen(5)

print("Server listening on {}:{}".format(host, port))

while True:
    client_socket, client_address = server_socket.accept()
    print("New connection from {}:{}".format(client_address[0], client_address[1]))
    
    while True:
        data = client_socket.recv(1024)
        if not data:
            break
        client_socket.sendall(data)
    
    client_socket.close()

The socket.socket() function is used to create a new socket object with the specified address family and socket type. In this case, we initialize a TCP socket using socket.AF_INET for the address family and socket.SOCK_STREAM for the socket type.

We then bind the server socket to a specific host and port number using the bind() method. After that, we call the listen() method to start listening for incoming connections. The parameter passed to listen() specifies the maximum number of queued connections.

Once a new connection is accepted using accept() method, we create a new client socket for that connection. We then continuously receive data from the client using recv() and send it back using sendall() until the client closes the connection.

Creating a TCP Client

To create a TCP client in Python, you can also use the socket module. Here is an example of a simple TCP client that connects to a server and sends data to it.

import socket

client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_host = "127.0.0.1"
server_port = 12345
client_socket.connect((server_host, server_port))

message = "Hello, server!"
client_socket.send(message.encode('utf-8'))

response = client_socket.recv(1024)
print("Received response from server: {}".format(response.decode('utf-8')))

client_socket.close()

We create a new client socket using socket.socket() with the same address family and socket type as the server. Then, we use the connect() method to establish a connection with the server by specifying the server’s IP address and port number.

Next, we send a message to the server using send() after encoding it to bytes. We then receive the server’s response using recv() and decode it back to a string before printing it.

Finally, we close the client socket.

Conclusion

In this blog post, we explored TCP socket programming in Python and learned how to create a TCP server and a TCP client. TCP socket programming enables reliable communication between devices over a network, making it a powerful tool for building network applications.

Remember to always properly handle exceptions and close the sockets after usage to ensure efficient and secure network communication.