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

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)
1241(non...3/22/201421118(글쓴이의 요청으로 삭제합니다.) [4]
1240이석주3/21/201423791인터넷 익스플로러가 hang이 걸리는 현상 문의 [1]파일 다운로드1
1238(non...3/13/201417709(글쓴이의 요청으로 삭제합니다.) [2]
1237(non...3/11/201417747(글쓴이의 요청으로 삭제합니다.) [2]
1236(non...3/11/201418603(글쓴이의 요청으로 삭제합니다.) [2]
1235(non...3/10/201417688(글쓴이의 요청으로 삭제합니다.) [2]
1234(non...3/10/201420883(글쓴이의 요청으로 삭제합니다.) [3]
1233(non...3/9/201418556(글쓴이의 요청으로 삭제합니다.) [4]
1232(non...3/8/201417452(글쓴이의 요청으로 삭제합니다.) [2]
1231(non...3/7/201418744(글쓴이의 요청으로 삭제합니다.) [9]
1230POCO3/7/201419094쓰레드 안에서 DependencyProperty get, set시 또 다른 스레드 오류.. [1]
1229(non...3/6/201419756(글쓴이의 요청으로 삭제합니다.) [11]
1228POCO3/6/201417806안녕하세요. 질문이 있습니다. [1]
1226김형진3/4/201427964안녕하세요 windows azure에 관해 질문했던 사람입니다. [2]
1224(non...3/3/201423280(글쓴이의 요청으로 삭제합니다.) [11]
1223sadf...3/3/201417824아래 질문에 답변 감사드립니다. 한가지 더 궁금한점이 있어 질문드립니다. [1]
1222(non...3/2/201418272(글쓴이의 요청으로 삭제합니다.) [4]
1221(non...3/1/201418694(글쓴이의 요청으로 삭제합니다.) [2]
1220Until2/28/201416999질문드립니다. [1]
1219이성환2/28/201416474string.Join()과 Enumerable.Aggregate()의 차이가 궁금합니다. [2]파일 다운로드1
1218김형진2/25/201418278안녕하세요. window azure에 대해서 질문이 있어서 문의 드립니다 [4]
1217(non...2/23/201418878(글쓴이의 요청으로 삭제합니다.) [1]
1215아리수2/20/201422294C# 공부하면서 WPF에 대한 질문. [2]
1214조광훈2/20/201420301IIS8 응용프로그램 풀 관련 질문 드립니다. [2]파일 다운로드1
1213김태훈2/17/201417502가상화 프로그램 질문입니다. [1]파일 다운로드1
1212조광훈2/13/201416269ISAPI 필터에서 커스텀 헤더 정보 추가 [1]파일 다운로드1
... 46  47  48  49  50  51  52  53  54  55  56  [57]  58  59  60  ...