Flush() public method

public Flush ( ) : void
return void
示例#1
0
        //Méthode statique pour l'envoi et la réception
        public void Send(Paquet paquet, NetworkStream stream)
        {
            BinaryFormatter bf = new BinaryFormatter();

            bf.Serialize(stream, paquet);
            stream.Flush();
        }
示例#2
0
        /** 
         * Function to send a message to the server
         * \param stream - The stream to write to
         * \param msg - The message to send
         * \return - Retun true on success, false on failure
         */
        private bool sendMessage(NetworkStream stream, Message msg)
        {
            string json = JsonConvert.SerializeObject(msg);

            using (MemoryStream mstream = new MemoryStream())
            {
                using (BinaryWriter writer = new BinaryWriter(mstream))
                {
                    byte[] encodedJson = Encoding.UTF8.GetBytes(json);
                    writer.Write(hostToBig_i32(encodedJson.Length));
                    writer.Write(encodedJson);
                }

                byte[] request = mstream.ToArray();
                try
                {
                    stream.Write(request, 0, request.Length);
                    stream.Flush();
                }
                catch(SocketException ex)
                {
                    Message emsg = new Message("error_socket", null);
                    emsg.AddParameter("error_code", ex.ErrorCode);
                    emsg.AddParameter("message", ex.Message);                    
                    recvq.Enqueue(emsg);
                    return false;
                }
            }

            return true;
        }
示例#3
0
        public void Write(NetworkStream stream)
        {
            stream.WriteByte((byte)this.Type);

            StreamHelper.WriteDouble(stream, this.Unknown1);
            StreamHelper.WriteDouble(stream, this.Unknown2);
            StreamHelper.WriteDouble(stream, this.Unknown3);
            StreamHelper.WriteFloat(stream, this.Unknown4);
            StreamHelper.WriteInt(stream, Unknown5.Count);

            int i = (int)this.Unknown1;
            int j = (int)this.Unknown2;
            int k = (int)this.Unknown3;
            sbyte j1;
            for (IEnumerator<ChunkPosition> iter = this.Unknown5.GetEnumerator(); iter.MoveNext(); stream.WriteByte(j1))
            {
                ChunkPosition chunkpos = iter.Current;
                int l = chunkpos.X - i;
                int i1 = chunkpos.Y - j;
                j1 = (sbyte)(chunkpos.Z - k);
                stream.WriteByte((byte)l);
                stream.WriteByte((byte)i1);
            }

            stream.Flush();
        }
示例#4
0
        /// <summary>
        /// Start the listener for incoming messages
        /// </summary>
        public void Start()
        {
            tcpListener = new TcpListener(System.Net.IPAddress.Any, this.port);
            tcpListener.Start();
            Console.WriteLine("Server Started") ;
            this.IsListening = true;

            try
            {
                while (isListening)
                {
                    Console.WriteLine("Server listening...");
                    Socket socketForClient = tcpListener.AcceptSocket();
                    Console.WriteLine("Client connected");
                    NetworkStream networkStream = new NetworkStream(socketForClient);

                    BinaryFormatter bf = new BinaryFormatter();
                    Object message = (RemoteMessage)bf.Deserialize(networkStream);

                    networkStream.Flush();

                    interpretMessage((RemoteMessage)message);

                    socketForClient.Disconnect(true);
                }
            }
            catch(Exception e)
            {
                Console.WriteLine(e.ToString()) ;
            }
        }
示例#5
0
        public void Write(NetworkStream stream)
        {
            stream.WriteByte((byte)this.Type);
            StreamHelper.WriteString(stream, this.Username);

            stream.Flush();
        }
 private void send(NetworkStream networkStream, Byte[] bufferOut)
 {
     //------------------------------------------------Send message
     networkStream.Write(bufferOut, 0, bufferOut.Length);
     networkStream.Flush();
     base.ByteSent += bufferOut.Length;
 }
