不显示删除回复显示所有回复显示星级回复显示得分回复 C# socket客户端程序一运行就卡住了,咋个回事啊???

CSX_Me 2011-11-21 05:40:18
这鬼程序一运行 按连接按钮后就卡卡卡卡卡卡卡死啦!也不知道咋回事!功能可不可实现!
哪位好心人帮忙研究下,作业马上要交了啊!1
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 mmysocket
{
public partial class Form1 : Form
{
Socket sock1;
bool flag = true;
public Form1()
{

InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
IPAddress HostIP = IPAddress.Parse("113.55.44.18");
try
{
IPEndPoint ipep = new IPEndPoint(HostIP, Int32.Parse("2011"));
sock1 = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
sock1.Connect(ipep);

}
catch (Exception ey)
{
MessageBox.Show("服务器没有开启+\r\n" + ey.Message);
}
if(sock1.Connected)
while (flag)
{
byte[] bytes = new byte[1024];
sock1.Receive(bytes, bytes.Length, 0);
string str = Encoding.ASCII.GetString(bytes);
richTextBox1.Text += "client:" + str + "\r\n";
}
}

private void button2_Click(object sender, EventArgs e)
{
try
{
richTextBox1.Text += "client:" + richTextBox2.Text + "\r\n";
string m = richTextBox2.Text;
richTextBox2.Text = "";
byte[] byteData = new byte[1024];
string strsend = "client:" + m + "\r\n";
byteData = Encoding.ASCII.GetBytes(strsend.ToCharArray());
sock1.Send(byteData, byteData.Length, 0);

}
catch { }
}

private void button3_Click(object sender, EventArgs e)
{
Environment.Exit(Environment.ExitCode);
}
}
}
...全文
33 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
gyweiUstc 2012-01-10
  • 打赏
  • 举报
回复
主线程即UI线程一直在循环,那么你的UI就无法得到响应。界面就死了。
lexfu 2011-12-22
  • 打赏
  • 举报
回复
LZ你的读取一直都是在主线程里面进行的呃,flag是true的情况下,while一致在循环,肯定会导致页面失去响应的,建议读取在别的线程中进行。
rlef 2011-12-21
  • 打赏
  • 举报
回复
写的不是很对的,要用独立线程才不会被卡,要不程序一直等待反馈,看上去像卡住了
林g 2011-11-22
  • 打赏
  • 举报
回复
没有接触过socket 的东西,替你把贴顶上去先,然后学习

1,979

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 其他语言讨论
社区管理员
  • 其他语言社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