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

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)
3643힘찬도약11/11/201520761C# 함수의 processing time과 재호출 [14]
3642.net11/10/201520098c# 으로 작성된 com+ 에 대한 문제입니다. [2]
3641힘참도약11/9/201519858c# log file 관련해서 질문드립니다. [5]
3638윤창선11/4/201521996사설IP가 부여된 무선라우터간 영상전송 관련 문의 [8]
3634Hyun...11/2/201518915c# 에서 webkit browser에서 webgl을 이용하는 사이트에 접속이 안됩니다. [1]
3633힘찬도약10/31/201519144mysql insert where not exists [6]
3632힘찬도약10/27/201519134C# Lock 관련해서 질문드립니다. [6]
3655iwc11/30/201517313    답변글 [답변]: C# Lock 관련해서 질문드립니다.
3631강준10/26/201521101iis 8.5 preload 기능에 대해 질문이 있습니다. [9]
3630김정훈10/25/201519460몬티홀 게임 관련 질문 [1]
3629pooq10/23/201519825리플렉션 관련해서 질문 입니다. [3]
3628최영민10/22/201518123스마트 클라이언트 로딩속도 문의입니다. [3]
3627양주호10/22/201517966C#으로 컨버팅 하려고 하는데요... [1]
3626조성진10/21/201518877책보고 첫번째 예제부터 문제가 생기네요 ^^; [4]파일 다운로드1
3623Bere...10/19/201519543질문이라기 보단... [2]
3625Bere...10/20/201518357    답변글 [답변]: 질문이라기 보단... [2]파일 다운로드1
3621힘찬도약10/18/201518533[C# 6.0]multi threading과 ui control [9]
3624힘찬도약10/19/201518712    답변글 [답변]: [C# 6.0]multi threading과 ui control [6]파일 다운로드1
3620popo10/13/201517251WPF의 datagrid, listview 컨트롤 관련 질문 입니다. [1]
3619링크의 ...10/12/201522025OCX 로드 관련 질문입니다. [5]파일 다운로드1
3616수요일밥...10/7/201523225몇 가지 오류 (2) [6]
3615김응규10/7/201517697다시한번 질문 드립니다. (이번엔 자세하게 기술했습니다.) [1]
3614김응규10/6/201517212안녕하세요. wcf net.tcp 관련 질문 하나만 올려요~~ [4]
3613강준10/5/201521957IIS Application Pool 시작/중단 에 대한 이벤트 로그는 어디에 남나요??? [2]
3612심심한일...10/4/201523520몇 가지 오류 [4]
3611나그네9/30/201517929안녕하세요 답글을 이제 보았습니다. [3]
... 46  47  48  [49]  50  51  52  53  54  55  56  57  58  59  60  ...