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

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

1  2  3  4  5  6  7  8  9  10  11  12  13  14  [15]  ...
NoWriterDateCnt.TitleFile(s)
5561Edun10/15/20215050도와주세요!! 팝업 띄우는 더블클릭 이벤트!! 부탁드립니다 ㅠㅠ [4]파일 다운로드1
5560Edun10/13/20215247C#초보자 개념이해 도와주세요!! [4]
5559최가운10/5/20215531C# debugging 중에 Thread Pool Worker들이 사라지는데요 [3]
5558옥코드9/29/20215831안녕하세요! EntityFramework Linq 질문 드리겠습니다. [4]
5557임기성9/16/20216379웹 사이트(IIS)가 오류로 인하여 빈번하게 죽는 이슈 문의 [1]
5556hjlee9/13/20215131visual studio 설치 없이 csi.exe (C# interactive) tool을 설치할 수 있을까요? [2]
5555이성열 donator9/2/20214920C# 런타임(Reflection)에서 소스코드에 입력한 함수설명(Summary)을 가져올 수 있을까요? [3]
5554농상9/2/20215609enum 원소값이 중복인 경우 출력 [4]
5553shdt...8/31/20214583제너릭을 new로 할당했는데 null인 경우가 존재하나요? [1]
5552hjsh...8/30/20214853C# 폼 - 마우스가 다른 버튼 위를 움직일 때도 폼의 페인트핸들러가 호출되는 것이 맞나요? [2]
5551하나다라마8/27/20214967뽀모도로 타이머 개선사항 [1]
5550하영8/27/20216045SqlDataReader 관련 질문입니다. [8]
5549책 감사...8/23/20217524C# 9.0 책 오타 [1]
5548Dev8/23/20215791String의 IndexOfAny 함수 질문 [3]
5547민성8/20/20215215WPF Radio 버튼을 GroupName으로 읽어와서 비교하기 [1]
5546Deve...8/19/20216120C++ C# API 연동 관련 [1]
5545영귤8/18/20215035C# 14.3장 예제에서 반환 값의 값 복사 부하가 어디서 발생하는 것인가요? [4]
5544이상호8/18/20215357C# Graphics 객체 저장 가능 여부 [2]
5543정형지8/18/20215107c# outofmemoryexception [3]
5542영귤8/11/20217905C#9.0 886쪽 오류 발생 안 해요 [2]
5541서지훈8/11/20218514C# 응용프로그램(Winform)의 비정상 종료(Exit Code) [2]
5540산들마을...8/11/20216160C# 많은 Control(Label 300개) Update 관련 문의 [2]파일 다운로드1
5539초초초촙8/9/20214665c# textbox 입력값이 다름 [1]파일 다운로드1
5538VS8/7/20214957vsmacros 확장자 파일을 사용하는 방법을 알 수 있을까요? [2]
5537민성8/6/20215197안녕하세요 WPF에서 이미 있는창이 있다면 안띠우게 하는 방법 [1]
55358/5/20216730안녕하세요. 초보 웹 개발자입니다. [10]파일 다운로드2
1  2  3  4  5  6  7  8  9  10  11  12  13  14  [15]  ...