UNITY/네트워크7 [네트워크] TCP/IP - 비동기 방식의 연결 (2)TCP 비동기 클라이언트 TCP 비동기 클라이언트using UnityEngine;using UnityEngine.UI;using System;using System.Net.Sockets;using System.Threading.Tasks;using System.Text;public class TcpClientAsync : MonoBehaviour{ [SerializeField] private string serverIP = "127.0.0.1"; [SerializeField] private int serverPort = 8888; [SerializeField] private InputField inputField; [SerializeField] private Button sendButton; [.. 2025. 6. 17. [네트워크] TCP/IP - 비동기 방식의 연결 (1)TCP 비동기 서버 TCP 비동기 서버 TcpServerAsync.cs using UnityEngine;using System;using System.Net;using System.Net.Sockets;using System.Text;using System.Threading;public class TcpServerASync : MonoBehaviour{ private TcpListener tcpListener; private Thread listenerThread; // Start is called before the first frame update void Start() { listenerThread = new Thread(ListenForIncomingRequests); .. 2025. 6. 12. [네트워크] TCP/IP 비동기 통신 함수 Asynchronous TCP/IP 통신 함수 Asynchronous TCP/IP 통신 함수 TCP/IP 통신을 위한 클래스들을 제공하는 네임스페이스 System.Net.Sockets- C# 5.0부터 도입된async/await키워드를 사용하면 비동기 코드를 동기 코드처럼 간결하게 작성할 수 있습니다. TcpListener(서버)TcpListener(IPAddress localaddr, int port): 서버가 사용할 IP 주소와 포트에서 클라이언트 연결을 수신할 TcpListener 객체를 생성합니다.Start(): 클라이언트 연결 수신을 시작합니다.AcceptTcpClientAsync(): 클라이언트로부터 온 리퀘스트(연결 요청)를 비동기적으로 수락하고, await 키워드와 함께 사용하여 연.. 2025. 6. 12. [네트워크] 비동기 통신 비동기 통신 Asynchronous (비동기) 통신 Asynchronous(비동기) 통신은 프로그램이 특정 작업(예: 네트워크 요청)을 시작한 후, 그 작업의 완료를 기다리지 않고 바로 다음 코드를 실행하는 통신 방식입니다. 작업의 결과는 나중에 콜백(callback) 함수, 이벤트, future/promise 객체 등을 통해 확인하거나 처리합니다. 비동기 TCP/IP 통신의 장단점 장점• UI 멈춤 방지:네트워크 작업 중에도 프로그램 실행이 중단(block)되지 않아, UI가 멈추는 현상(Freezing)을 방지할 수 있습니다.• 높은 응답성:사용자의 입력에 즉각적으로 반응할 수 있어 애플리케이션의 응답성이 유지됩니다. • 효율적인 자원 사용:대기 시간 동안 다른 작업을 병렬 처리할 .. 2025. 6. 11. [네트워크] TCP/IP - 동기형 방식의 연결 (2)TCP 동기형 클라이언트 TCPClientSync.cs(클라이언트) 1.TcpClient를 사용하여 서버에 연결합니다. 2.사용자가 입력한 텍스트 메시지를 서버에 보냅니다. 3.서버로부터 에코된 메시지를 받아 화면에 표시합니다. ConnectToServer()를 Start()에서 바로 호출ReceiveMessages()를 ConnectToServer()가 성공한 직후 바로 호출하여 메인 스레드에서 계속해서 메시지를 수신 또는 Update()에서 주기적으로 호출하는 방식으로 변경할 수도 있다.using System.Collections;using System.Collections.Generic;using UnityEngine;using UnityEngine.UI;using System;using System.Net.S.. 2025. 6. 10. [네트워크] TCP/IP - 동기형 방식의 연결 (1)TCP 동기형 서버 TCPServerSync.cs (서버) 1.TcpListener를 사용하여 클라이언트 연결을 수신합니다. 2.클라이언트로부터 메시지를 받고, 동일한 메시지를 다시 보냅니다. 3.별도의 스레드에서 실행됩니다. (메인 스레드 블로킹을 완전히 피할 수는 없지만, 최소화합니다.) using System.Collections;using System.Collections.Generic;using UnityEngine;using System;using System.Net;using System.Net.Sockets;using System.Text;using System.Threading;public class TcpServerSync : MonoBehaviour //Unity 컴포넌트로 동작하기 위해 Mo.. 2025. 5. 30. 이전 1 2 다음