initial commit_2
This commit is contained in:
parent
8337b3bb84
commit
972101851c
8
tcp_client.py
Normal file
8
tcp_client.py
Normal file
@ -0,0 +1,8 @@
|
||||
import socket
|
||||
|
||||
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||
client.connect(('172.20.10.10', 10000))
|
||||
client.sendall(b'hello server')
|
||||
data = client.recv(1024)
|
||||
print(f"Ответ от сервера: {data.decode()}")
|
||||
client.close()
|
||||
21
tcp_server.py
Normal file
21
tcp_server.py
Normal file
@ -0,0 +1,21 @@
|
||||
import socket
|
||||
|
||||
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||
server.bind(("0.0.0.0", 10000))
|
||||
server.listen(1)
|
||||
print("TCP сервер запущен")
|
||||
|
||||
while True:
|
||||
|
||||
conn, addr = server.accept()
|
||||
print(f"Подключение от {addr}")
|
||||
|
||||
data = conn.recv(1024)
|
||||
if not data:
|
||||
break
|
||||
conn.sendall(data.upper())
|
||||
|
||||
conn.close()
|
||||
|
||||
if data.upper() == b'EXIT':
|
||||
break
|
||||
46
udp_chat.py
Normal file
46
udp_chat.py
Normal file
@ -0,0 +1,46 @@
|
||||
import socket
|
||||
import threading
|
||||
|
||||
# Константы
|
||||
BROADCAST_IP = "255.255.255.255"
|
||||
PORT = 10000
|
||||
BUFFER_SIZE = 1024
|
||||
|
||||
def send_messages(sock, username):
|
||||
while True:
|
||||
message = input()
|
||||
if message.startswith("/pm"):
|
||||
try:
|
||||
_, target_ip, private_message = message.split(" ", 2)
|
||||
sock.sendto(f"PM {username}: {private_message}".encode(), (target_ip, PORT))
|
||||
except ValueError:
|
||||
print("Неверный формат команды. Используйте: /pm <ip> <сообщение>")
|
||||
else:
|
||||
sock.sendto(f"BC {username}: {message}".encode(), (BROADCAST_IP, PORT))
|
||||
|
||||
def receive_messages(sock):
|
||||
while True:
|
||||
data, addr = sock.recvfrom(BUFFER_SIZE)
|
||||
message = data.decode()
|
||||
if addr[0] != socket.gethostbyname(socket.gethostname()):
|
||||
print(f"[{addr[0]}] {message}")
|
||||
|
||||
# Настройка сокета
|
||||
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
||||
sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
|
||||
|
||||
# Привязка к порту
|
||||
sock.bind(("", PORT))
|
||||
|
||||
# Имя пользователя
|
||||
username = input("Введите ваше имя: ")
|
||||
|
||||
# Запуск потоков для отправки и получения сообщений
|
||||
send_thread = threading.Thread(target=send_messages, args=(sock, username))
|
||||
receive_thread = threading.Thread(target=receive_messages, args=(sock,))
|
||||
|
||||
send_thread.start()
|
||||
receive_thread.start()
|
||||
|
||||
send_thread.join()
|
||||
receive_thread.join()
|
||||
8
udp_client.py
Normal file
8
udp_client.py
Normal file
@ -0,0 +1,8 @@
|
||||
import socket
|
||||
|
||||
client = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
||||
client.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
|
||||
client.sendto(b'hello server', ('255.255.255.255', 10001))
|
||||
data, _ = client.recvfrom(1024)
|
||||
print(f"Ответ от сервера: {data.decode()}")
|
||||
client.close()
|
||||
8
udp_client_2.py
Normal file
8
udp_client_2.py
Normal file
@ -0,0 +1,8 @@
|
||||
import socket
|
||||
|
||||
client = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
||||
client.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
|
||||
client.sendto(b'hello server', ('255.255.255.255', 10001))
|
||||
data, _ = client.recvfrom(1024)
|
||||
print(f"Ответ от сервера: {data.decode()}")
|
||||
client.close()
|
||||
10
udp_server.py
Normal file
10
udp_server.py
Normal file
@ -0,0 +1,10 @@
|
||||
import socket
|
||||
|
||||
server = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
||||
server.bind(('0.0.0.0', 10001))
|
||||
print("UDP сервер запущен")
|
||||
|
||||
while True:
|
||||
data, addr = server.recvfrom(1024)
|
||||
print(f"Сообщение от {addr}: {data.decode()}")
|
||||
server.sendto(data.upper(), addr)
|
||||
Loading…
Reference in New Issue
Block a user