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

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)
1281(non...7/1/201418825(글쓴이의 요청으로 삭제합니다.) [4]
1280동동이6/25/201417979안녕하세요. ocx의 비동기 또는 쓰레드에서 호출 [1]
1279(non...6/23/201418298(글쓴이의 요청으로 삭제합니다.) [17]
1278이상식6/19/201419410.net DLL 내 자바스크립트를 수정 또는 재정의 할 수 있을까요? [3]
1277김솔지6/18/201416503silverlight에서 datagrid, listbox질문이여 [2]
1276정우석6/16/201415976쿠키 [1]
1274김솔지6/10/201420553배포 페이지 url을 얻고 싶습니다. [8]
1272이훈모6/7/201415564정말 어려운 상황에 직면했습니다. [1]
1270Jong...6/2/201425429C#과 C++을 이용한 Image 처리. [13]
1269김아영5/29/201416003InitializeComponent 함수 호출 지연 현상 [5]
1268솔솔5/27/201415649smart client [1]
1266김솔지5/22/201417881clickonce 수정에 대해 알고싶습니다. [2]
1265이은아5/22/201422299DataGridView 헤더를 두줄이상으로 하고싶습니다. [1]파일 다운로드1
1264김인호5/18/201420398소스코드 및 예제그림 zip 파일 [1]
1263이영종5/15/201417953159페이지 오타인것 같습니다 [5]
1262(non...5/4/201418889(글쓴이의 요청으로 삭제합니다.) [10]
1261이근주5/4/201416608다시 한번 질문드릴께요. [2]
1259이근주5/1/201416050도서 오류인 것 같네요.. [1]
1258최세정4/28/201418694안녕하세요~php module 오류로 고민하다가 여기까지 왔네요..ㅜㅜ [2]
1252popo4/21/201417927바인딩 질문입니다. [2]
1251(non...4/20/201422009(글쓴이의 요청으로 삭제합니다.) [11]
1249홍용규4/17/201422333app.config 파일 관련 질문 있습니다. [2]
1246(non...3/30/201418101(글쓴이의 요청으로 삭제합니다.) [1]
1245POPO3/26/201417384Http 프로토콜 관련 질문 입니다. [1]
1244(non...3/26/201417250(글쓴이의 요청으로 삭제합니다.) [1]
1241(non...3/22/201421116(글쓴이의 요청으로 삭제합니다.) [4]
... 46  47  48  49  50  51  52  53  54  55  [56]  57  58  59  60  ...