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

问题描述

演练一个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连接这部分代码从主窗口代码中移植所造成的,很奇怪,放在主窗口代码中就可以,放在其他地方通过创建对象调用就不行。

时间: 2024-08-07 05:51:44

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

c#Socket提示由于套接字没有连接并且没有提供地址,发送或接收数据的请求没有被接受。

问题描述 usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Windows;usingSystem.Windows.Controls;usingSystem.Windows.Data;usingSystem.Windows.Documents;usingSystem.Windows.Input;usingSyst

C#服务端代码就是必须在服务器上写吗?

问题描述 C#服务端代码就是必须在服务器上写吗? 我这里有个别人的程序,可以上传下载文件,我看了好像上传下载功能是在服务端上http://120.24.93/APPService.svc/GetFiles/写好的这样. 我想实现删除服务器的文件,就是必须在服务器上写服务端代码吗? 解决方案 当然不是,你可以在你的计算机上写程序.但是服务器端代码必须在服务器上运行(这里说的服务器,不一定需要服务器计算机,也可以是pc,但是在系统中充当的角色是服务器) 解决方案二: 服务器端的程序,不是一定要在服务

EaseUIDemo的服务端代码开源吗?

问题描述 可以让我们参考一下吗 ?我觉得那个EaseUIdemo做的还是挺好的,基本上关于IM的都包含了,我希望可以参考一个服务端代码,看看是怎么实现的.php的有吗 ? 解决方案 https://github.com/easemob/emchat-server-examples/tree/master/emchat-server-php   看下这个

[100分]求ms ajax中关于js 调用服务端代码的方法

问题描述 以前用AjaxPro好像很容易,但换成MsAjax后不知该怎么做.要能实现异步回传的,就是在页面中使用了UpdatePanel控件.我提问一般都是给100分的.知道的快来吧. 解决方案 解决方案二:没用过.解决方案三:该回复于2008-04-10 08:33:57被版主删除解决方案四:没用过.解决方案五:该回复于2008-04-10 08:33:55被版主删除解决方案六:我现在正在学一本书叫asp.netajax程序设计第II卷microsoftajaxlibrary异步通信层是陈黎夫

git post-receive 钩子部署服务端代码立马重启生效

本文以部署基于 OpenResty 的服务端程序为例来介绍我的做法. 技术信息     OS: CentOS 6.3    服务器软件: OpenResty    开发语言: Lua 名词解释     服务器: 服务器硬件 + OS    服务端程序: OpenResty 在服务器中的进程    服务端代码: 部署在 OpenResty 中的 Lua 源程序 一.git 服务端钩子类型     pre-receive    在客户端推送时最先执行,可以用它来拒绝客户端的推送.    update

vba-VBAxmlhttp发http请求用post方法,服务端取不到send的数据包

问题描述 VBAxmlhttp发http请求用post方法,服务端取不到send的数据包 vba用xmlhttp发http请求,使用的post方法,可是服务端怎么也抓不到send数据包. http请求消息头是有,消息体怎么也娶不着. request.getInputStream(); debug进去看了,里面确实啥也没有. 代码在内网,贴不出来,汗..... 各位有遇到过类似的问题么?

android端soap请求上传图片字符流,服务端C#

问题描述 android端soap请求上传图片字符流,服务端C# android端soap请求上传图片字符流,服务端C#写的webservice, 调用成功后返回当前图片地址 解决方案 一种方法为:C# webservice返回图片的base64编码,然后你在客户端调用这个ws方法,获取到图片的base64编码后,再转为图片.根据你的描述:返回当前图片地址,你根据这个Url直接请求下载图片就可以了.http://www.cnblogs.com/sun8134/archive/2010/07/05

请教SOCKET 服务端代码

问题描述 //监听函数publicvoidListen(){//初始化SOCKET实例newsock=newSocket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);//初始化终结点实例localEP=newIPEndPoint(IPAddress.Any,setPort);try{//绑定newsock.Bind(localEP);//监听newsock.Listen(10);//用于设置按钮状态m_Listen

[ASP.NET]实现在执行完服务端代码后弹出提醒对话框

asp.net|对话框|执行 大家在开发中经常会遇到这样的情况:点击一个按钮执行一个服务端操作,如:添加数据,添加成功后需要弹出一个提示框来提醒客户.代码如下: private void Button2_Click(object sender, System.EventArgs e){ String scriptString = "<script language=javascript>"; scriptString += "alert('数据写入成功!')&qu