Beispiel #1
0
        public override void OnTest(string[] args)
        {
            if (args.Length != 1)   // 파라미터의 개수 확인
            {
                throw new ArgumentException("Parameters: <Port>");
            }

            int port = Int32.Parse(args[0]);

            // 클라이언트의 연결 요청을 수락할 TCPListener 객체를 생성한다.
            TcpListener listener = new TcpListener(IPAddress.Any, port);

            listener.Start();

            TcpClient client = listener.AcceptTcpClient();      // 클라이언트 연결 요청을 수락한다.

            // 텍스트로 인코딩된 가격 책정 정보를 수신한다.
            ItemQuoteDecoder decoder = new ItemQuoteDecoderText();
            ItemQuote        quote   = decoder.decode(client.GetStream());

            Console.WriteLine("Received Text-Encoded Quote: ");
            Console.WriteLine(quote);

            // 가격 책정 정보에서 단가에 10센트를 추가한 후, 이진의 형태로 재전송한다.
            ItemQuoteEncoder encoder = new ItemQuoteEncoderBin();

            quote.unitPrice += 10;      // Add 10 cents to unit price
            Console.WriteLine("Sending (binary)...");
            byte[] bytesToSend = encoder.encode(quote);
            client.GetStream().Write(bytesToSend, 0, bytesToSend.Length);

            client.Close();
            listener.Stop();
        }
Beispiel #2
0
        public override void OnTest(string[] args)
        {
            if (args.Length != 1 && args.Length != 2)   // 파라미터의 개수 확인
            {
                throw new ArgumentException("Parameters: <Port> [<encoding>]");
            }

            int port = Int32.Parse(args[0]);        // 수신 포트

            UdpClient client = new UdpClient(port); // 수신을 위한 UDP 소켓

            byte[]     packet           = new byte[ItemQuoteTextConst.MAX_WIRE_LENGTH];
            IPEndPoint remoteIPEndPoint = new IPEndPoint(IPAddress.Any, port);

            packet = client.Receive(ref remoteIPEndPoint);

            ItemQuoteDecoderText decoder = (args.Length == 2) ? // 인코딩 확인
                                           new ItemQuoteDecoderText(args[1]) : new ItemQuoteDecoderText();

            ItemQuote quote = decoder.decode(packet);

            Console.WriteLine(quote);

            client.Close();
        }
        public override void OnTest(string[] args)
        {
            if (args.Length != 2)   // 파라미터 개수 확인
            {
                throw new ArgumentException("Parameter(s): <Multicast Addr> <Port>");
            }
            IPAddress address = IPAddress.Parse(args[0]);       // 멀티캐스트 주소

            if (!MCIPAddress.isValid(args[0]))
            {
                throw new ArgumentException("Valid MC addr: 224.0.0.0 - 239.255.255.255");
            }
            int    port = Int32.Parse(args[1]);         // 멀티캐스트 포트
            Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram,
                                     ProtocolType.Udp); // 멀티캐스트 메시지 수신 소켓

            // 주소 재사용 옵션을 설정한다.
            sock.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, 1);
            // IPEndPoint 인스턴스를 생성하여 이에 결합한다.
            IPEndPoint ipep = new IPEndPoint(IPAddress.Any, port);

            sock.Bind(ipep);

            // 멀티캐스트 그룹 멤버쉽에 가입한다.
            sock.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership,
                                 new MulticastOption(address, IPAddress.Any));
            IPEndPoint receivePoint     = new IPEndPoint(IPAddress.Any, 0);
            EndPoint   tempReceivePoint = (EndPoint)receivePoint;

            // 데이터그램을 생성하고 수신한다.
            byte[] packet = new byte[ItemQuoteTextConst.MAX_WIRE_LENGTH];
            int    length = sock.ReceiveFrom(packet, 0, ItemQuoteTextConst.MAX_WIRE_LENGTH,
                                             SocketFlags.None, ref tempReceivePoint);

            ItemQuoteDecoderText decoder = new ItemQuoteDecoderText();      // 텍스트 디코딩 작업
            ItemQuote            quote   = decoder.decode(packet);

            Console.WriteLine(quote);

            // 그룹 멤버쉽을 탈퇴한다.
            sock.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.DropMembership,
                                 new MulticastOption(address, IPAddress.Any));
            sock.Close();
        }