try
                        {                            senddata.WriteLine("entrybalance," + carbrand);
                            senddata.Flush();                            //tcpClient.ReceiveTimeout = 1000;
                            byte[] Data = new Byte[400];
                            Int32 bytes = ns.Read(Data, 0, Data.Length);
                            receiveData = Encoding.Default.GetString(Data, 0, bytes);
                            MessageBox.Show("2222");
                            double i_balance = Convert.ToDouble(receiveData);
                            if (i_balance <= 30) { c_i_ReasonType = 2; }
                            else { c_b_PermitPass = true; c_i_ReasonType = 0; }
                            c_isConnect = true;
                        }
                        catch
                        {
                        }以上是我的发送和接收代码,如何在这代码设置,接受数据超过1秒就不在接收数据了。然后还可以继续接收下面发送的数据来
问题困惑多天,请教大家帮帮忙。

解决方案 »

  1.   

    你是不是用线程接收的是的话直接在catch里面终止这次循环就行了,就开始接收下一条了
      

  2.   

    socket 接收 超过1秒,不知道 为什么这么设定,
    socket 接收 数据库 会直接放进缓存里面的
    建议楼主 研究一下 异步 发送和接收
    server端
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.Net.Sockets;
    using System.Net;
    using System.Threading;
    namespace se
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
          
            private IPEndPoint MyServer;
            private Socket sock;
            private Socket handler;
            private bool bb = true;        private void Form1_Load(object sender, EventArgs e)
            {
                try
                {
                    MyServer = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 999);
                    sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                    sock.Bind(MyServer);
                    sock.Listen(50);
                    toolStripStatusLabel1.Text = "开始监听 ...";
                    sock.BeginAccept(new AsyncCallback(AcceptCallback), sock);
                }
                catch (Exception ee) { toolStripStatusLabel1.Text = ee.Message; }            Control.CheckForIllegalCrossThreadCalls = false;        }        private void targett()
            {
                if (handler.Connected)
                {
                    toolStripStatusLabel1.Text = "与客户建立连接。";
                    while (bb)
                    {
                        Byte[] bbb = new Byte[640];
                        handler.Receive(bbb, bbb.Length, 0);
                        string ccc = System.Text.Encoding.UTF8.GetString(bbb);
                        string x = "w";
                        int i = ccc.IndexOf(x);
                        if (i > 0)
                        {
                            saveUser(ccc);
                        }
                        richTextBox1.AppendText(ccc + "\r\n");
                    }
                }
            }
            private void saveUser(string str) 
            { 
               
            }
            private void AcceptCallback(IAsyncResult ar)
            {
                toolStripStatusLabel1.Text = "与客户建立连接!";
                handler = ((Socket)ar.AsyncState).EndAccept(ar);
                Thread thread = new Thread(new ThreadStart(targett));
                thread.Start();
            }     }
    }客户端:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.Net.Sockets;
    using System.Net;
    using System.Threading;
    namespace Client
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
      
        private IPEndPoint MyServer; 
            private Socket sock; 
            private bool bb = true; 
                    
            private void Form1_Load(object sender, EventArgs e) 
            { 
                Control.CheckForIllegalCrossThreadCalls = false; 
            } 
            
            private void targett() 
            { 
                while (bb) 
                { 
                    Byte[] bbb = new Byte[640]; 
                    sock.Receive(bbb, bbb.Length, 0); 
                    string aaaaa = System.Text.Encoding.BigEndianUnicode.GetString(bbb);
                    this.richTextBox1.AppendText(aaaaa); 
                } 
            } 
            
            private void button2_Click(object sender, EventArgs e) 
            { 
                try 
                { 
                    Byte[] bytee = new Byte[640];
                    string send = this.richTextBox2.Text + "\r\n"; 
                    bytee = System.Text.Encoding.BigEndianUnicode.GetBytes(send.ToCharArray()); 
                    sock.Send(bytee, bytee.Length, 0); 
                } 
                catch { MessageBox.Show("连接尚未建立! 无法发送!"); } 
            }        private void richTextBox1_TextChanged(object sender, EventArgs e)
            {        }        private void button1_Click(object sender, EventArgs e)
            {
                try
                {
                    Byte[] bytee = new Byte[640];
                    string send = this.richTextBox2.Text + "\r\n";
                    bytee = System.Text.Encoding.UTF8.GetBytes(send.ToCharArray());
                    sock.Send(bytee, bytee.Length, 0);
                }
                catch { MessageBox.Show("连接尚未建立! 无法发送!"); } 
            }        private void button2_Click_1(object sender, EventArgs e)
            {
                try
                {
                    MyServer = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 999);
                    sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                    sock.Connect(MyServer);
                    this.label1.Text = "连接成功!";
                    Thread thread = new Thread(new ThreadStart(targett));
                    thread.Start();
                }
                catch (Exception ee) { MessageBox.Show(ee.Message); } 
            }
        }
    }