由于套接字没有连接并且(当使用一个 sendto 调用发送数据报套接字时)没有提供地址,发送或接收数据的请求没有被接受。(含服务端代码)

hualangk30 2008-02-17 03:26:43
演练一个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{}
}



}
}



...全文
10893 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
桐桐 2012-03-08
  • 打赏
  • 举报
回复
我也遇到了这个问题,是socket.Shutdown(SocketShutdown.Both);造成的,但是是我把socket连接这部分代码从主窗口代码中移植所造成的,很奇怪,放在主窗口代码中就可以,放在其他地方通过创建对象调用就不行。
christ 2012-02-23
  • 打赏
  • 举报
回复
csu309 2011-08-26
  • 打赏
  • 举报
回复
我也遇到这个问题了啊?请高手指点啊
clydehzy 2010-11-16
  • 打赏
  • 举报
回复
this.richTextBoxAc.Rtf=System.Text.Encoding.Unicode.GetString(message);
我用了断点查找,在这一步出了问题.请高手指点下.
clydehzy 2010-11-16
  • 打赏
  • 举报
回复
我也遇到这个问题,请高手指点啊.
starlistener 2010-11-03
  • 打赏
  • 举报
回复
人呢,接着问
starlistener 2010-11-02
  • 打赏
  • 举报
回复
到底是怎么情况有权威说下么
beautifulmouse 2010-09-16
  • 打赏
  • 举报
回复
没有网络连接
xueyuan299 2010-06-28
  • 打赏
  • 举报
回复
使用try catch强行关闭连接,释放资源。
book_mp3boy 2010-06-28
  • 打赏
  • 举报
回复
是因为你开启了防火墙造成的,请关闭防火墙或者把邮箱主文件加入到可信程序里面。可以结帖了。
火星大能猫 2010-06-06
  • 打赏
  • 举报
回复
就是ip地址或者端口为空造成的。
zhy_nobel 2010-04-23
  • 打赏
  • 举报
回复
我也遇到同样问题,搭车同问
rachelshao 2009-11-02
  • 打赏
  • 举报
回复
我也遇到了同样的问题,不知道楼主的问题解决了没有呀?解决了就在此告诉下小妹呀~~~
rachelshao 2009-11-02
  • 打赏
  • 举报
回复
我也遇到了同样的问题,不知道楼主的问题解决了没有呀?解决了就在此告诉下小妹呀~~~
tongyiyi 2009-04-18
  • 打赏
  • 举报
回复
检查一下防火墙,是不是把你程序要使用的端口关掉了!
talayayaya 2009-02-24
  • 打赏
  • 举报
回复
同样碰到这个问题
zyxgy 2008-07-01
  • 打赏
  • 举报
回复
我也是遇到同样的问题,希望高手给予指点,谢谢!
hualangk30 2008-02-17
  • 打赏
  • 举报
回复
希望哪为高手有时间指点下

110,536

社区成员

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

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

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