첨부 소스 코드는 나눔고딕코딩 폰트를 사용합니다.
728x90
반응형
728x170
using System;
using System.Net.Sockets;
using System.Threading;

#region TCP 데이터 수신하기 - ReceiveTCPData(socket, bufferByteArray, offset, receiveSize, timeOut)

/// <summary>
/// TCP 데이터 수신하기
/// </summary>
/// <param name="socket">소켓</param>
/// <param name="bufferByteArray">버퍼 목록</param>
/// <param name="offset">오프셋</param>
/// <param name="receiveSize">수신 크기</param>
/// <param name="timeOut">타임 아웃</param>
public static void ReceiveTCPData(Socket socket, byte[] bufferByteArray, int offset, int receiveSize, int timeOut)
{
    int startTickCount = Environment.TickCount;
    int countReceived  = 0;

    do
    {
        if(Environment.TickCount > startTickCount + timeOut)
        {
            throw new TimeoutException();
        }

        try
        {
            countReceived += socket.Receive
            (
                bufferByteArray,
                offset + countReceived,
                receiveSize - countReceived,
                SocketFlags.None
            );
        }
        catch(SocketException socketException)
        {
            if(socketException.SocketErrorCode == SocketError.WouldBlock ||
               socketException.SocketErrorCode == SocketError.IOPending  ||
               socketException.SocketErrorCode == SocketError.NoBufferSpaceAvailable)
            {
                Thread.Sleep(30);
            }
            else
            {
                throw socketException;
            }
        }
    }
    while(countReceived < receiveSize);
}

#endregion
728x90
반응형
그리드형(광고전용)
Posted by icodebroker

댓글을 달아 주세요