示例#7
0
文件: Form1.cs 项目: Mortion/Uses
        private void button2_Click(object sender, EventArgs e)

        {

            readData = "Conected to Chat Server ...";

            msg();

            clientSocket.Connect("127.0.0.1", 8888);

            serverStream = clientSocket.GetStream();



            byte[] outStream = System.Text.Encoding.ASCII.GetBytes(textBox3.Text + "$");

            serverStream.Write(outStream, 0, outStream.Length);

            serverStream.Flush();



            Thread ctThread = new Thread(getMessage);

            ctThread.Start();

        }
示例#8
0
 public static void SendString(NetworkStream client, string s)
 {
     var bytes = Encoding.UTF8.GetBytes(s);
     client.Write(BitConverter.GetBytes(bytes.Length), 0, 4);
     client.Write(bytes, 0, bytes.Length);
     client.Flush();
 }
示例#9
0
        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                readData = "Connected to Chat Server ...";
                msg();
                clientSocket.Connect("127.0.0.1", 8888);
                serverStream = clientSocket.GetStream();

                byte[] outStream = System.Text.Encoding.ASCII.GetBytes(textBox2.Text + "$");
                serverStream.Write(outStream, 0, outStream.Length);
                serverStream.Flush();

                Thread ctThread = new Thread(getMessage);
                ctThread.Start();

                if(strUser == null)
                {
                    strUser = textBox2.Text;
                    Registry.SetValue(keyName, "user", strUser);
                }

                buttonSend.Enabled = true;
            }
            catch(Exception ex)
            {
                buttonSend.Enabled = false;
            }
        }
示例#10
0
 public virtual void send(NetworkStream ns)
 {
     if (data == null) {
         getBytes ();
     }
     ns.Write (data, 0, data.Length);
     ns.Flush ();
 }
示例#11
0
        public void Write(NetworkStream stream)
        {
            stream.WriteByte((byte)this.Type);

            StreamHelper.WriteShort(stream, this.PlayerHealth);

            stream.Flush();
        }
示例#12
0
 /* This procedure puts a message on the current client stream. */
 private void sendClient(String message)
 {
     byte[] bytesToSend = Encoding.UTF8.GetBytes(message);
     stream.Write(bytesToSend, 0, bytesToSend.Length);
     /* Since we're writing something, flush() must be used. */
     stream.Flush();
     Debug.Log("answer was sent: " + message);
 }
示例#13
0
        public void Write(NetworkStream stream)
        {
            stream.WriteByte((byte)this.Type);

            StreamHelper.WriteInt(stream, this.EntityID);

            stream.Flush();
        }
示例#14
0
        public void Write(NetworkStream stream)
        {
            stream.WriteByte((byte)this.Type);

            stream.WriteByte(this.WindowId);

            stream.Flush();
        }
示例#15
0
        public void Write(NetworkStream stream)
        {
            stream.WriteByte((byte)this.Type);

            stream.WriteByte((OnGround ? (byte)1 : (byte)0));

            stream.Flush();
        }
 public void AsyncProcessResponse (IServerResponseChannelSinkStack sinkStack, object state,
                                   IMessage msg, ITransportHeaders headers, Stream responseStream)
 {
     ClientConnection connection = (ClientConnection)state;
     NetworkStream stream = new NetworkStream (connection.Client);
     UnixMessageIO.SendMessageStream (stream, responseStream, headers, connection.Buffer);
     stream.Flush ();
     stream.Close ();
 }
示例#17
0
        public void Write(NetworkStream stream)
        {
            stream.WriteByte((byte)this.Type);

            StreamHelper.WriteFloat(stream, this.Yaw);
            StreamHelper.WriteFloat(stream, this.Pitch);
            stream.WriteByte((OnGround ? (byte)1 : (byte)0));

            stream.Flush();
        }
