Exemplo n.º 1
0
 public ClientConnHandler(ManagedMessageMocket msgMocket)
 {
     _mocket = msgMocket;
     PeerUnreachableWarningCallback callback = new PeerUnreachableWarningCallback(this.peerUnreachableCallback);
     _mocket.registerPeerUnreachableWarningCallback(callback);
     _useMockets = true;
 }
Exemplo n.º 2
0
        void ClientThread()
        {
            while (true)
            {
                ManagedMessageMocket mm = new ManagedMessageMocket();
                System.Console.WriteLine("{0}: connecting to localhost:4444", name);
                mm.connect("localhost", 4444);

                int bytes = 0;
                System.Console.WriteLine("{0}: waiting for data", name);
                while (bytes != -1)
                {
                    byte[] buf = new byte[256];
                    bytes = mm.receive(buf, 256);
                    if (bytes != -1)
                    {
                        System.Console.Write("{0}: received {1} bytes: ", name, bytes);
                        System.Console.WriteLine(System.Text.Encoding.ASCII.GetString(buf, 0, bytes));
                    }
                    else
                    {
                        System.Console.WriteLine("{0}: disconnected", name);
                    }
                }
                //System.Threading.Thread.Sleep(500);
            }
        }
Exemplo n.º 3
0
        public static void saveStats(ManagedMessageMocket mocket, String type, long txTime)
        {
            String fname = "stats-" + type + "-mockets-cs.txt";
            FileStream file = new FileStream(fname, FileMode.OpenOrCreate, FileAccess.Write);
            StreamWriter sw = new StreamWriter(file);
            sw.WriteLine("[{0}]\t{1}\t{2}\t{3}\t{4}\t{5}\t{6}\t{7}\t{8}",
                Util.currentTimeMillis(),
                txTime,
                mocket.getStatistics().getSentPacketCount(),
                mocket.getStatistics().getSentPacketCount(),
                mocket.getStatistics().getSentByteCount(),
                mocket.getStatistics().getReceivedPacketCount(),
                mocket.getStatistics().getReceivedByteCount(),
                mocket.getStatistics().getRetransmitCount(),
                mocket.getStatistics().getDuplicatedDiscardedPacketCount() + mocket.getStatistics().getNoRoomDiscardedPacketCount()
                );

            sw.Flush();
            sw.Close();
            file.Close();
        }
Exemplo n.º 4
0
        public int doClientTask(string remoteHost, ushort remotePort, bool useMockets, Stats stats)
        {
            int rc;
            byte[] buf = new byte[1024];

            if (useMockets) {
                Console.WriteLine ("doClientTask: Using MessageMockets");
                ManagedMessageMocket mocket = new ManagedMessageMocket();
                //mocket.bind("127.0.0.1", 6789);

                Console.WriteLine ("doClientTask: MessageMockets: Before connect\n");
                rc = mocket.connect(remoteHost, remotePort);
                if (0 != rc) {
                    Console.WriteLine("doClientTask: failed to connect using MessageMockets to remote host {0} on port {1}; rc = {2}",
                             remoteHost, remotePort, rc);
                    Console.WriteLine("doClientTask: Unable to connect\n");
                    return -1;
                }

                PeerUnreachableWarningCallback callback = new PeerUnreachableWarningCallback(this.peerUnreachableCallback);
                mocket.registerPeerUnreachableWarningCallback(callback);

                int dataSize = IntDataTest.DATA_SIZE;
                int bytesSent = 0;
                long startTime = Util.currentTimeMillis();

                ManagedMessageSender sender = mocket.getSender(true, true);
                byte[] auxBuf = new byte[4];
                Util.fromUnsignedIntTo4Bytes((uint)dataSize, auxBuf, 0);

                // sending the data size.
                sender.send(auxBuf, 4);
                Console.WriteLine("will write {0} bytes of data", dataSize);
                while (bytesSent < dataSize) {
                    sender.send (buf, (uint)buf.Length);
                    bytesSent += buf.Length;
                    //Console.WriteLine("written so far :: {0}", bytesSent);
                }

                Console.WriteLine("done writing data.");

                mocket.receive(auxBuf, 1);
                if (auxBuf[0] != '.') {
                    Console.WriteLine("doClientTask: failed to receive . from remote host");
                    return -2;
                }

                long time = Util.currentTimeMillis() - startTime;
                stats.update((double) time);

                // Save results to a file
                Util.saveStats (mocket, "MsgMockets-cs", time);
            }
            else {
                //Socket socket = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                //socket.Connect(remoteHost, remotePort);
                TcpClient client = new TcpClient(remoteHost, remotePort);
                NetworkStream stream = client.GetStream();

                int dataSize = IntDataTest.DATA_SIZE;
                int bytesSent = 0;
                long startTime = Util.currentTimeMillis();

                byte[] auxBuf = new byte[4];
                Util.fromUnsignedIntTo4Bytes((uint)dataSize, auxBuf, 0);

                // sending the data size.
                //socket.Send (auxBuf);
                stream.Write(auxBuf, 0, 4);
                Console.WriteLine ("socket will write {0} bytes of data", dataSize);
                while (bytesSent < dataSize) {
                    //socket.Send (buf);
                    stream.Write(buf, 0, buf.Length);
                    bytesSent += buf.Length;
                    //Console.WriteLine("written so far :: {0}", bytesSent);
                }
                stream.Flush();

                Console.WriteLine("Socket done writing data.");
                Console.WriteLine("->>:Socket before socket.Receive");
                //socket.Receive (auxBuf);
                stream.Read(auxBuf, 0, 1);
                Console.WriteLine("->>:Socket after socket.Receive", auxBuf[0]);
                if (auxBuf[0] != '.') {
                    Console.WriteLine("doClientTask:Socket failed to receive . from remote host");
                    return -2;
                }

                long time = Util.currentTimeMillis() - startTime;
                stats.update ((double)time);

                // Save results to a file
                Console.WriteLine("->>:Socket before saveStats");
                //Util.saveStats (socket, "Sockets-cs", time);
                Util.saveStats(client, "Sockets-cs", time);
                Console.WriteLine("->>:Socket after saveStats");
            }
            return 0;
        }
Exemplo n.º 5
0
 public Handler(ManagedMessageMocket c)
 {
     this.c = c;
     myThread = new System.Threading.Thread(HandlerThread);
     myThread.Start();
 }