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

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

... [31]  32  33  34  35  36  37  38  39  40  41  42  43  44  45  ...
NoWriterDateCnt.TitleFile(s)
5132존기미3/3/201914644Physical Disk Access문의 [1]
5131강용규2/25/201913934c# 개발자입니다. 타이머 질문드립니다. [1]
5130노인코래방2/25/201918167C#에서 가장 좋은 성능을 보이는 파일 읽고 쓰는 방법이 무엇인가요? [2]
5129진우2/23/201917910닷넷 32비트 기반에서 메모리 부족으로 프로그램이 죽는경우 문의 [2]
5128게스트2/23/201915104안녕하세요. 초보개발자입니다. [3]파일 다운로드1
5127c#2/20/201913990책에 예제 문의드립니다. [2]
5125게스트2/19/201913819delegate를 활용한 event 를 적절히 불러오고 싶습니다. [2]
5124정근화2/12/201913866윈도우 서버2003 환경 오류 [2]
5123김주현2/8/201913692MS LUIS 에 대한 소개 하실 계획이 있으신가요? [1]
5122jaka...2/1/201915785Clickonce 배포 후 Command 실행 [2]파일 다운로드1
5121엔벌이1/31/201915409C# DataGridView의 MDB파일 함수? ArrayList? [1]파일 다운로드1
5120임우진1/30/201916556웹에서 응용프로그램 바로 실행하기 관련 브라우저에서 파라미터가 넘어오지 않습니다.ㅜㅜ [2]
5119guest1/29/201918135교재에 오탈자 있어 알려드리려 합니다 [1]
5118WPF꿈...1/26/201916138GetHashCode 메서드에 대해서 [1]
5117하주형1/25/201915909List<int>에 대한 이해가 잘안됩니다. [5]
5116게스트1/24/201914450asp.net 관련 gridview webform 질문 드립니다. [1]파일 다운로드1
5115Soul...1/24/201914672투명 패널 질문드립니다. [2]
5114박현일1/20/201915238WPF DataContext 관련 초보 질문을 드려봅니다.^^ [5]
5113하주형1/20/201914506안녕하세요 시작하세요 C# 인코딩관련 질문드립니다. [1]
5112손성배1/19/201923839안녕하세요 cp949 인스톨시 오류입니다... 너무 힘들어요 [5]
5111게스트1/10/201914989암호화 라이센스 관련 문의 드립니다. [1]
5110WPF꿈...1/9/201914474Thread Abort 함수 사용시 [2]
5109닷넷개발1/9/201914251thread 관련 질문 예제.. [2]파일 다운로드1
5108닷넷개발1/9/201915468thread 관련 질문 드립니다.. [4]
5107우코아1/4/201918217WPF에서 로딩중 이미지를 구현 - Project [5]파일 다운로드1
5106우코아1/3/201915853WPF에서 로딩중 이미지를 구현 - Source [1]
... [31]  32  33  34  35  36  37  38  39  40  41  42  43  44  45  ...