본문 바로가기
cs공부/네트워크프로그래밍

네트워크프로그래밍 - Session이란

by 코딩하는 돼징 2023. 7. 15.
반응형

Session이란

네트워크 통신에서 클라이언트와 서버 간의 연결을 관리하고 필요한 정보를 포함하고 있다.

 

앞서 사용했던 GameSession을 통하여 서버측과 클라이언트 측의 Session 차이를 알아보도록 하자


public override void OnConnected(EndPoint endPoint)

클라이언트 측 : 클라이언트가 서버에 연결되었을 때 연결 성공 후 초기 메시지를 서버로 보낸다.

public override void OnConnected(EndPoint endPoint)
{
    Console.WriteLine($"OnConnected : {endPoint}");
    // 보낸다
    for (int i = 0; i < 5; i++)
    {
        byte[] sendBuff = Encoding.UTF8.GetBytes($"HelloWorld! {i}");
        Send(sendBuff);
    }
}

서버 측 : 클라이언트가 서버에 연결되었을 때 연결된 클라이언트에게 "Welcome to piggy Server"라는 초기 메시지를 보낸다.

public override void OnConnected(EndPoint endPoint)
{
    Console.WriteLine($"OnConnected : {endPoint}");
    byte[] sendBuff = Encoding.UTF8.GetBytes("Welcome to piggy Server");
    Send(sendBuff);

    Thread.Sleep(1000);

    Disconnect();
}

public override void OnDisconnected(EndPoint endPoint)

클라이언트 측 : 클라이언트가 서버와의 연결이 끊어졌을 때 연결이 끊어진 클라이언트의 endPoint 정보를 출력한다.

public override void OnDisconnected(EndPoint endPoint)
{
    Console.WriteLine($"OnDisconnected : {endPoint}");
}

서버 측 : 클라이언트와의 연결이 끊어졌을 때 연결이 끊어진 클라이언트의 엔드포인트 정보를 출력한다.

public override void OnDisconnected(EndPoint endPoint)
{
    Console.WriteLine($"OnDisconnected : {endPoint}");
}

public override void OnReceive(ArraySegment<byte> buffer)

클라이언트 측서버에서 클라이언트로부터 데이터를 문자열로 변환하여 클라이언트로부터 받은 데이터를 출력합니다.

 public override void OnReceive(ArraySegment<byte> buffer)
{
    string recvData = Encoding.UTF8.GetString(buffer.Array, buffer.Offset, buffer.Count);
    Console.WriteLine($"[From Server] {recvData}");
}


서버 측 : 서버는 클라이언트로부터 데이터를 수신받았을 때 수신된 데이터를 문자열로 변환하여 "[From Client] {recvData}" 형식으로 출력한다.

public override void OnReceive(ArraySegment<byte> buffer)
{
    string recvData = Encoding.UTF8.GetString(buffer.Array, buffer.Offset, buffer.Count);
    Console.WriteLine($"[From Client] {recvData}");
}

public override void OnSend(int numofBytes)

클라이언트 측 : 클라이언트는 데이터를 전송한 후, 전송된 바이트 수를 출력합니다.

public override void OnSend(int numofBytes)
{
    Console.WriteLine($"Transfoerred bytes : {numofBytes}");
}

서버 측 : 서버는 데이터를 전송한 후  전송된 바이트 수를 출력한다. 

public override void OnSend(int numofBytes)
{
    Console.WriteLine($"Transfoerred bytes : {numofBytes}");
}

 

자세한 코드들은 이전 게시물들 참고해 주세요!

 

 

반응형

댓글