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

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)
5189진우7/19/20194786C# 스레드풀 코어별 실행 문의 [2]
5188황태관7/19/20194275비주얼베이직 2019 실행 할때 마다.. [3]
5187플하7/19/20196870UWP 관련 궁금한 사항에 대해서 [1]
5186김대훈7/14/20194783박싱과 언박싱에 대해 [2]
5185농상7/13/20194157Nullable에 대해서 [1]
5184김대훈7/4/20194332저자님의 책을 다 본후에는 [2]
51837/2/20194938.NET Compact Freamwork 컨트롤러 더블버퍼링 [1]
5182wp...7/2/20194705wpf 질문 드립니다. [1]파일 다운로드1
51817/1/20194974DataGridview Doublebuffer 에 대해서 질문드립니다. [2]
5180김대훈6/25/20195235배열과 반복문에서 질문드립니다 [2]
5177농상6/13/20196220멀티스레드 건의 [2]
5176이선호6/13/20195929안녕하세요. 닷넷 문제로 검색하다 알게되어 들어왔습니다. 현재 IIS 문제가 있어 질문드립니다. [1]
5175Ch...6/12/20195735WPF Ellipse 그리기! [3]
5174농상6/11/20195318ThreadPool 조인에 관해 [1]
5173전우치6/9/20195037공유 리소스에 대한 스레드 동기화 처리를 위해서 lock 이용 시 문의 [3]
5172김대훈6/7/20194658너무힘드네요 공부에 대한조언부탁드립니다 [2]
5171조남석6/4/20194596EX)11-2(treeview)에 대한 질문입니다. [3]
5170레리6/4/20195101Setup 프로젝트 레지스트리 설정 관련 질문입니다. [1]파일 다운로드1
5169농상6/3/20195039멀티스레드 파라미터 관련 [2]
5168익명...5/30/20194379항상 정말 감사드립니다... [1]
5167WPF5/23/20195221질문드립니다. [1]
5165이대희5/22/20194459Visual Studio 설치 구성요소 문의 (C# 7.3 개정판 관련) [1]
5164레드5/21/20195636실행 과정과 실행파일 디버그 시 Icon변경 질문드립니다. [5]
5163이대희5/20/20194356시작하세요 C# 7.3 프로그래밍 책 도착했습니다. [1]
5162채홍윤5/14/20196669Mono Develop window 설치 [6]
5161정대영5/13/20194655VS 2013에서 C#6.0(.netFramwork 4.6.1) $ 디버깅 오류 [1]
... 16  17  18  19  20  21  22  23  24  [25]  26  27  28  29  30  ...