private void sendBtn_Click(object sender, System.EventArgs e)
{
try
{
if(this.stream != null)
{
string msg = sendDataTB.Text;
// Translate the passed message into ASCII and store it as a Byte array.
Byte[] data = System.Text.Encoding.ASCII.GetBytes(msg.ToCharArray());
//Byte[] data = System.Text.Ecoding.Default.GetString(msg);
// System.Text.Ecoding.Default.GetString
// Send the message to the connected TcpServer.
stream.Write(data, 0, data.Length);
this.dataTB.Text += "DEMO: "+msg + "\r\n";
Byte[] svrData = new Byte[256]; // String to store the response ASCII representation.
String responseData = String.Empty; // Read the first batch of the TcpServer response bytes.
Int32 bytes = stream.Read(svrData, 0, svrData.Length);
responseData = System.Text.Encoding.ASCII.GetString(svrData, 0, bytes);
// responseData = System.Text.Ecoding.Default.GetString(svrData, 0, bytes);
this.dataTB.Text += "SERVER: "+ responseData + "\r\n";
}
else
{
throw new Exception("Connection stream is null");
}
}
catch(InvalidOperationException ioex)
{}}
{
try
{
if(this.stream != null)
{
string msg = sendDataTB.Text;
// Translate the passed message into ASCII and store it as a Byte array.
Byte[] data = System.Text.Encoding.ASCII.GetBytes(msg.ToCharArray());
//Byte[] data = System.Text.Ecoding.Default.GetString(msg);
// System.Text.Ecoding.Default.GetString
// Send the message to the connected TcpServer.
stream.Write(data, 0, data.Length);
this.dataTB.Text += "DEMO: "+msg + "\r\n";
Byte[] svrData = new Byte[256]; // String to store the response ASCII representation.
String responseData = String.Empty; // Read the first batch of the TcpServer response bytes.
Int32 bytes = stream.Read(svrData, 0, svrData.Length);
responseData = System.Text.Encoding.ASCII.GetString(svrData, 0, bytes);
// responseData = System.Text.Ecoding.Default.GetString(svrData, 0, bytes);
this.dataTB.Text += "SERVER: "+ responseData + "\r\n";
}
else
{
throw new Exception("Connection stream is null");
}
}
catch(InvalidOperationException ioex)
{}}
解决方案 »
- 大家有没有发现用C#写东西的时候内存跨度很大,比如一个通信的软件会在30-100M之间变化
- webBrowser 问题
- 有谁知道用VS2005做网站时新建文件(如.aspx,.cs)默认用utf-8
- 数据库,更新不成功
- 在VS2005怎样在MDI里Maximize子窗口?Window.State=Maximized时第一次显示时不是最大化状态。
- MD5加密ID号,在DataGrid里显示出问题!求救!
- 那位能给小第一个打印类~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~`(帮帮新人)
- 急急!!请问能否在两个线程中控制同一个timer控件
- 菜鸟问题--怎么进行复合查询?
- GDI+中如何实现GDI中setROP2的功能(C#)?
- 只有10分了,可是这个问题很棘手,没有办法,只好出来请大家帮忙
- 局域网通讯问题
->>
Encoding.Default或者Encoding.UTF8,并且发送和接收时编码要保持一致
Encoding.Unicode 就可以,Unicode 支持中文编码格式