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

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)
232정준명8/30/200512781[질문] HTTP 모듈/핸들러 또는 RewriteURL 관련입니다. [4]
231이진일8/30/200511555참조한 dll 들은 어떻게 배포하나요...? [1]
2303028/29/200512713[질문] 웹에서 Exchange Server 사용자 추가 [2]
229김재명8/26/200512108성태님의 홈페이지 J & J의경우 보안모듈을 어떻게 해결하셨는지 궁금합니다. [1]
227임규진8/24/200512043리소스 파일에 그림 첨가하는 방법 좀 알려 주세요. [1]
226최인호8/19/200511989스마트 클라이언트 exe 버젼 실행시 시간이 너무 오래 걸립니다. [1]
225코디8/17/200512370[질문]닷넷프레임워크 설치여부 [1]
224박준현8/17/200511814[질문]중첩 그리드라고 해야 할까요? [1]
223매커7/28/200511255강좌 잘 읽었습니다. [1]
222김지영7/27/200512680C#에서 oledb 사용하다가 에러 [2]
221박민우7/26/200513292스마트클라이언트에서의 DB접속 [1]
220테드7/22/200512419스마트 클라이언트에서 원치 않는 패킷 송수신 문제... [3]
219김종욱7/21/200512693안녕하세요... 툴밴드 콤보 박스에 대해.. [1]
2187/21/200511333스마트 클라이언트에 대한 질문 입니다. [1]파일 다운로드1
217왕초보7/21/200511955마소강좌의 AxForSmartClient 를 실행시키고 싶은데요... [1]
215궁금증6/23/200511578도대체 이게 무슨 에러 일까요... [1]
213궁금이6/18/200513319Winform 형태의 NTD에서 사용자 로그인 성공 여부 정보 유지하는 방법 [1]
212궁금이6/16/200511205ActiveX 컨트롤이 NTD에 이상이 없는지요? [1]
211류성태6/15/200511870smart client에 관하여.... [1]
206헤커의작...6/14/200510758[질문] 안녕하세요. 성태님의 홈페이지에서 화면전환시 처리되는 것에 관련된 건 [3]
205김종욱 6/12/200511211com+ 에서 서버 소켓을 열고 싶고요... com+에서 thread는 어떻게 돌리는지 알고 싶습니다
209정성태6/14/200511098    답변글 [답변]: com+에서 서버 소켓을 열고 싶고요... com+에서 thread는 어떻게 돌리는지 알고 싶습니다
204정현일6/9/200511454Smart Client 실행시 SecurityException파일 다운로드1
208정성태6/14/200511774    답변글 [답변]: Smart Client 실행시 SecurityException [1]
203나그네6/9/200511806태오 사이트 Smart Client 강좌중에서요
207정성태6/14/200511265    답변글 [답변]: 태오 사이트 Smart Client 강좌중에서요
... 76  77  78  79  80  81  82  83  84  85  86  87  88  [89]  90  ...