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

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

... 46  47  48  49  50  [51]  52  53  54  55  56  57  58  59  60  ...
NoWriterDateCnt.TitleFile(s)
3580국왕님7/14/201517455어셈블리 서명 pfx 파일의 가져오기시 암호를 확인할 수 있을까요? [1]
3579고훈용7/13/201517101스마트클라이언트 윈도우8.1 실행오류 해결 방법 문의 [1]파일 다운로드1
3578이성환7/9/201519882WPF Multi Dispatcher 사용 시 hang 발생 [4]파일 다운로드1
3577초보개발자7/9/201518665C# SHDocVw.InternetExplorer 관련 도움좀 부탁드리겠습니다. [1]
3576솔솔7/6/201517476zip압축시! [2]
3575염기돈6/29/201518168책을 보다가 익명 메서드 관련해서 질문거리가 생겼습니다. [2]파일 다운로드1
3574김기술6/26/201516428프로그램 사용중 USB 경로 관련 해서 문의 드립니다 [4]
3573gagl...6/24/201519386IIS 의 특정 페이지들에서 20초의 딜레이가 있습니다. [2]
3572프란치스코6/24/201517282책을 구입하고 싶은데, [5]
3571로손6/22/201517663.Net COM dll 관련 사용시 Event 부분에 관한 문의 입니다. [5]파일 다운로드1
3570나그네6/11/201521153안녕하세요.. ^^ 궁금한게 있어서 혹시 조언을 얻을수 있을까 해서.. [2]
3569김지용6/9/201523458FFmpeg.exe 를 이용한 C# 동영상 인코더 예제보고 질문 드립니다. [9]
3568유동근6/5/201517913C# TTS 오류 입니다 도와주세요.파일 다운로드1
3567김보경5/26/201518431c#이용한 음성인식에 질문드립니다. [1]
3566로손5/26/201520556Http 파일 업로드시 한글파일명 관련 [1]
3565김태훈5/20/201516375AxWebBrowser 파일 다운로드 관련하여 문의드립니다. [3]
3564이강산5/20/201516265MD5 인코딩 관련 문의 입니다 [1]
2564CatO...5/13/201517463AppDomain 문제로 삽질중입니다. [2]
2563솔솔5/13/201516071custom search!! [1]
2562안녕하세요5/13/201516848안녕하세요.. C# 구조체 관련. [1]
2560솔솔5/6/201516815zip파일생성시. [1]
2561솔솔5/6/201517451    답변글 [답변]: zip파일생성시. [5]파일 다운로드1
2558로손5/4/201520395VB6.0의 ActiveX(OCX) 와 .Net에서 만든 COM DLL의 차이점 ?? [1]
2559로손5/6/201517762    답변글 [답변]: VB6.0의 ActiveX(OCX) 와 .Net에서 만든 COM DLL의 차이점 ??
2557Guest4/24/201516500Serialize , DeSerialize 관련 질문입니다! [3]
1558민식짱4/23/201517374FFmpeg.exe 를 이용한 C# 동영상 인코더 예제를 보고 질문드립니다 [1]파일 다운로드1
... 46  47  48  49  50  [51]  52  53  54  55  56  57  58  59  60  ...