mystic-agit 개발 블로그

[RemoteAccess] 모바일에서 Windows Application 원격 제어하는 시스템 구현 - Server 구현 (3) 본문

Platform/RemoteAccess

[RemoteAccess] 모바일에서 Windows Application 원격 제어하는 시스템 구현 - Server 구현 (3)

mystic-agit 2024. 8. 21. 17:21

 

연관글 목록
[RemoteAccess] 모바일에서 Windows Application 원격 제어하는 시스템 구현 - 시스템 구상 (1)
[RemoteAccess] 모바일에서 Windows Application 원격 제어하는 시스템 구현 - Mobile Application 구현 (2)
[RemoteAccess] 모바일에서 Windows Application 원격 제어하는 시스템 구현 - Server 구현 (3)
[RemoteAccess] 모바일에서 Windows Application 원격 제어하는 시스템 구현 - Windows Unreal Application 구현 (4)
[RemoteAccess] 모바일에서 Windows Application 원격 제어하는 시스템 구현 - Unreal 에서 키맵핑(Enhanced Input) 활용 (5)

 

목  차
1. 시스템 조건
2. 주요 코드
3. Github

 

 

앞서 '시스템 구상'에서 계획하였던

input 데이터를 먼저 수신하고

Windows Application으로 송신하는 Server를 구성하였다.

 

1. 시스템 구상

  • Python 코드로 구현
  • 여러 클라이언트가 접속할 수 있게 관리
  • Mobile Application에서 받은 데이터를 연결된 모든 클라이언트에 송신

 

2. 주요 코드

import socket
import threading

# 전역 클라이언트 목록
clients = []

def broadcast(message):
    for client_socket in clients:
        try:
            client_socket.send(message.encode('utf-8'))
        except:
            # 오류가 발생한 클라이언트는 목록에서 제거
            clients.remove(client_socket)

def handle_client(client_socket):
    global clients
    clients.append(client_socket)
    print(f"Client connected: {client_socket}")

    while True:
        try:
            message = client_socket.recv(1024).decode('utf-8')
            if not message:
                break
            print(f"Received message: {message}")

            # 메시지를 모든 클라이언트에게 브로드캐스트
            broadcast(message)
        except ConnectionResetError:
            break

    print(f"Client disconnected: {client_socket}")
    clients.remove(client_socket)
    client_socket.close()

def main():
    server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    server.bind(('00.00.000.00', 12345))	// TODO : 서버 ip 및 포트
    server.listen(5)
    print("Server listening on port 12345")

    while True:
        client_socket, addr = server.accept()
        print(f"Accepted connection from {addr}")

        # 각 클라이언트 연결을 새로운 스레드에서 처리
        client_handler = threading.Thread(target=handle_client, args=(client_socket,))
        client_handler.start()

if __name__ == "__main__":
    main()

 

3. Github

Comments