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

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)
5623한예지 donator3/15/202211112인터프리터 원리가 궁금합니다. [4]
5622김민아3/8/202211358const와 readonly의 명확한 차이가 이게 맞나요? [2]
5621장성욱3/8/202211417c# 로그 관련 질문 [1]
5620김민아3/7/202210961안녕하세요 비관리 객체를 반환하는 메소드 호출 시 궁금한 점이 있어서 질문드립니다 [2]
5619팡팡이3/3/202212859RSA 문의드립니다. [3]
5618김기헌3/2/202210876안녕하세요 생성자 옆에 this 키워드를 붙여 생성자를 여러 개 호출 시 질문드립니다 [2]
5617Edun2/25/202211571ArgumentOutOrRangeException에러 발생 [2]파일 다운로드1
5616csha...2/24/202210963readonly struct로 선언된 구조체를 특정 클래스에서 멤버변수로 가지는 경우 [1]
5615장성욱2/22/202214427SetThreadAffinityMask를 이용한 쓰레드 지정하는 방법에 대해 궁금합니다. [4]
5614민우2/22/202213673SSL 통신 문의 [6]
5613김인태2/22/202211914서버와 PC 간의 어플리케이션 성능 차이 [1]
5612팬입니다2/20/202210927Kastrel 서버 관련 [1]
5611차가워2/19/202211094stopWatch 늘어짐 문의 [3]
5610차가워2/18/202210922Stopwatch 늘어짐 문제 [1]
5609cs린이2/15/202211100c# 함수의 호출 방식에 대해 궁금합니다! [2]
5608지호2/10/202211939시작하세요 C# 8.0 중 제네릭타입의 IEnumerable [3]파일 다운로드1
5607이로운2/10/202210567안녕하세요. 궁금한게 있어서 질문드립니다. [1]
5605강성봉2/10/202211066TCP PSH flag 패킷 수신 에러 [1]
5604LW2/9/202211868VISUAL STUDIO 2019 ==> 2020 설치시 오류가 생겨서 문의드립니다. [3]
5603김진명2/9/202212931C# 10.0 출간은 언제쯤 계획하고 계신가요? [1]
5602신갑영2/8/202211419윈폼에 대해서 질문 드립니다. [1]
5601김인태2/4/202211497setup project 관련 [7]
5600itkim2/3/202213373윈도우 서버 계정 패스워드 인증 문의 [5]
5599레드골드2/3/202213742c#으로 ms word 제어 가능할까요? [6]
5598jaew...2/2/202211948Dictionary는 참조형식인가요?? [1]
5597재원2/2/202212443c# 9.0에 대한 내용을 받을 수 있나요? [1]
1  2  3  4  5  6  7  8  9  10  11  12  13  [14]  15  ...