先谢郭嘉,再谢高手
一个通信客户端,每分钟发送"D704"这个字符串给服务器,服务器收到后与客户端开始通信(每分钟发是为的保持连接),不定时回发一些数据(大概一秒一条,不确定),然后这些信息在richTextBox1后追加现在问题是接收回发时不会写了,求帮我研究下,调试报错误的地方代码有标出来
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 GPRSMonitor
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }        //
        //全局变量------------------------------------------------------------------------------------------------
        //        //配置信息变量
        string server, port;//服务器,端口号
        string sendStr = "D704", outPutStr;//发送字符串,输出显示字符串
        Socket sock, sock1;
        IPEndPoint MyServer;
        int monitorTime;        //
        //主要运算过程--------------------------------------------------------------------------------------------
        //        //创建监测线程
        public void button1_Click(object sender, EventArgs e)
        {
            //各文本框给程序变量赋值
            server = 111.111.111.111;
            port = 9999;            //连接,如果成功开始线程
            try
            {
                //连接
                MyServer = new IPEndPoint(IPAddress.Parse(server), Convert.ToInt32(port));
                sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                sock.Connect(MyServer);
                this.label4.Text = server + ":" + port + " 连接成功";                //开始发送线程
                Thread st = new System.Threading.Thread(sendThreading);
                st.IsBackground = true;
                st.Start();                //开始接收线程
                Thread rt = new System.Threading.Thread(receiveThreading);
                rt.IsBackground = true;
                rt.Start();
            }
            catch (Exception ee)
            {
                //失败后报错
                MessageBox.Show(ee.Message);
                this.label4.Text = "";
            }
        }        //
        //调用函数------------------------------------------------------------------------------------------------
        //        public delegate void mydeleSend();//创建send委托
        public delegate void mydeleReceive();//创建receive委托        //send线程
        public void sendThreading()
        {
            this.button1.BeginInvoke(new mydeleSend(sendMonitorStr));
            System.Threading.Thread.Sleep(1000 * 60 * 1);
            sendThreading();
        }        //receive线程
        public void receiveThreading()
        {
            this.button1.BeginInvoke(new mydeleSend(receiveMonitorStr));
            //System.Threading.Thread.Sleep(1000);
            //receiveThreading();
        }        //监测函数
        private void sendMonitorStr()
        {
            try
            {
                Byte[] byteSend = new Byte[100];
                string send = sendStr;
                byteSend = System.Text.Encoding.Default.GetBytes(send.ToCharArray());
                sock.Send(byteSend, byteSend.Length, 0);
            }
            catch 
            { 
                MessageBox.Show("连接尚未建立! 无法发送!"); 
            } 
        }        //得到返回字符串 
        private void receiveMonitorStr()
        {
            while (true)
            {
                Byte[] byteReceive = new Byte[65535];
                sock.Receive(byteReceive, byteReceive.Length, 0);//<--错误在这里,第一次循环能在下面给outPutStr赋值,第二次就卡住了
                outPutStr = System.Text.Encoding.Default.GetString(byteReceive).Replace("\0", "") + "\r\n";
                this.richTextBox1.AppendText(outPutStr);
            }
        }
    }
}

解决方案 »

  1.   

    顶上,研究过,一直没真正搞太明白过.参考下别人的代码.或msdn上的.我以前写这就是这么干的。
      

  2.   

    楼主这个问题不是多复杂,建议上网找些例子学习,转化为自己的东西,我这里有一个socket的程序例子,可以参考下,例子比较简单,希望能对你有启发;http://blog.csdn.net/kongdelu2008/archive/2010/08/29/5848212.aspx
      

  3.   


    http://blog.csdn.net/kongdelu2008/archive/2010/08/29/5848212.aspx
      

  4.   

    为什么不用ClicentSocket?
    无连接状态发送
    还简单实现
    异步开一个线程