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

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

... 16  17  18  19  20  21  22  [23]  24  25  26  27  28  29  30  ...
NoWriterDateCnt.TitleFile(s)
5354민성7/15/20209634안녕하세요 db연결 문자 보안 [2]
5353형성재7/14/20208783Visual Studio Setup Project에서 VC++2010재배포패키지설치 [4]
5352윤식7/10/20208485닷넷코어 빌드 시 runtimeconfig 파일을 없앨 수 있는지.. [4]
5351runr...7/2/20208508C# 에서 C++ dll 병렬 사용 [1]
5350홍성호6/29/202010212C++에서 정의된 구조체(공유메모리)를 C#으로 공유메모리에서 구조체 변환시 에러가 발생 합니다. [1]파일 다운로드1
5349jero...6/24/20207561logon credential provider 관련 문의드립니다. [1]
5348776/19/20207337시작하세요 C# 8.0프로그래밍 책에 대한 질문입니다. [8]
5347민성6/19/20208162안녕하세요 WPF Binding에서요 [1]
5346dimo...6/18/20208281Task.WhenAll() 메소드를 이용한 코드를 좀더 짧게 사용하고 싶은데 다른 표현이 있을까요? [3]
5345질문요6/12/20207260idc vs 집 속도 [2]
5344하태6/2/20208535c++ .ilb에서 c# dll 호출 질문 드리겠습니다 (콜백함수 전달) [4]
5343하태6/2/202011564안녕하십니까! c# dll을 c++ .lib에서 호출 질문 드립니다. [6]
5342진우5/30/202012436c++ 에서 C# DLL 사용 문의 [2]
5341미나리5/28/20208260스레드 lock키워드 관련 질문드립니다 [3]
5339민성5/27/20207567WPF cmd을 실행을 할때 파라미터 넘기는 방법 [1]
5338서영준5/26/20209529.Net Core Blazor 서버에 Xing API를 이용한 통신 요청 [5]파일 다운로드1
5337ogos...5/26/20209602C# DB connection string 보호 방법에 대하여 [2]
5336saki5/21/20209412이벤트 뷰어 .NET Runtime 오류 [3]
5335민성5/21/20207859안녕하세요 C#으로 컴퓨터 시작프로그램 목록을 가져와서 사용안함으로 바꿀려면 [1]
5334민성5/19/20208259안녕하세요 WPF 콘솔창을 띠우면서 Ping이라는 명령어가 콘솔에 Write되게 할려면 어떻게 해야 하나요?? [1]
5333초보5/18/20208250공유 메모리 관련 문의 [2]
5332질문요5/15/20208163TcpListener TcpClient 문의 [4]
5331sdd5/13/20207938안녕하세요 Settings관련하여 질문드립니다. [2]파일 다운로드1
5330하태5/7/202012556안녕하세요! 질문 드리겠습니다! C# dll에서 c++ CLR프로젝트를 참조 추가 할 수 있나요? [2]
5329김태령4/21/20207879젠킨스에서 원격 머신에 있는 실행 파일을 실행하면 백그라운드로 뜹니다 [2]
5328crea...4/17/20207921안녕하세요! [3]파일 다운로드1
... 16  17  18  19  20  21  22  [23]  24  25  26  27  28  29  30  ...