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

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

[1]  2  3  4  5  6  7  8  9  10  11  12  13  14  15  ...
NoWriterDateCnt.TitleFile(s)
5874민성4/1/202327안녕하세요 질문 하나만 드릴깨요~ [1]
5873손님3/31/202374제어판에서 삭제불가 MS Edge ---> 레지스트리 편집기에서도 안보임 [5]파일 다운로드1
58723/31/202390web config 파일 확인부탁드려요 [6]
58713/31/202347web config 파일 수정이요 [2]파일 다운로드1
5870손님3/30/202369.NET Core SDK 삭제 시 주의 사항 [4]파일 다운로드1
5869손님3/30/202379Dictionary의 Update 그리고 Foreach [7]
5868손님3/29/202380Speech Recognition과 Form1 그리고 정확도 [4]파일 다운로드1
5866월급...3/28/202388cmake 크로스 컴파일 관련하여 질문이 있습니다 [1]
5865손님3/28/202394Github Copilot과 코딩실력 향상? [1]
5864손님3/27/202390System.NullReferenceException - 개체참조가 개체의 인스턴스... [6]파일 다운로드1
5863손님3/24/2023147이벤트 핸들러 사라짐 현상 - Button [4]
5862손님3/21/2023244세계최초 hts와 싱글스레드 [8]
5861다크...3/21/2023166WPF를 사용하려고 하려고 도서 문의합니다. [2]
5860손님3/21/2023138인텔코어 i5 CPU와 스레드 [4]
5859손님3/21/2023111개발 일지 어떻게 관리하시나요? 이런 프로그램 없나요? [3]
5858김태원3/18/2023185안녕하세요! [5]
5857손님3/17/2023144귀도 반 로썸을 보고 [3]
5856손님3/17/2023139Form1_FormClosing에 closing time을 Sqlite 저장하는 법? [6]파일 다운로드1
5855욜로3/17/2023116C# 메타데이터에서 불러오는 참조 정의가 안됨 [1]
5854민성3/16/202372안녕하세요 asp.net mvc using문 관련하여 [1]
5853pa3/16/202377오피스 2016 업데이트 후 파일 출력 불가 [1]
5852손님3/16/2023112입력 foreach 검색/출력 foreach [3]
5851손님3/15/2023149foreach내 list변경 [10]
5850독서가3/14/2023161C#에서 동적dll 사용시 문의입니다. [4]파일 다운로드1
5849손님3/9/2023182C# wpf로 Web에서 구동되는 hts가능한가요? (노트북없고 스마트폰 없음) [4]
[1]  2  3  4  5  6  7  8  9  10  11  12  13  14  15  ...