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

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)
979왕초보7/1/201117974Apache + IIS Redirect [2]
9776/16/201122277웹컨트롤 어느거 쓰는게 좋나요 [1]
976박성준6/13/201117769VS2008 Add-in 구현 관련 질문 [4]
972김길6/6/201120039메모리 해제 예외 처리 관련.. [2]
971강동원5/29/201117560firebird install건 [1]
970임동찬5/18/201118354ASP.net 솔루션 디버깅 관련 [1]
969이성환5/4/201120028WMI 를 사용하지 않고 하드웨어 정보를 가져올 수 없을까요? [3]
968김동미4/28/201119437안녕하세요 다시 한번 문의를 드립니다.. [2]파일 다운로드1
967임동찬4/22/201122179C# using문 관련 [9]
964김동미4/18/201119779wcf IsOneWay 속성관련 문의 입니다..
965정성태4/18/201121152    답변글 [답변]: wcf IsOneWay 속성관련 문의 입니다..
966김동미4/19/201118980        답변글 [답변]: [답변]: wcf IsOneWay 속성관련 문의 입니다.. [1]
963최재훈4/12/201117363wcf inactivityTimeout 설정시 문의 사항이 있습니다. [2]
962임동찬4/8/201116985TFS 사용관련 [1]
961임동찬4/7/201117337XSD & XML & XmlCodeGenerator [2]
960임동찬4/5/201118866XML Schema Editor [4]
959immm3/24/201116671로그인 연동 어려운 건가요? [1]
958꼭지3/3/201118699Supporting compressed request in WCF 3.5 [5]
957임동찬2/21/201119209WCF channel faulted 관련 [2]
956윤용한2/18/201122649WaitHandle.WaitOne 과 Stopwatch에 관한 질문 [3]
955최광욱2/17/201119313TFS 에서 소스 영구 제거 방법 [1]
954한장우2/16/201117036atl activeX 질문이요~ [1]
952박용운2/16/201117900IE8.0에서 BHO [1]
953박용운2/16/201117898    답변글 [답변]: IE8.0에서 BHO
951임동찬2/11/201117926WCF Service Reference [1]
950이성환2/9/201119386Windows application 프로젝트를 참조 했을 때 생성되는 실행파일을 직접 실행 불가능하도록 하고 싶습니다. [6]파일 다운로드1
... 61  62  63  64  [65]  66  67  68  69  70  71  72  73  74  75  ...