由于套接字没有连接并且(当使用一个 sendto 调用发送数据报套接字时)没有提供地址,发送或接收数据的请求没有被接受。(含服务端代码)
演练一个Windows应用程序-聊天软件,分别有服务端和客户端
问题是在服务端激发buttonStop_Click事件时出现"由于套接字没有连接并且(当使用一个 sendto 调用发送数据报套接字时)没有提供地址,发送或接收数据的请求没有被接受。"错误,不知道如何解决,麻烦高手帮帮我
//下面是服务端代码
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Threading;
namespace WinAp1
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button buttonStart;
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.RichTextBox richTextBoxAc;
private System.Windows.Forms.RichTextBox richTextBoxSend;
private System.Windows.Forms.ListBox listBoxState;
private System.Windows.Forms.Button buttonSend;
private System.Windows.Forms.Button buttonStop;
private System.Windows.Forms.TextBox textBoxIP;
private System.Windows.Forms.TextBox textBoxPort;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Label label4;
//添加私有成员
private Socket socket;
private Socket clientSocket;
private Thread thread;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
this.listBoxState.Items.Clear();
this.richTextBoxAc.Text="";
this.richTextBoxSend.Text="";
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
//由于帖只太长该部分省略
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void AcceptMessage()
{
while(true)
{
try
{
NetworkStream netStream=new NetworkStream(clientSocket);
byte[] datasize=new byte[4];
netStream.Read(datasize,0,4);
int size=System.BitConverter.ToInt32(datasize,0);
byte[] message=new byte[size];
int dataleft=size;
int start=0;
while(dataleft>0)
{
int recv=netStream.Read(message,start,dataleft);
start+=recv;
dataleft-=recv;
}
this.richTextBoxAc.Rtf=System.Text.Encoding.Unicode.GetString(message);
}
catch
{
this.listBoxState.Items.Add("与客户断开连接");
break;
}
}
}
private void buttonStart_Click(object sender, System.EventArgs e)
{
this.buttonStart.Enabled=false;
IPAddress ip=IPAddress.Parse(this.textBoxIP.Text);
IPEndPoint server=new IPEndPoint(ip,Int32.Parse(this.textBoxPort.Text));
socket=new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
socket.Bind(server);
socket.Listen(10);
clientSocket=socket.Accept();
this.listBoxState.Items.Add("与客户"+clientSocket.RemoteEndPoint.ToString()+"建立连接");
thread=new Thread(new ThreadStart(AcceptMessage));
thread.Start();
}
private void buttonSend_Click(object sender, System.EventArgs e)
{
string str=this.richTextBoxSend.Rtf;
int i=str.Length;
if(i==0)
{
return;
}
else
{
i*=2;
}
byte[] datasize=new byte[4];
datasize=System.BitConverter.GetBytes(i);
byte[] sendbytes=System.Text.Encoding.Unicode.GetBytes(str);
try
{
NetworkStream netStream=new NetworkStream(clientSocket);
netStream.Write(datasize,0,4);
netStream.Write(sendbytes,0,sendbytes.Length);
netStream.Flush();
this.richTextBoxSend.Rtf="";
}
catch
{
MessageBox.Show("无法发送");
}
}
private void buttonStop_Click(object sender, System.EventArgs e)
{
this.buttonStart.Enabled=true;
socket.Shutdown(SocketShutdown.Both);
if(clientSocket.Connected)
{
clientSocket.Close();
thread.Abort();
}
socket.Close();
/*catch
{
MessageBox.Show("监听尚未开始,关闭无效!");
}*/
}
private void Form1_Closing(object sender,System.ComponentModel.CancelEventArgs e)
{
try
{
socket.Shutdown(SocketShutdown.Both);
socket.Close();
if(clientSocket.Connected)
{
clientSocket.Close();
thread.Abort();
}
}
catch{}
}
}
}