socket programming (online automatic Chat tool) --Python3

Posted by leena on Sun, 30 Jun 2019 21:08:50 +0200

Catalog

@

1. The server supports only one client connection

Server-side program (server.py):

import socket

# Create a server-side socket object
server = socket.socket()
# Bind IP (ipocnfig command looks at IP address) and port (custom network port can be used without conflicting with an existing system port)
server.bind(('192.168.5.106', 8000))
# You can wait for five connections
server.listen(5)
# Wait for client to connect, if not, wait in order
# conn: The object through which the client connects to the server to send and receive data
# addr: client address information
print('Server is already running,Waiting for client to connect...')
conn, addr = server.accept()  # This is where the blockage occurs, and only when the client connects, the client connection is acquired and the communication begins.
print('Someone connected!')
data = conn.recv(1024)  # 1024: The server fetches data up to 1024 bytes at a time
print('The client sent me:', data)
conn.send(b'May I help you?')
conn.close()  # Disconnect from client
server.close()  # Shut down the service on the server side

Client program (client.py):

import socket

# Create client socket object
client = socket.socket()
client.connect(('192.168.5.106', 8000))  # Blocked, code will not continue until the connection is successful
client.send(b'hello')  # Send a message to the server after connecting to it
data = client.recv(1024)  # Messages waiting to be sent by the server
print('The server replied to me:',data)
client.close()  # Close Connection

Server side running:

Server is running, waiting for client to connect...
Someone connected!
Client sent me: b'hello'

Client Run:

The server replied to me: b'May I help you?'

2. Server supports multi-client connections

Server (server.py):

import socket

# Create a server-side socket object
server = socket.socket()
# Bind IP and Port
server.bind(('192.168.5.106', 8000))
# You can wait for five connections
server.listen(5)
# Wait for client to connect, if not, wait in order
# conn: The object through which the client connects to the server to send and receive data
# addr: client address information
while True:
    print('Server is already running,Waiting for client to connect...')
    conn, addr = server.accept()  # This is where the blockage occurs, and only when the client connects, the client connection is acquired and the communication begins.
    print('Someone connected!')
    data = conn.recv(1024)  # Byte type
    resp = data + ',Hello!'.encode('utf-8')
    print('Sent "', resp.decode('utf-8'), '"To the client...')
    conn.send(resp)  # Byte type
    conn.close()
    print('The server disconnected...', '\n')

Client program (client.py):

import socket

# Create client socket object
client = socket.socket()
client.connect(('192.168.5.106', 8000))  # Blocked, code will not continue until the connection is successful
name = input('Enter your name:')
client.send(name.encode('utf-8'))  # Converts a string to a byte, which is sent to the server when it is connected
resp = client.recv(1024)  # Blocking, waiting for messages from the server
print(resp.decode('utf-8'))
client.close()  # Close Connection

Server side run record:

Server is running, waiting for client to connect...
Someone connected!
"Thanlon, Hello!" sent to client...
The server disconnected...

Server is running, waiting for client to connect...
Someone connected!
"Hello Kiku!" sent to client...
The server disconnected...

Server is running, waiting for client to connect...

Client 1 run record:

Enter your name: Thanlon
Hello, Thanlon!
Client 1 runs:

Client 2 run record:

Enter your name: Kiku
Hello, Kiku!

3. Online chat tools

Server-side program (server.py):

import socket

# Create a server-side socket object
server = socket.socket()
# Bind IP and Port
server.bind(('192.168.5.106', 8000))
# You can wait for five connections
server.listen(5)
# Wait for client to connect, if not, wait in order
# conn: The object through which the client connects to the server to send and receive data
# addr: client address information
while True:
    print('\n System Tip: Server is already running,Waiting for client to connect...')
    conn, addr = server.accept()  # This is where the blockage occurs, and only when the client connects, the client connection is acquired and the communication begins.
    print('System Tips:' + str(list(addr)) + 'The client has connected to this server...\n')
    while True:
        data = conn.recv(1024)  # Byte type
        if data == b'0':
            break
        print('System Tip: Received the message from the client: "' + data.decode('utf-8') + '"')
        resp = data + ',Hello!'.encode('utf-8')
        print('System Tip: Sent "', resp.decode('utf-8'), '"To the client...')
        conn.send(resp)  # Byte type
        print('System Tip: and' + str(list(addr)) + 'The client's connection is not closed, so you can continue to have a conversation with the client...')
    conn.close()
    print('System Tip: and' + str(list(addr)) + 'The client connection is closed...')

Client program (client.py):

import socket

# Create client socket object
client = socket.socket()
client.connect(('192.168.5.106', 8000))  # Blocked, code will not continue until the connection is successful
while True:
    name = input('System prompt, please enter your name(Enter 0 to close the connection): \n')
    client.send(name.encode('utf-8'))  # Converts a string to a byte, which is sent to the server when it is connected
    if name == '0':
        break
    resp = client.recv(1024)  # Blocking, waiting for messages from the server
    print('System Tip: Client replies "' + resp.decode('utf-8') + '"')
client.close()  # Close Connection
print('System Tip: Close the client connection!')

Server side run record:

System Tip: Server is running, waiting for client to connect...
System Tip: ['192.168.5.106', 57892] The client has connected to this server...

System Tip: Received message from client:'Thnlon'
System Tip: Sent "Thnlon, Hello!" to client...
System Tip: The connection with ['192.168.5.106', 57892] client is not closed, you can continue to have a conversation with this client...
System Tip: Received message from client:'Kiku'
System Tip: Sent "Hello Kiku!" to client...
System Tip: The connection with ['192.168.5.106', 57892] client is not closed, you can continue to have a conversation with this client...
System Tip: The connection to the ['192.168.5.106', 57892] client has been closed...

System Tip: Server is running, waiting for client to connect...
System Tip: ['192.168.5.106', 57939] The client has connected to this server...

System Tip: Received message from client:'Maria'
System Tip: Sent "Maria, Hello!" to client...
System Tip: The connection with ['192.168.5.106', 57939] client is not closed, you can continue to have a conversation with this client...
System Tip: The connection to the ['192.168.5.106', 57939] client has been closed...

System Tip: Server is running, waiting for client to connect...

Client 1 side running record:

System prompt, please enter your name (enter 0 to close the connection):
Thnlon
System Tip: Client replies "Thnlon, Hello!"
System prompt, please enter your name (enter 0 to close the connection):
Kiku
System Tip: Client replies "Hello Kiku!"
System prompt, please enter your name (enter 0 to close the connection):
0
System Tip: Close the client connection!

Client 2 side running record:

System prompt, please enter your name (enter 0 to close the connection):
Maria
System Tip: Client replies "Hello Maria!"
System prompt, please enter your name (enter 0 to close the connection):
0
System Tip: Close the client connection!

Of course there can be as many clients...

Topics: PHP socket network