我在一本教材上找的一段聊天程序源码,调试时出现问题

dzhl01 2009-12-13 05:38:39

关键源码如下
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;
using System.Net.Sockets;
using System.Threading;
using System.Text;


namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public IPAddress MyHostIP;
public IPEndPoint MyServer;
public Socket MySocket;
public bool MyChattingState;
public Socket MyAcceptSocket;
public void MyChatProcess()
{//创建聊天线程
byte[] MyReceivedByte=new byte[65];
string MyReceivedString;
if(MyAcceptSocket.Connected)
{
//this.label5.Text = "准备聊天!";
while (MyChattingState)
{
MyAcceptSocket.Receive(MyReceivedByte, MyReceivedByte.Length, SocketFlags.None);
MyReceivedString = Encoding.BigEndianUnicode.GetString(MyReceivedByte);
this.listBox1.Items.Add(MyReceivedString);
}
}
}
private void button1_Click(object sender, EventArgs e)
{//创建聊天室
Thread MyThread;
try
{
MyHostIP = IPAddress.Parse(this.textBox1.Text);
MyServer = new IPEndPoint(MyHostIP, Int32.Parse("8888"));

try
{
MySocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

MySocket.Bind(MyServer);
MySocket.Listen(50);
this.label5.Text = "服务器已经处于监听状态";
MyAcceptSocket = MySocket.Accept();

ThreadStart MyDelegate = new ThreadStart(MyChatProcess);
MyThread = new Thread(MyDelegate);
MyThread.Start();

}
catch { MessageBox.Show("sdcdsv"); }



}
catch(Exception ex)
{
this.label5.Text = ex.Message;
}
}

private void button3_Click(object sender, EventArgs e)
{//发送信息
byte[] MySentByte = new byte[65];
string MySendString;
try
{
MySendString = this.textBox2.Text + "." + this.richTextBox1.Text;
MySentByte = Encoding.BigEndianUnicode.GetBytes(MySendString.ToCharArray());
MyAcceptSocket.Send(MySentByte, MySentByte.Length, SocketFlags.None);
this.richTextBox1.Text = "";
}
catch(Exception ex)
{
this.label5.Text = ex.Message;
}
}
private void button2_Click(object sender, EventArgs e)
{//关闭聊天室
try
{
MySocket.Close();
MyAcceptSocket.Close();
this.label5.Text = "成功断开与客户端程序的连接!";
}
catch(Exception ex)
{
this.label5.Text = "客户端程序可能不存在!";
}
}

private void Form1_Load(object sender, EventArgs e)
{
MyChattingState = true;
}
}
}

当开启聊天室时,程序会死掉,初步分析,是button1_Click(object sender, EventArgs e)函数中的MyAcceptSocket = MySocket.Accept()语句导致,我是初接触网络编程,对此不懂,请问高手如何解决
...全文
100 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
tyg111 2010-06-09
  • 打赏
  • 举报
回复
study hard and make progress ever day
cymandhxl 2009-12-16
  • 打赏
  • 举报
回复
day day up
dzhl01 2009-12-16
  • 打赏
  • 举报
回复
我是业余的,对计算机方面的英语也不是很懂啊
孤剑 2009-12-15
  • 打赏
  • 举报
回复
看一下MSDN的描述吧:

Accept synchronously extracts the first pending connection request from the connection request queue of the listening socket, and then creates and returns a new Socket. You cannot use this returned Socket to accept any additional connections from the connection queue. However, you can call the RemoteEndPoint method of the returned Socket to identify the remote host's network address and port number.

In blocking mode, Accept blocks until an incoming connection attempt is queued. Once a connection is accepted, the original Socket continues queuing incoming connection requests until you close it.

If you call this method using a non-blocking Socket, and no connection requests are queued, Accept throws a SocketException. If you receive a SocketException, use the SocketException..::.ErrorCode property to obtain the specific error code. After you have obtained this code, refer to the Windows Sockets version 2 API error code documentation in the MSDN library for a detailed description of the error.

Note:
Before calling the Accept method, you must first call the Listen method to listen for and queue incoming connection requests.

Note:
This member outputs trace information when you enable network tracing in your application. For more information, see Network Tracing.



建议楼主先看看网络编程的思想,编码时最好多看看MSDN
dzhl01 2009-12-15
  • 打赏
  • 举报
回复
是是button1_Click(object sender, EventArgs e)函数中的MyAcceptSocket = MySocket.Accept()语句导致的问题,只是不知道如何解决
  • 打赏
  • 举报
回复
用Debug,实在不行就单步,看看卡在哪里
代码太长,看不下去
建议你一点点删,在出现问题的时候,关键代码不超过20行,再发上来大家看看

111,120

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

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