示例#18
0
        public void Write(NetworkStream stream)
        {
            stream.WriteByte((byte)this.Type);

            StreamHelper.WriteInt(stream, this.X);
            StreamHelper.WriteInt(stream, this.Y);
            stream.WriteByte((Mode ? (byte)1 : (byte)0));

            stream.Flush();
        }
示例#19
0
        public void Write(NetworkStream stream)
        {
            stream.WriteByte((byte)this.Type);

            StreamHelper.WriteInt(stream, this.EID);
            StreamHelper.WriteInt(stream, this.Target);
            stream.WriteByte(this.Button);

            stream.Flush();
        }
示例#20
0
        public void Write(NetworkStream stream)
        {
            stream.WriteByte((byte)this.Type);

            stream.WriteByte(this.WindowID);
            StreamHelper.WriteShort(stream, this.Token);
            stream.WriteByte((byte)(this.Acknowledged ? 1 : 0));

            stream.Flush();
        }
示例#21
0
        public void Write(NetworkStream stream)
        {
            stream.WriteByte((byte)this.Type);

            stream.WriteByte(this.WindowID);
            StreamHelper.WriteShort(stream, this.Bar);
            StreamHelper.WriteShort(stream, this.Progress);

            stream.Flush();
        }
示例#22
0
        public void Write(NetworkStream stream)
        {
            stream.WriteByte((byte)this.Type);

            StreamHelper.WriteInt(stream, this.EID);
            StreamHelper.WriteShort(stream, this.Slot);
            StreamHelper.WriteShort(stream, this.Primary);
            StreamHelper.WriteShort(stream, this.Secondary);

            stream.Flush();
        }
示例#23
0
        public void Write(NetworkStream stream)
        {
            stream.WriteByte((byte)this.Type);
            StreamHelper.WriteInt(stream, this.Version); //version
            StreamHelper.WriteString(stream, this.Username); //username
            StreamHelper.WriteString(stream, this.ServerPassword); //server password
            StreamHelper.WriteLong(stream, this.MapSeed); //map seed
            stream.WriteByte(this.Dimension); //not used

            stream.Flush();
        }
示例#24
0
        public void Write(NetworkStream stream)
        {
            stream.WriteByte((byte)this.Type);

            stream.WriteByte(this.WindowId);
            stream.WriteByte((byte)this.WindowType);
            StreamHelper.WriteString(stream, this.Title);
            stream.WriteByte(this.Slots);

            stream.Flush();
        }
示例#25
0
        public void Write(NetworkStream stream)
        {
            stream.WriteByte((byte)this.Type);

            StreamHelper.WriteDouble(stream, this.X);
            StreamHelper.WriteDouble(stream, this.Y);
            StreamHelper.WriteDouble(stream, this.Stance);
            StreamHelper.WriteDouble(stream, this.Z);
            stream.WriteByte((OnGround ? (byte)1 : (byte)0));

            stream.Flush();
        }
示例#26
0
        public void Write(NetworkStream stream)
        {
            stream.WriteByte((byte)this.Type);

            stream.WriteByte((byte)this.Status);
            StreamHelper.WriteInt(stream, this.X);
            stream.WriteByte((byte)this.Y);
            StreamHelper.WriteInt(stream, this.Z);
            stream.WriteByte((byte)this.FaceType);

            stream.Flush();
        }
示例#27
0
        public void Write(NetworkStream stream)
        {
            stream.WriteByte((byte)this.Type);

            StreamHelper.WriteInt(stream, this.X);
            stream.WriteByte((byte)this.Y);
            StreamHelper.WriteInt(stream, this.Z);
            stream.WriteByte((byte)this.BlockType);
            stream.WriteByte((byte)this.Metadata);

            stream.Flush();
        }
示例#28
0
        public void Write(NetworkStream stream)
        {
            stream.WriteByte((byte)this.Type);

            StreamHelper.WriteInt(stream, this.a);
            stream.WriteByte((byte)this.b);
            StreamHelper.WriteInt(stream, this.c);
            stream.WriteByte((byte)this.d);
            StreamHelper.WriteInt(stream, this.e);

            stream.Flush();
        }
