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

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)
985이성환7/27/201118774P/Inovke 관련 질문입니다. [4]파일 다운로드1
983이성환7/13/201127708Mouse이벤트 관련해서 질문있습니다. [10]파일 다운로드2
982궁굼이7/12/201119169TFS2010 사용관련 [2]
981김창욱7/11/201121695주식형태의 프로그램 처럼 SQL서버의 특정 필드 데이터의 변화가 있을때 재 클라이언트가 정보를 갱신 할 수 있게 하는 방법은 없을까요? [2]
980YJ7/7/201121437App Pool idle time 과 WCF 서비스의 비동기 function 호출. [3]
979왕초보7/1/201119720Apache + IIS Redirect [2]
9776/16/201123458웹컨트롤 어느거 쓰는게 좋나요 [1]
976박성준6/13/201118998VS2008 Add-in 구현 관련 질문 [4]
972김길6/6/201121267메모리 해제 예외 처리 관련.. [2]
971강동원5/29/201118800firebird install건 [1]
970임동찬5/18/201119591ASP.net 솔루션 디버깅 관련 [1]
969이성환5/4/201121228WMI 를 사용하지 않고 하드웨어 정보를 가져올 수 없을까요? [3]
968김동미4/28/201120646안녕하세요 다시 한번 문의를 드립니다.. [2]파일 다운로드1
967임동찬4/22/201123382C# using문 관련 [9]
964김동미4/18/201121004wcf IsOneWay 속성관련 문의 입니다..
965정성태4/18/201122338    답변글 [답변]: wcf IsOneWay 속성관련 문의 입니다..
966김동미4/19/201119802        답변글 [답변]: [답변]: wcf IsOneWay 속성관련 문의 입니다.. [1]
963최재훈4/12/201118616wcf inactivityTimeout 설정시 문의 사항이 있습니다. [2]
962임동찬4/8/201118365TFS 사용관련 [1]
961임동찬4/7/201118509XSD & XML & XmlCodeGenerator [2]
960임동찬4/5/201120257XML Schema Editor [4]
959immm3/24/201118013로그인 연동 어려운 건가요? [1]
958꼭지3/3/201120131Supporting compressed request in WCF 3.5 [5]
957임동찬2/21/201120512WCF channel faulted 관련 [2]
956윤용한2/18/201124044WaitHandle.WaitOne 과 Stopwatch에 관한 질문 [3]
955최광욱2/17/201120734TFS 에서 소스 영구 제거 방법 [1]
... 61  62  63  64  [65]  66  67  68  69  70  71  72  73  74  75  ...