Example #1
0
        public override void OnTest(string[] args)
        {
            if (args.Length != 2)       // Test for correct # of args
            {
                throw new ArgumentException("Parameters: <Destination> <Port>");
            }
            String server   = args[0];              // 목적지 주소
            int    servPort = Int32.Parse(args[1]); // 목적지 포트

            // 서버의 해당 포트에 접속하는 소켓 객체를 생성한다.
            TcpClient     client    = new TcpClient(server, servPort);
            NetworkStream netStream = client.GetStream();

            ItemQuote quote = new ItemQuote(1234567890987654L, "5mm Super Widgets",
                                            100, 12999, true, false);
            // 텍스트 형태로 인코딩 한 가격 책정(quote) 정보를 전송한다.
            ItemQuoteEncoderText coder = new ItemQuoteEncoderText();

            byte[] codedQuote = coder.encode(quote);
            Console.WriteLine("Sending Text-Encoded Quote (" + codedQuote.Length + "bytes): ");
            Console.Write(quote);

            netStream.Write(codedQuote, 0, codedQuote.Length);

            // 이진 형태로 인코딩한 가격 책정 정보를 수신한다.
            ItemQuoteDecoder decoder       = new ItemQuoteDecoderBin();
            ItemQuote        receivedQuote = decoder.decode(client.GetStream());

            Console.WriteLine("Received Binary-Encode Quote: ");
            Console.WriteLine(receivedQuote);

            netStream.Close();
            client.Close();
        }
        public override void OnTest(string[] args)
        {
            if (args.Length < 2 || args.Length > 3) // 파라미터 개수 확인
            {
                throw new ArgumentException("Parameter(s): <Multicast Addr> <Port> [<TTL>]");
            }
            IPAddress destAddr = IPAddress.Parse(args[0]);      // 목적지 주소

            if (!MCIPAddress.isValid(args[0]))
            {
                throw new ArgumentException("Valid MC addr: 224.0.0.0 - 239.255.255.255");
            }
            int destPort = Int32.Parse(args[1]); // 목적지 포트
            int TTL;                             // 데이터그램이 유효한 시간(TTL)

            if (args.Length == 3)
            {
                TTL = Int32.Parse(args[2]);
            }
            else
            {
                TTL = 1;        // 기본 TTL 수치
            }
            ItemQuote quote = new ItemQuote(123456789098765L, "5mm Super Widgets",
                                            1000, 12999, true, false);
            Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);

            //TTL 수치를 설정한다.
            sock.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.MulticastTimeToLive, TTL);

            ItemQuoteEncoderText encoder = new ItemQuoteEncoderText();  // 텍스트 인코딩

            byte[] codedQuote = encoder.encode(quote);

            // IP 엔드포인트 클래스 인스턴스를 생성한다.
            IPEndPoint ipep = new IPEndPoint(destAddr, destPort);

            // 패킷을 생성하고 전송한다.
            sock.SendTo(codedQuote, 0, codedQuote.Length, SocketFlags.None, ipep);
            sock.Close();
        }