示例#29
0
        public void Write(NetworkStream stream)
        {
            stream.WriteByte((byte)this.Type);

            StreamHelper.WriteInt(stream, this.X);
            StreamHelper.WriteShort(stream, this.Y);
            StreamHelper.WriteInt(stream, this.Z);
            stream.WriteByte((byte)Instrument);
            stream.WriteByte(this.Pitch);

            stream.Flush();
        }
示例#30
0
        static void Main(string[] args)
        {
            clientSocket.Connect("127.0.0.1", 8888);
            serverStream = clientSocket.GetStream();

            byte[] outStream = System.Text.Encoding.ASCII.GetBytes("LOGIN%0%TATAR1N");
            serverStream.Write(outStream, 0, outStream.Length);
            serverStream.Flush();

            Thread ctThread = new Thread(getMessage);
            ctThread.Start();

            while (true)
            {
                outStream = System.Text.Encoding.ASCII.GetBytes(Console.ReadLine() + "");
                serverStream.Write(outStream, 0, outStream.Length);
                serverStream.Flush();
            }

            Console.ReadKey();
        }
示例#31
0
文件: Message.cs 项目: Richti/CSharp
 public static void send(Message message, NetworkStream stream)
 {
     BinaryFormatter bf = new BinaryFormatter();
     try
     {
         bf.Serialize(stream, message);
         stream.Flush();
     }
     catch (Exception e)
     {
         Console.WriteLine(e.Message);
     }
 }
 public void SendMessage(byte[] bytesToSend)
 {
     try {
         //Send the text
         System.Net.Sockets.NetworkStream ns = default(System.Net.Sockets.NetworkStream);
         lock (Client.GetStream()) {
             ns = Client.GetStream();
             //byte[] bytesToSend = System.Text.Encoding.ASCII.GetBytes(msg);
             ns.Write(bytesToSend, 0, bytesToSend.Length);
             ns.Flush();
         }
     } catch (Exception ex) {
         //MessageBox.Show(ex.ToString);
     }
 }
    public void DisconnectFromServer()
    {
        print("Disconnect form Server");

        if (ns != null)
        {
            byte[] ba = new byte[] { 0x65, 0x6e, 0x64 };//System.Text.Encoding.Unicode.GetBytes ("end");
            ns.Write(ba, 0, ba.Length);
            ns.Flush();
            //閉じる
            ns.Close();
            tcp.Close();
        }

        isConnectedToServer = false;
    }
示例#34
0
        /// <summary>
        /// 发送数据
        /// </summary>
        /// <param name="data"></param>
        public void sendData(Stream sr)
        {
            System.Net.Sockets.NetworkStream ns = tcpClient1.GetStream();
            byte[] bs  = new byte[128];
            int    num = 0;

            //  sr.Position = 0;
            while (sr.CanRead)
            {
                num = sr.Read(bs, 0, bs.Length);
                ns.Write(bs, 0, num);
            }
            //  sr.CopyTo(ns);


            ns.Flush();
        }
示例#35
0
        internal static NewPayLoad ReadPayLoad(TcpClient clientSocket)
        {
            System.Net.Sockets.NetworkStream ns = clientSocket.GetStream();
            byte[] rcvLenBytesBB = new byte[4];

            //TODO: wait till max comm timeout

            while (!ns.DataAvailable)
            {
                //TODO: adaptive sleep
                // General.DoEvents();

                // TODO: try async call instead of loop and thread sleep - = much faster
                // meanwhile we can sleep 1
                Thread.Sleep(1);   //TODO 10 is too big how can we avoid? - change to 1 for super speed but check CPU spin on long requests
                if (!SocketConnected(clientSocket))
                {
                    throw new Exception("GingerSocket.GetResponse ERROR: Lost connection");
                }
            }
            ns.Read(rcvLenBytesBB, 0, 4);

            int rcvLen = ((rcvLenBytesBB[0]) << 24) + (rcvLenBytesBB[1] << 16) + (rcvLenBytesBB[2] << 8) + rcvLenBytesBB[3];

            int received = 0;

            byte[] rcvBytes = new byte[rcvLen + 4];

            //Copy len
            rcvBytes[0] = rcvLenBytesBB[0];
            rcvBytes[1] = rcvLenBytesBB[1];
            rcvBytes[2] = rcvLenBytesBB[2];
            rcvBytes[3] = rcvLenBytesBB[3];

            while (received < rcvLen)
            {
                received += ns.Read(rcvBytes, received + 4, rcvLen - received);
            }

            ns.Flush();

            NewPayLoad plRC = new NewPayLoad(rcvBytes);

            return(plRC);
        }
