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

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)
5269달려라라...12/2/201918096C# winform(framework) tabcontrol 에서 button 함수 관련 질문 [3]파일 다운로드1
5266엔벌잉11/26/201916719라즈베리파이 autostart관련 질문 [2]
5265해나11/25/201915415C# 6.0 책 내용 문의 [1]
5264김혜원11/18/201919995C# CodeSign에 대해 여쭤볼 것이 있습니다 [4]
5263정환맨11/17/201918563Task관련 질문입니다 [3]
5262닷린이11/14/201920865Dispatcher 와 synchronizationcontext의 관계가 궁금합니다. [6]
5260방문자11/13/201916920안녕하세요 도움받고 싶습니다 [1]
5259ming11/13/201916536string타입 파라메터로 넘길때 메모리 내부가 궁금합니다. [3]
5258C#초보11/13/201918057DCOM COM+ 등록 관련 문의드립니다. [7]
5257C#초보11/9/201915962파일 저장 방법에 대해 조언 부탁드립니다. [3]
5256세퉁11/8/201915114안녕하세요~ 질문은 아닙니다만.. [1]
5255C#초보11/6/201916615중첩 딕셔너리를 어떻게 만들 수 있을까요? [2]
5252김동수11/4/201917020Windows 10 로그인 ( Credential Provider ) 문의드립니다. [1]
5251kss11/1/201916512c#문법질문입니다. [1]
5250이대희10/31/201916457시작하세요 C# 7.3 프로그래밍 책이 절판이네요. [1]
5249황태관10/24/201917780c# 폼안에 특정 프로그램 불러오기.. [1]파일 다운로드1
5248황태관10/23/201919440C언어로 만든 dll 를 참조해서 c#으로 만들려면.. [3]
5245민성10/21/201916612안녕하세요 Ui 스레드 접근에 대해서 질문드리도록 하겠습니다. [1]
5244hbli...10/20/201916677두개의 쓰레드에서 하나의 Queue 리스트 사용시 질문드립니다. [2]
5243최선호 donator10/18/201916143Screen Capture 와.. Overlay 질문 좀 드릴게요 ㅠ.ㅠ ( 장문 주의..... ) [2]
5241C#초보10/16/201918816C# USB 통신 구현 관련 질문 드립니다! [1]
5240김동규10/15/201918307런타임 시 Encoding.Default를 UTF-8로 설정할 수 있을까요? [3]
5239밤톨이10/13/201916508시놀리지 DSM에 asp.net core 2 웹서버 구축이 가능한가요? [1]
5238권석헌10/2/201917365C# - C++ 간의 Memorymapped file 관련 Data 질문좀 드립니다 선생님 [1]
5237유호성10/1/201917354사용자 정의 컨트롤 VS IDE에서만 Exception 발생 [2]
5234koko9/25/201919372안녕하세요. 64bit WPF -> 32bit C++ DLL Import사용 문의 드립니다. [3]
... 16  17  18  19  20  21  22  23  24  25  26  [27]  28  29  30  ...