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

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를 몇 번 하는지 확인해 보세요. ^^
정성태

... 46  47  48  [49]  50  51  52  53  54  55  56  57  58  59  60  ...
NoWriterDateCnt.TitleFile(s)
3606guest9/20/201511363확장 클래스 네이밍 규칙은 어떻게 하시나요? [1]
3605spow...9/18/201510537익명형식을 다른 클래스의 인스턴스로 전달하고 싶습니다. [1]
3604popo9/11/201511706[C#] Singleton 을 사용하면서 궁금한점이 있어 질문 남깁니다. [2]
3603강준9/11/201510704xamarin 을 시작하려고 하는데 혹시 도움이 될만한 사이트나 정보 있으면 공유해 주세요~~ [1]
3602Mink...9/9/201511803aspx > 다른 확장자로 변경 후 디버깅 때문에 질문 드려 봅니다. [10]
3601edain9/8/201512010캐스팅...에 관해 궁금합니다. [5]
3600guest9/7/201515271C# 버전의 정의는 어떻게 구분하나요? [2]
3599김태훈9/4/201512932안녕하세요. C언어를 배우려고 하는 문과생입니다. [1]
3598미니8/29/2015134576.0은 전체소스? [1]
3597손성수8/28/201511937시작하세요 ! c# 프로그래밍 이책으로 배우고 있는데요 [2]
3596지나가는...8/26/201511517크로스플랫폼 [1]
3594guest8/25/201511188요즘 Windonws Form 기반 N Tier 어떻게 구성하나요?? [1]
3593개발자8/18/201513890현 시점에서 VS 버전은 뭐가 좋을까요? [8]
3592나그네8/15/201513280안녕하세요 질문이 있어서... [4]
3589spow...8/11/201514303Nancy + Razor 사용시 cshtml의 IntelliSense 및 참조 오류 [3]
3590spow...8/12/201513183    답변글 [질문]: (첨부 추가) [1]파일 다운로드1
3588재현8/8/201518031안녕하세요. C# 6.0 책을 보는 중에 의문이 생겨 질문드립니다. [4]
3587주문중8/4/201518091신간 출간 [7]
3586꿈꾸는개...7/29/201513083자바스크립트 checkbox 관련하여 ie10에서 호완이 안되는 부분이 있어 질문드립니다 [1]
3585유동근7/21/201511691TTS오류 [1]파일 다운로드1
3583노영우7/20/201511768iisnode 를 클래식 파이프라인 모드에서 호스팅하기 [2]
3582황희성7/17/201514636동영상 캡쳐에 관련해서 궁금하것이 있습니다. [6]파일 다운로드1
3581popo7/15/201511273[WPF] DependencyProperty에서 callback 처리시 문의 입니다. [1]
3580국왕님7/14/201511503어셈블리 서명 pfx 파일의 가져오기시 암호를 확인할 수 있을까요? [1]
3579고훈용7/13/201510959스마트클라이언트 윈도우8.1 실행오류 해결 방법 문의 [1]파일 다운로드1
3578이성환7/9/201513523WPF Multi Dispatcher 사용 시 hang 발생 [4]파일 다운로드1
... 46  47  48  [49]  50  51  52  53  54  55  56  57  58  59  60  ...