如题,程序界面不能动,没有响应。全部代码如下,请问大侠帮我看看哪里有问题
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.Threading;namespace _232_TO_NET
{ public partial class Form1 : Form
{
private IPAddress IP;
private IPEndPoint MyServer;
private Socket connectsock;
public delegate void TextDelegate();
public int IntTemp;
public string HexString;
public Form1()
{
InitializeComponent();
} private void btconnect_Click(object sender, EventArgs e)
{
try
{
//把textBox1.Text输入的IP地址字符串转换为IPAddress格式;
IP=IPAddress.Parse (textIP.Text);
}
catch
{
MessageBox.Show ("您输入的IP地址格式不正确!");
}
try
{
MyServer=new IPEndPoint(IP,Int32.Parse (textPort.Text ));
connectsock=new Socket(AddressFamily.InterNetwork ,SocketType.Stream ,ProtocolType.Tcp );
connectsock.Connect (MyServer);
label1.Text="与主机"+textIP.Text +"端口"+textPort.Text+"连接成功!\r\n";
}
catch(Exception ee)
{
MessageBox.Show(ee.Message,"连接失败");
}
Thread thread1=new Thread (new ThreadStart (receive));
thread1.Start ();
}
private void receive()
{
TextDelegate dd = new TextDelegate(this.receivefun);
textReceive.BeginInvoke(dd);
//Thread thread2 = new Thread(new ThreadStart(receive));
//thread2.Start();
//Thread.Sleep(10000);
}
public void receivefun()
{
while (true)
{
Byte[] ReceiveBytes = new byte[32];
NetworkStream netstream = new NetworkStream(connectsock);
netstream.Read(ReceiveBytes, 0, ReceiveBytes.Length);//读取客户发送来的信息。
if (checkBox1.Checked)
{
foreach (char letter in ReceiveBytes)
{
if (letter != 0)
{
IntTemp = Convert.ToInt32(letter);
HexString = String.Format("{0:X}", IntTemp);
if (letter < 0x10)
textReceive.AppendText('0' + HexString + ' ');//写入到接收信息栏中。
else
textReceive.AppendText(HexString + ' ');//写入到接收信息栏中。
}
}
textReceive.AppendText("\n");
}
else
{
string RecvMessage = System.Text.Encoding.ASCII.GetString(ReceiveBytes);
textReceive.AppendText(RecvMessage);//写入到接收信息栏中。
}
}
}
private void btsend_Click(object sender, EventArgs e)
{
try
{
Byte[] SendBytes=new Byte [64];
string SendString=textSend.Text;
//生成NetworkStream实例,用于发送基础数据流。
NetworkStream netstream=new NetworkStream (connectsock);
SendBytes=System.Text .Encoding .BigEndianUnicode .GetBytes(SendString.ToCharArray ());
netstream.Write (SendBytes,0,SendBytes.Length );//向socket服务器发送信息。
netstream.Flush ();
}
catch
{
MessageBox.Show ("连接没有建立!无法发送!");
} }
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.Threading;namespace _232_TO_NET
{ public partial class Form1 : Form
{
private IPAddress IP;
private IPEndPoint MyServer;
private Socket connectsock;
public delegate void TextDelegate();
public int IntTemp;
public string HexString;
public Form1()
{
InitializeComponent();
} private void btconnect_Click(object sender, EventArgs e)
{
try
{
//把textBox1.Text输入的IP地址字符串转换为IPAddress格式;
IP=IPAddress.Parse (textIP.Text);
}
catch
{
MessageBox.Show ("您输入的IP地址格式不正确!");
}
try
{
MyServer=new IPEndPoint(IP,Int32.Parse (textPort.Text ));
connectsock=new Socket(AddressFamily.InterNetwork ,SocketType.Stream ,ProtocolType.Tcp );
connectsock.Connect (MyServer);
label1.Text="与主机"+textIP.Text +"端口"+textPort.Text+"连接成功!\r\n";
}
catch(Exception ee)
{
MessageBox.Show(ee.Message,"连接失败");
}
Thread thread1=new Thread (new ThreadStart (receive));
thread1.Start ();
}
private void receive()
{
TextDelegate dd = new TextDelegate(this.receivefun);
textReceive.BeginInvoke(dd);
//Thread thread2 = new Thread(new ThreadStart(receive));
//thread2.Start();
//Thread.Sleep(10000);
}
public void receivefun()
{
while (true)
{
Byte[] ReceiveBytes = new byte[32];
NetworkStream netstream = new NetworkStream(connectsock);
netstream.Read(ReceiveBytes, 0, ReceiveBytes.Length);//读取客户发送来的信息。
if (checkBox1.Checked)
{
foreach (char letter in ReceiveBytes)
{
if (letter != 0)
{
IntTemp = Convert.ToInt32(letter);
HexString = String.Format("{0:X}", IntTemp);
if (letter < 0x10)
textReceive.AppendText('0' + HexString + ' ');//写入到接收信息栏中。
else
textReceive.AppendText(HexString + ' ');//写入到接收信息栏中。
}
}
textReceive.AppendText("\n");
}
else
{
string RecvMessage = System.Text.Encoding.ASCII.GetString(ReceiveBytes);
textReceive.AppendText(RecvMessage);//写入到接收信息栏中。
}
}
}
private void btsend_Click(object sender, EventArgs e)
{
try
{
Byte[] SendBytes=new Byte [64];
string SendString=textSend.Text;
//生成NetworkStream实例,用于发送基础数据流。
NetworkStream netstream=new NetworkStream (connectsock);
SendBytes=System.Text .Encoding .BigEndianUnicode .GetBytes(SendString.ToCharArray ());
netstream.Write (SendBytes,0,SendBytes.Length );//向socket服务器发送信息。
netstream.Flush ();
}
catch
{
MessageBox.Show ("连接没有建立!无法发送!");
} }
}
}
解决方案 »
- winform datagridview+listbox,仿百度搜索提示效果
- winform窗体中 DataGridView显示图片
- javascript bold(),fontsize
- 关于URL的截取
- 小弟 新手 求CS开发中文件下载的 代码
- 在线等!!怎样自动启动COM+
- 熟悉自动事务的进来(com+)(是高手的请进)
- 初学C#,请教一些关于事件的问题,恳求大家帮助
- 怎样用C#生成一个能在其它机子直接运行的exe文件(机子上装有.net)
- 为什么我用ASP.NET制作的登陆页面只能登陆一个用户,登陆其他的用户就会提示密码错误,请教高手
- 在窗体中实例化自己建的类对象,需要手动销毁吗?
- 压缩文件夹下的文件个数
又给UI线程阻塞了
先看下别人如何写得,到网上找些例子看下
www.codeproject.com上面有很多
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.Threading;namespace _232_TO_NET
{ public partial class Form1 : Form
{
private IPAddress IP;
private IPEndPoint MyServer;
private Socket connectsock;
public delegate void TextDelegate();
public int IntTemp;
public string HexString;
public Byte[] ReceiveBytes = new byte[32];
public Form1()
{
InitializeComponent();
} private void btconnect_Click(object sender, EventArgs e)
{
try
{
//把textBox1.Text输入的IP地址字符串转换为IPAddress格式;
IP=IPAddress.Parse (textIP.Text);
}
catch
{
MessageBox.Show ("您输入的IP地址格式不正确!");
}
try
{
MyServer=new IPEndPoint(IP,Int32.Parse (textPort.Text ));
connectsock=new Socket(AddressFamily.InterNetwork ,SocketType.Stream ,ProtocolType.Tcp );
connectsock.Connect (MyServer);
label1.Text="与主机"+textIP.Text +"端口"+textPort.Text+"连接成功!\r\n";
}
catch(Exception ee)
{
MessageBox.Show(ee.Message,"连接失败");
}
Thread thread1=new Thread (new ThreadStart (receive));
thread1.Start ();
}
private void receive()
{
NetworkStream netstream = new NetworkStream(connectsock);
netstream.Read(ReceiveBytes, 0, ReceiveBytes.Length);//读取客户发送来的信息。
TextDelegate dd = new TextDelegate(this.receivefun);
textReceive.BeginInvoke(dd);
Thread thread2 = new Thread(new ThreadStart(receive));
thread2.Start();
//Thread.Sleep(10000);
}
public void receivefun()
{ if (checkBox1.Checked)
{
foreach (char letter in ReceiveBytes)
{
if (letter != 0)
{
IntTemp = Convert.ToInt32(letter);
HexString = String.Format("{0:X}", IntTemp);
if (letter < 0x10)
textReceive.AppendText('0' + HexString + ' ');//写入到接收信息栏中。
else
textReceive.AppendText(HexString + ' ');//写入到接收信息栏中。
}
}
textReceive.AppendText("\n");
}
else
{
string RecvMessage = System.Text.Encoding.ASCII.GetString(ReceiveBytes);
textReceive.AppendText(RecvMessage);//写入到接收信息栏中。
}
}
private void btsend_Click(object sender, EventArgs e)
{
try
{
Byte[] SendBytes=new Byte [64];
string SendString=textSend.Text;
//生成NetworkStream实例,用于发送基础数据流。
NetworkStream netstream=new NetworkStream (connectsock);
SendBytes=System.Text .Encoding .BigEndianUnicode .GetBytes(SendString.ToCharArray ());
netstream.Write (SendBytes,0,SendBytes.Length );//向socket服务器发送信息。
netstream.Flush ();
}
catch
{
MessageBox.Show ("连接没有建立!无法发送!");
} }
}
}