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

#region TCP 데이터 전송하기 - SendTCPData(socket, bufferByteArray, offset, transferSize, timeOut)

/// <summary>
/// TCP 데이터 전송하기
/// </summary>
/// <param name="socket">소켓</param>
/// <param name="bufferByteArray">버퍼 바이트 배열</param>
/// <param name="offset">오프셋</param>
/// <param name="nSize">전송 크기</param>
/// <param name="timeOut">타임 아웃</param>
public void SendTCPData(Socket socket, byte[] bufferByteArray, int offset, int transferSize, int timeOut)
{
    int startTickCount = Environment.TickCount;
    int countSent      = 0;

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

        try
        {
            countSent += socket.Send(bufferByteArray, offset + countSent, transferSize - countSent, 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(countSent < transferSize);
}

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

댓글을 달아 주세요