Ejemplo n.º 1
0
        public void clientAcceptMessage()
        {
            string sTemp;

            while (client_bConnected)
            {
                try
                {
                    sTemp = client_tReader.ReadLine();
                    if (sTemp.Length != 0)
                    {
                        lock (this)
                        {
                            object _pNew = new TrainInit();
                            SerializeTool.deserializeStrToObj(sTemp, out _pNew);
                            richTextBox3.Text = "服务器:" + _pNew.ToString();
                        }
                    }
                }
                catch
                {
                    client_tAcceptMsg.Abort();
                    MessageBox.Show("无法与服务器通讯");
                }
            }
            client_socket.Shutdown(SocketShutdown.Both);
            client_socket.Close();
            client_socket.Shutdown(SocketShutdown.Both);
            client_socket.Close();
        }
Ejemplo n.º 2
0
        //public SendData Client_Listener()
        //{

        //    if (client_socket.Connected)
        //    {
        //        client_nStream = new NetworkStream(client_socket);
        //        client_tReader = new StreamReader(client_nStream);
        //        client_wReader = new StreamWriter(client_nStream);
        //        client_tAcceptMsg = new Thread(new ThreadStart(this.clientAcceptMessage));
        //        client_tAcceptMsg.Start();
        //        client_bConnected = true;
        //        MessageBox.Show("成功建立连接,可以通信");
        //    }
        //    return client_sendata;
        //}
        public void clientAcceptMessage()
        {
            string sTemp;

            while (client_bConnected)
            {
                client_sendata.BConnected = client_bConnected;
                client_sendata.TReader    = client_tReader;
                client_sendata.WReader    = client_wReader;
                try
                {
                    sTemp = client_tReader.ReadLine();
                    if (sTemp.Length != 0)
                    {
                        lock (this)
                        {
                            SerializeTool.deserializeStrToObj(sTemp, out client_pNew);
                            //richTextBox3.Text = "服务器:" + _pNew.ToString();
                            client_sendata.Obj1 = client_pNew;
                        }
                    }
                }
                catch
                {
                    client_tAcceptMsg.Abort();
                    MessageBox.Show("无法与服务器通讯");
                }
            }
            client_socket.Shutdown(SocketShutdown.Both);
            client_socket.Close();
            client_socket.Shutdown(SocketShutdown.Both);
            client_socket.Close();
        }
Ejemplo n.º 3
0
        //public SendData Listener()
        //{
        //    tAcceptMsg = new Thread(new ThreadStart(this.TCPAcceptMessage));
        //    tAcceptMsg.Start();
        //    return sendata;
        //}
        public void TCPAcceptMessage()
        {
            clientSocket = socket.Accept();
            if (clientSocket != null)
            {
                bConnected = true;

                //this.label1.Text = "与客户:" + clientSocket.RemoteEndPoint.ToString() + "成功建立连接";
            }
            nStream = new NetworkStream(clientSocket);
            tReader = new StreamReader(nStream);
            wReader = new StreamWriter(nStream);
            string sTemp;

            while (bConnected)
            {
                sendata.BConnected = bConnected;
                sendata.TReader    = tReader;
                sendata.WReader    = wReader;
                try
                {
                    sTemp = tReader.ReadLine();
                    if (sTemp.Length != 0)
                    {
                        lock (this)
                        {
                            SerializeTool.deserializeStrToObj(sTemp, out Serve_pNew);
                            //richTextBox1.Text = "客户机:" + _pNew;
                            sendata.Obj1 = Serve_pNew;
                        }
                    }
                }
                catch
                {
                    tAcceptMsg.Abort();
                    MessageBox.Show("无法与客户机通讯");
                }
            }
            clientSocket.Shutdown(SocketShutdown.Both);
            clientSocket.Close();
            socket.Shutdown(SocketShutdown.Both);
            socket.Close();
        }
Ejemplo n.º 4
0
        private void richTextBox4_KeyPress_1(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)13)
            {
                if (client_sendata.BConnected)
                {
                    try
                    {
                        lock (this)
                        {
                            TrainInit _p = new TrainInit();
                            _p.DataBaseName      = "SHL07_01";
                            _p.ProgramName       = "SHL07_2017051201";
                            _p.TractionCurveName = "上海大学-杨高南路";
                            _p.PacketVersion     = 001;
                            _p.DataTime          = 563;
                            string result;
                            bool   success = SerializeTool.serializeObjToStr(_p, out result);

                            richTextBox3.Text = "客户机:" + result + richTextBox3.Text;
                            client_sendata.WReader.WriteLine(result);
                            client_sendata.WReader.Flush();
                            richTextBox4.Text = "";
                            richTextBox4.Focus();
                        }
                    }
                    catch
                    {
                        MessageBox.Show("与服务器连接断开");
                    }
                }
                else
                {
                    MessageBox.Show("未与与服务器建立连接,无法通讯");
                }
            }
        }
Ejemplo n.º 5
0
        private void richTextBox2_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)13)
            {
                if (bConnected)
                {
                    try
                    {
                        lock (this)
                        {
                            TrainInit _p = new TrainInit();
                            _p.DataBaseName      = "SHL07_01";
                            _p.ProgramName       = "SHL07_2017051201";
                            _p.TractionCurveName = "上海大学-杨高南路";
                            _p.PacketVersion     = 1;
                            _p.DataTime          = 2012123;
                            string result;
                            bool   success = SerializeTool.serializeObjToStr(_p, out result);
                            richTextBox1.Text = "服务器" + result + richTextBox2.Text;

                            wReader.WriteLine(result);
                            wReader.Flush();
                            richTextBox2.Text = "";
                            richTextBox2.Focus();
                        }
                    }
                    catch
                    {
                        MessageBox.Show("无法与客户机通讯");
                    }
                }
                else
                {
                    MessageBox.Show("未与与客户机建立连接,无法通讯");
                }
            }
        }