134 lines
4.8 KiB
Python
134 lines
4.8 KiB
Python
|
|
# import socket
|
||
|
|
# import time
|
||
|
|
|
||
|
|
# def handle_client(client_socket, client_address):
|
||
|
|
# print(f"Connected by {client_address}")
|
||
|
|
|
||
|
|
# with client_socket:
|
||
|
|
# while True:
|
||
|
|
# try:
|
||
|
|
# recv_data = client_socket.recv(1024).decode()
|
||
|
|
|
||
|
|
# if not recv_data:
|
||
|
|
# print(f"Connection closed by {client_address}")
|
||
|
|
# break
|
||
|
|
|
||
|
|
# print(f"Received from {client_address}: {recv_data}")
|
||
|
|
|
||
|
|
# if recv_data == 'get <socket_num>':
|
||
|
|
# send_data = '<socket_num><2>'
|
||
|
|
# client_socket.send(send_data.encode())
|
||
|
|
|
||
|
|
# if recv_data == 'get <socket_str>':
|
||
|
|
# send_data = '<socket_str><"b">'
|
||
|
|
# client_socket.send(send_data.encode())
|
||
|
|
|
||
|
|
# if recv_data == 'get <socket_arr>':
|
||
|
|
# send_data = '<socket_arr><[1,1,1,1,1,1]>'
|
||
|
|
# client_socket.send(send_data.encode())
|
||
|
|
|
||
|
|
# if recv_data == 'get #real#6#':
|
||
|
|
# send_data = '[2,2,2,2,2,2]'
|
||
|
|
# client_socket.send(send_data.encode())
|
||
|
|
|
||
|
|
# if recv_data == 'palletType':
|
||
|
|
# send_data = '<palletType><1>'
|
||
|
|
# client_socket.send(send_data.encode())
|
||
|
|
|
||
|
|
# except ConnectionResetError as e:
|
||
|
|
# print(f"Connection closed by {client_address}")
|
||
|
|
# break
|
||
|
|
|
||
|
|
# def start_server(host='0.0.0.0', port=12345):
|
||
|
|
# server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||
|
|
# server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
||
|
|
# server_socket.bind((host, port))
|
||
|
|
# server_socket.listen()
|
||
|
|
# server_socket.settimeout(1)
|
||
|
|
# print(f"Server listening on {host}:{port}")
|
||
|
|
|
||
|
|
# try:
|
||
|
|
# while True:
|
||
|
|
# try:
|
||
|
|
# client_socket, client_address = server_socket.accept()
|
||
|
|
# handle_client(client_socket, client_address)
|
||
|
|
|
||
|
|
# except socket.timeout:
|
||
|
|
# continue
|
||
|
|
# except KeyboardInterrupt:
|
||
|
|
# print("Server shutting down...")
|
||
|
|
# finally:
|
||
|
|
# server_socket.close()
|
||
|
|
|
||
|
|
# if __name__ == "__main__":
|
||
|
|
# start_server()
|
||
|
|
import socket
|
||
|
|
import time
|
||
|
|
|
||
|
|
def handle_client(client_socket, client_address):
|
||
|
|
print(f"Connected by {client_address}")
|
||
|
|
|
||
|
|
with client_socket:
|
||
|
|
while True:
|
||
|
|
try:
|
||
|
|
recv_data = client_socket.recv(1024).decode()
|
||
|
|
|
||
|
|
if not recv_data:
|
||
|
|
print(f"Connection closed by {client_address}")
|
||
|
|
return False # 클라이언트 종료 신호 반환
|
||
|
|
|
||
|
|
print(f"Received from {client_address}: {recv_data}")
|
||
|
|
|
||
|
|
if recv_data == 'get <socket_num>':
|
||
|
|
send_data = '<socket_num><2>'
|
||
|
|
client_socket.send(send_data.encode())
|
||
|
|
|
||
|
|
if recv_data == 'get <socket_str>':
|
||
|
|
send_data = '<socket_str><"b">'
|
||
|
|
client_socket.send(send_data.encode())
|
||
|
|
|
||
|
|
if recv_data == 'get <socket_arr>':
|
||
|
|
send_data = '<socket_arr><[1,1,1,1,1,1]>'
|
||
|
|
client_socket.send(send_data.encode())
|
||
|
|
|
||
|
|
if recv_data == 'get #real#6#':
|
||
|
|
send_data = '[2,2,2,2,2,2]'
|
||
|
|
client_socket.send(send_data.encode())
|
||
|
|
|
||
|
|
if recv_data == 'palletType':
|
||
|
|
send_data = '<palletType><1>'
|
||
|
|
client_socket.send(send_data.encode())
|
||
|
|
|
||
|
|
except ConnectionResetError:
|
||
|
|
print(f"Connection closed by {client_address}")
|
||
|
|
return False # 클라이언트 종료 신호 반환
|
||
|
|
return True # 클라이언트 정상 처리 완료 신호 반환
|
||
|
|
|
||
|
|
|
||
|
|
def start_server(host='0.0.0.0', port=12345):
|
||
|
|
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||
|
|
server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
||
|
|
server_socket.bind((host, port))
|
||
|
|
server_socket.listen()
|
||
|
|
server_socket.settimeout(1)
|
||
|
|
print(f"Server listening on {host}:{port}")
|
||
|
|
|
||
|
|
try:
|
||
|
|
while True:
|
||
|
|
try:
|
||
|
|
client_socket, client_address = server_socket.accept()
|
||
|
|
connection_active = handle_client(client_socket, client_address)
|
||
|
|
if not connection_active:
|
||
|
|
print("Client disconnected. Shutting down server...")
|
||
|
|
break # 루프 종료
|
||
|
|
except socket.timeout:
|
||
|
|
continue
|
||
|
|
except KeyboardInterrupt:
|
||
|
|
print("Server shutting down...")
|
||
|
|
finally:
|
||
|
|
server_socket.close()
|
||
|
|
print("Server socket closed.")
|
||
|
|
|
||
|
|
if __name__ == "__main__":
|
||
|
|
start_server()
|