问题描述
演练一个Windows应用程序-聊天软件,分别有服务端和客户端问题是在服务端激发buttonStop_Click事件时出现"由于套接字没有连接并且(当使用一个sendto调用发送数据报套接字时)没有提供地址,发送或接收数据的请求没有被接受。"错误,不知道如何解决,麻烦高手帮帮我//下面是服务端代码usingSystem;usingSystem.Drawing;usingSystem.Collections;usingSystem.ComponentModel;usingSystem.Windows.Forms;usingSystem.Data;usingSystem.IO;usingSystem.Net;usingSystem.Net.Sockets;usingSystem.Threading;namespaceWinAp1{///<summary>///Form1的摘要说明。///</summary>publicclassForm1:System.Windows.Forms.Form{privateSystem.Windows.Forms.ButtonbuttonStart;privateSystem.Windows.Forms.GroupBoxgroupBox1;privateSystem.Windows.Forms.RichTextBoxrichTextBoxAc;privateSystem.Windows.Forms.RichTextBoxrichTextBoxSend;privateSystem.Windows.Forms.ListBoxlistBoxState;privateSystem.Windows.Forms.ButtonbuttonSend;privateSystem.Windows.Forms.ButtonbuttonStop;privateSystem.Windows.Forms.TextBoxtextBoxIP;privateSystem.Windows.Forms.TextBoxtextBoxPort;privateSystem.Windows.Forms.Labellabel1;privateSystem.Windows.Forms.Labellabel2;privateSystem.Windows.Forms.Labellabel3;privateSystem.Windows.Forms.Labellabel4;//添加私有成员privateSocketsocket;privateSocketclientSocket;privateThreadthread;///<summary>///必需的设计器变量。///</summary>privateSystem.ComponentModel.Containercomponents=null;publicForm1(){////Windows窗体设计器支持所必需的//InitializeComponent();////TODO:在InitializeComponent调用后添加任何构造函数代码//this.listBoxState.Items.Clear();this.richTextBoxAc.Text="";this.richTextBoxSend.Text="";}///<summary>///清理所有正在使用的资源。///</summary>protectedoverridevoidDispose(booldisposing){if(disposing){if(components!=null){components.Dispose();}}base.Dispose(disposing);}#regionWindows窗体设计器生成的代码///<summary>///设计器支持所需的方法-不要使用代码编辑器修改///此方法的内容。///</summary>privatevoidInitializeComponent(){//由于帖只太长该部分省略}#endregion///<summary>///应用程序的主入口点。///</summary>[STAThread]staticvoidMain(){Application.Run(newForm1());}privatevoidAcceptMessage(){while(true){try{NetworkStreamnetStream=newNetworkStream(clientSocket);byte[]datasize=newbyte[4];netStream.Read(datasize,0,4);intsize=System.BitConverter.ToInt32(datasize,0);byte[]message=newbyte[size];intdataleft=size;intstart=0;while(dataleft>0){intrecv=netStream.Read(message,start,dataleft);start+=recv;dataleft-=recv;}this.richTextBoxAc.Rtf=System.Text.Encoding.Unicode.GetString(message);}catch{this.listBoxState.Items.Add("与客户断开连接");break;}}}privatevoidbuttonStart_Click(objectsender,System.EventArgse){this.buttonStart.Enabled=false;IPAddressip=IPAddress.Parse(this.textBoxIP.Text);IPEndPointserver=newIPEndPoint(ip,Int32.Parse(this.textBoxPort.Text));socket=newSocket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);socket.Bind(server);socket.Listen(10);clientSocket=socket.Accept();this.listBoxState.Items.Add("与客户"+clientSocket.RemoteEndPoint.ToString()+"建立连接");thread=newThread(newThreadStart(AcceptMessage));thread.Start();}privatevoidbuttonSend_Click(objectsender,System.EventArgse){stringstr=this.richTextBoxSend.Rtf;inti=str.Length;if(i==0){return;}else{i*=2;}byte[]datasize=newbyte[4];datasize=System.BitConverter.GetBytes(i);byte[]sendbytes=System.Text.Encoding.Unicode.GetBytes(str);try{NetworkStreamnetStream=newNetworkStream(clientSocket);netStream.Write(datasize,0,4);netStream.Write(sendbytes,0,sendbytes.Length);netStream.Flush();this.richTextBoxSend.Rtf="";}catch{MessageBox.Show("无法发送");}}privatevoidbuttonStop_Click(objectsender,System.EventArgse){this.buttonStart.Enabled=true;socket.Shutdown(SocketShutdown.Both);if(clientSocket.Connected){clientSocket.Close();thread.Abort();}socket.Close();/*catch{MessageBox.Show("监听尚未开始,关闭无效!");}*/}privatevoidForm1_Closing(objectsender,System.ComponentModel.CancelEventArgse){try{socket.Shutdown(SocketShutdown.Both);socket.Close();if(clientSocket.Connected){clientSocket.Close();thread.Abort();}}catch{}}}}
解决方案
解决方案二:
希望哪为高手有时间指点下
解决方案三:
我也是遇到同样的问题,希望高手给予指点,谢谢!
解决方案四:
同样碰到这个问题
解决方案五:
检查一下防火墙,是不是把你程序要使用的端口关掉了!
解决方案六:
我也遇到了同样的问题,不知道楼主的问题解决了没有呀?解决了就在此告诉下小妹呀~~~
解决方案七:
我也遇到了同样的问题,不知道楼主的问题解决了没有呀?解决了就在此告诉下小妹呀~~~
解决方案八:
我也遇到同样问题,搭车同问
解决方案九:
就是ip地址或者端口为空造成的。
解决方案十:
是因为你开启了防火墙造成的,请关闭防火墙或者把邮箱主文件加入到可信程序里面。可以结帖了。
解决方案十一:
使用trycatch强行关闭连接,释放资源。
解决方案十二:
没有网络连接
解决方案十三:
到底是怎么情况有权威说下么
解决方案十四:
人呢,接着问
解决方案十五:
我也遇到这个问题,请高手指点啊.
解决方案:
this.richTextBoxAc.Rtf=System.Text.Encoding.Unicode.GetString(message);我用了断点查找,在这一步出了问题.请高手指点下.
解决方案:
我也遇到这个问题了啊?请高手指点啊
解决方案:
解决方案:
我也遇到了这个问题,是socket.Shutdown(SocketShutdown.Both);造成的,但是是我把socket连接这部分代码从主窗口代码中移植所造成的,很奇怪,放在主窗口代码中就可以,放在其他地方通过创建对象调用就不行。