示例#36
0
 /// <summary>
 /// 发送数据
 /// </summary>
 /// <param name="data"></param>
 public void sendData(byte[] data)
 {
     lock (this)
     {
         try
         {
             System.Net.Sockets.NetworkStream ns = tcpClient1.GetStream();
             ns.Write(data, 0, data.Length);
             ns.Flush();
         }
         catch (ObjectDisposedException ex2)
         {
             connectionDisconnection();
         }
         catch (IOException ex3)
         {
             connectionDisconnection();
         }
     }
 }
        public void SendMessage(Chat.Packet msg)
        {
            try {
                //Send the text
                System.Net.Sockets.NetworkStream ns = default(System.Net.Sockets.NetworkStream);
                lock (_client.GetStream()) {
                    ns = _client.GetStream();

                    // Serialize the message
                    string message = "";
                    message = Chat.Serialize(msg);

                    byte[] bytesToSend = System.Text.Encoding.ASCII.GetBytes(message);
                    ns.Write(bytesToSend, 0, bytesToSend.Length);
                    ns.Flush();
                }
            } catch (Exception ex) {
                //MessageBox.Show(ex.ToString);
            }
        }
        public bool SendMessage(byte[] bytesToSend)
        {
            Exception = null;
            bool result = false;

            try
            {
                //Send the text
                if (TCP.Connected == true)
                {
                    System.Net.Sockets.NetworkStream ns = default(System.Net.Sockets.NetworkStream);
                    lock (TCP.GetStream())
                    {
                        ns = TCP.GetStream();

                        //byte[] bytesToSend = System.Text.Encoding.ASCII.GetBytes(msg);

                        //Sends the text
                        ns.Write(bytesToSend, 0, bytesToSend.Length);
                        ns.Flush();
                    }

                    result = true;
                }
                else
                {
                    //If the client is unable to connect to the IM server
                    //and it tries to send a message, it will display this message.
                    Exception = "Your connection to the server was lost.";
                }
            }
            catch (Exception ex)
            {
                Exception = ex.ToString();
            }

            return(result);
        }
示例#39
0
    /* This function will get started by a new client-specific thread to handle its communication */
    public void handleClient(object obj)
    {
        // retrieve client from parameter passed to thread
        TcpClient client = (TcpClient)obj;

        System.Net.Sockets.NetworkStream currentStream = client.GetStream();

        /* If we have to send the same instruction to all clients, we order all client handlers to do so. */

        while (true)
        {
            if (toAllClients)
            {
                byte[] bytesToSend = Encoding.UTF8.GetBytes(currentInstruction);
                currentInstruction = "N";
                currentStream.Write(bytesToSend, 0, bytesToSend.Length);
                /* Since we're writing something, flush() must be used. */
                currentStream.Flush();
                Debug.Log("answer was sent: " + currentInstruction);
            }
            System.Threading.Thread.Sleep(1000);
        }
    }
示例#40
0
 internal static void Send(System.Net.Sockets.NetworkStream ns, PayLoad pl)
 {
     byte[] b = pl.GetPackage();
     ns.Write(b, 0, b.Length);
     ns.Flush();
 }