先谢郭嘉,再谢高手
一个通信客户端,每分钟发送"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);
}
}
}
}
一个通信客户端,每分钟发送"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);
}
}
}
}
解决方案 »
- exe程序运行时会中断
- 如何理解构造函数与析构函数?
- vs内置ChangePassword控件视图
- 【在线观望】if----else if----else 的小小问题
- 请问在C#中如何用Reflection技术读取Linq to sql 类中DataAnnotation中的属性?
- 如何将DataGrid中的null变为空白
- 有个关于此组件的问题,那个大哥大姐帮帮小弟!我搞了N久了!
- 小问题高手指点?
- c# WinForm DataGridView 刷新巨慢
- 为何我无法安装VisualStudio.net
- 怎么把C#的数组存到oracle数据库的表中?
- 用timer控件更新datagridview怎么一块更新表中的数据?
http://blog.csdn.net/kongdelu2008/archive/2010/08/29/5848212.aspx
无连接状态发送
还简单实现
异步开一个线程