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

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)
5881guest4/6/20232932static method - <에러메시지 Extension method must be defined in a non-generic static class> [4]
5880유비4/4/20232821WPF DataGrid CollectionView, IEditableCollectionView 관련 문의 [1]
5879guest4/4/20233044Async method의 에러 표시 [3]
5878guest4/3/20233108C#으로 CMOS 설정 변경가능한지요? [4]
5875guest4/2/20233534성태님 책을 완독 하고 Static [7]
5874민성4/1/20232927안녕하세요 질문 하나만 드릴깨요~ [1]
5873guest3/31/20233147제어판에서 삭제불가 MS Edge ---> 레지스트리 편집기에서도 안보임 [6]파일 다운로드1
58723/31/20232797web config 파일 확인부탁드려요 [6]
58713/31/20232727web config 파일 수정이요 [2]파일 다운로드1
5870guest3/30/20233204.NET Core SDK 삭제 시 주의 사항 [4]파일 다운로드1
5869guest3/30/20233442Dictionary의 Update 그리고 Foreach [7]
5868guest3/29/20232916Speech Recognition과 Form1 그리고 정확도 [4]파일 다운로드1
5866월급쟁이3/28/20232983cmake 크로스 컴파일 관련하여 질문이 있습니다 [1]
5865guest3/28/20232864Github Copilot과 코딩실력 향상? [1]
5864guest3/27/20233283System.NullReferenceException - 개체참조가 개체의 인스턴스... [6]파일 다운로드1
5863guest3/24/20233422이벤트 핸들러 사라짐 현상 - Button [4]
5862guest3/21/20233476세계최초 hts와 싱글스레드 [8]
5861다크파이썬3/21/20233604WPF를 사용하려고 하려고 도서 문의합니다. [2]
5860guest3/21/20232915인텔코어 i5 CPU와 스레드 [4]
5859guest3/21/20232783개발 일지 어떻게 관리하시나요? 이런 프로그램 없나요? [3]
5858김태원3/18/20232814안녕하세요! [5]
5857guest3/17/20232881귀도 반 로썸을 보고 [4]
5856guest3/17/20233139Form1_FormClosing에 closing time을 Sqlite 저장하는 법? [6]파일 다운로드1
5855욜로3/17/20232731C# 메타데이터에서 불러오는 참조 정의가 안됨 [1]
5854민성3/16/20232905안녕하세요 asp.net mvc using문 관련하여 [1]
5853pa3/16/20233163오피스 2016 업데이트 후 파일 출력 불가 [1]
1  2  [3]  4  5  6  7  8  9  10  11  12  13  14  15  ...