Microsoft MVP성태의 닷넷 이야기
TCP 소켓 [링크 복사], [링크+제목 복사],
조회: 14246
글쓴 사람
안중언
홈페이지
첨부 파일

using System;
using System.Text;
using System.Threading;
using System.Net;
using System.Net.Sockets;

namespace TCP_Test_Client
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("현재 클라이언트 컴퓨터의 IP : " + Utility.GetCurrentIPAddress());

            // 클라이언트 소켓이 동작하는 스레드.
            Thread clientThread = new Thread(ClientFunction);
            clientThread.IsBackground = true;
            clientThread.Start();

            Console.WriteLine("현재 클라이언트 프로그램 실행중입니다. (종료하려면 아무 키나 누르세요...)");
            Console.ReadLine();
        }

        private static void ClientFunction(object obj)
        {
            Console.WriteLine("현재 클라이언트 프로그램을 시작하겠습니다.");

            using (Socket clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
            {
                EndPoint serverEndPoint = new IPEndPoint(IPAddress.Parse("220.119.20.225"), 11200);

                clientSocket.Connect(serverEndPoint);

                int nTimes = 5;

                while (nTimes-- > 0)
                {
                    // 클라이언트 -> 서버 : 데이터 송신.
                    byte[] buf = Encoding.UTF8.GetBytes(DateTime.Now.ToString());
                    Console.WriteLine(DateTime.Now.ToString());
                    clientSocket.Send(buf);

                    // 서버 -> 클라이언트 : 데이터 수신.
                    byte[] receivebytes = new byte[1024];
                    int nReceive = clientSocket.Receive(receivebytes);
                    string txt = Encoding.UTF8.GetString(receivebytes, 0, nReceive);

                    Console.WriteLine(txt);

                    Thread.Sleep(1000);
                }
            }

            Console.WriteLine("TCP Client socket : Closed");
        }
    }
}

public static class Utility
{
    public static IPAddress GetCurrentIPAddress()
    {
        IPAddress[] iPAddresses = Dns.GetHostEntry(Dns.GetHostName()).AddressList;

        foreach (IPAddress iPAddress in iPAddresses)
        {
            if (iPAddress.AddressFamily == AddressFamily.InterNetwork)
            {
                return iPAddress;
            }
        }
        return null;
    }
}

서버측은 488 Page 그대로 구현했습니다.

책 488 Page 내용에 UDP에서는 5번 보냈길래 TCP 에서도 5번 보내니까 이런 오류가 뜨는데 이건 왜 이런건가요?








[최초 등록일: ]
[최종 수정일: 11/10/2018]


비밀번호

댓글 작성자
 



2018-11-10 11시44분
서버 측의 코드가 Send / Receive를 몇 번 하는지 확인해 보세요. ^^
정성태

... 61  62  63  64  65  [66]  67  68  69  70  71  72  73  74  75  ...
NoWriterDateCnt.TitleFile(s)
950이성환2/9/201119394Windows application 프로젝트를 참조 했을 때 생성되는 실행파일을 직접 실행 불가능하도록 하고 싶습니다. [6]파일 다운로드1
947김순조1/24/201119018.NET based Com에서 Native ActiveX로 이벤트 보내기?? [2]파일 다운로드1
943김기룡1/3/201122067닷넷 에러시 조치사항관련... [2]
942김기룡12/27/201017074Thread 안정성 관련 문의 드립니다. [2]
941최광욱12/20/201016842정성태님 올리신 글중에 [1]
940최광욱12/20/201018621Assembly Unloading 관련해서 [2]
939최광욱12/20/201017248IIS 로그 읽기 [1]
938날쌘돌이12/14/201018025자바로 asp.net 인증하기 [3]
935김기룡12/13/201028372c#에서 c++로 개발된 dll에 byte[] 전달 관련하여 문의 드립니다. [6]
934임동찬12/7/201015875System.Reflection.Assembly.GetTypes() 메서드에 대해 [1]
929김준호12/2/201016047안녕하세요 비주얼베이직 2005 닷넷 관련 문의입니다 [1]
928김준호11/30/201016607안녕하세요 비주얼베이직 2005 닷넷 관련 문의입니다 [1]파일 다운로드1
927임동찬11/30/201017992Windows\\Temp 폴더의 이름모를 .tmp 파일들에 대해서 [1]
926이승규11/26/201037921IP접속 시도시 ORA-12504 에러 [1]
925임동찬11/11/201015515다른 프로그램의 컨트롤 건드려보기_추가질문(2) [1]
924임동찬11/10/201016288다른 프로그램의 컨트롤 건드려보기_추가질문 [1]
923임동찬11/9/201018806다른 프로그램의 컨트롤 건드려보기 [1]
922박태근11/2/201017443html5의 shape파일 관련 [1]파일 다운로드1
921박태근11/1/201018174DataTable 의 Binary변환! [1]
920김재영10/26/201018386GAC에 등록된 어셈블리를 Visual Studio에서 참조 대화상자에 보이게 할려면 어떤 방법이 있습니까? [2]
919임동찬10/22/201016788IStream [1]
918임동찬10/21/201016005System.Runtime.InteropServices.ComTypes.IStream 관련 [1]
917한귀순10/20/201020231IIS 최초 loading 시 속도 [2]
916임동찬10/15/201016482file lock 관련 [2]
915오병태10/11/201015277바쁘신대 답변 감사드립니다. [1]
914오병태10/11/201015432감사드립니다. 염치없지만 또 한번 문의드립니다. [2]
... 61  62  63  64  65  [66]  67  68  69  70  71  72  73  74  75  ...