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

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

... 76  77  78  79  80  [81]  82  83  84  85  86  87  88  89  90  ...
NoWriterDateCnt.TitleFile(s)
456guest9/7/200616168데이타셋 암호화 방법...이 있을까요? [2]
451임은주8/31/200615755mms 스트림을 로컬 파일로 저장해주는 프로그램 에 대해서 여쭤볼께요 [1]
448guest8/28/200616391서버인증서및클라이언트인증서 발급
449정성태8/28/200615774    답변글 [답변]: 서버인증서및클라이언트인증서 발급
450ligh...8/29/200616140        답변글 [답변]: [답변]: 서버인증서및클라이언트인증서 발급 [3]
468light9/28/200616015            답변글 [답변]: [답변]: [답변]: 서버인증서및클라이언트인증서 발급
472정성태10/5/200616549    답변글 [답변]: 서버 인증서 및 클라이언트 인증서 발급
475ligh...10/11/200615480        답변글 [답변]: [답변]: 서버인증서및클라이언트인증서 발급
446윤경재8/20/200621043COM+ 프로젝트 디버깅 방법. [2]파일 다운로드1
442이남호8/7/200615893스마트클라이언트에 Farpoint Winform을 이용했는데 배포가 안되요.
443이남호8/7/200617814    답변글 [답변]: 스마트클라이언트에 Farpoint Winform을 이용했는데 배포가 안되요.
441강혜영8/5/200616806SHDocVw를 이용한 익스플로어 제어
444정성태8/7/200614876    답변글 [답변]: SHDocVw를 이용한 익스플로어 제어
445강혜영8/8/200615574        답변글 [답변]: [질문]: SHDocVw를 이용한 익스플로어 제어 [1]
437혀기7/25/200614188DirectoryEntry의 Childrend에 Add할때 엑세스가 거부됩니다~ㅜㅜ [2]파일 다운로드1
436조성택7/24/200614011IE를 가로채서 그리고 싶을때.. [1]
439태기7/25/200614701    답변글 [답변]: IE를 가로채서 그리고 싶을때..(재질문) [1]파일 다운로드1
435이영균7/21/200615379작그마한 스마트클라이언트 프로젝트를 진행하고 있습니다. [1]파일 다운로드1
431혁이7/19/200614058UpdatePanel(Atlas)위의 SmartClient가 이벤트후 사라집니다. ㅜㅜ파일 다운로드1
432정성태7/19/200614404    답변글 [답변]: UpdatePanel(Atlas)위의 SmartClient가 이벤트후 사라집니다. ㅜㅜ
433혁이7/20/200615220        답변글 [답변]: [답변]: UpdatePanel(Atlas)위의 SmartClient가 이벤트후 사라집니다. ㅜㅜ
434정성태7/20/200616227            답변글 [답변]: [답변]: [답변]: UpdatePanel(Atlas) 위의 SmartClient가 이벤트 후 사라집니다. ㅜㅜ [1]
429kwt7/12/200615441ActiveX 개발을 배우고 싶은데요 [3]
427이홍자7/11/200615642스마트 클라이언트 실행 시 그리드의 체크박스가 안보여요.. [1]
426정보문7/11/200616327액티브X 설치2 [1]
423이방은7/9/200615218리소스 임베디드 질문요.. [1]파일 다운로드1
... 76  77  78  79  80  [81]  82  83  84  85  86  87  88  89  90  ...