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

qq_35093621 2016-05-28 04:01:23

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Threading;


namespace WpfApplication32
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{



public MainWindow()
{


IPAddress iPAddress = IPAddress.Any;
tcpServer = new TcpListener(iPAddress, 333);
//999是端口号,可以随便改 0-1024,主要不要和什么80,8080之类的常用端口号相冲突哦。
tcpServer.Start();


Thread t1 = new Thread(ExceMethod);
t1.IsBackground = true;
t1.Start();



}
private static TcpListener tcpServer = null;

private static byte[] bytes = new byte[256];
private void ExceMethod()
{
byte[] msg = Encoding.UTF8.GetBytes("服务端数据");

while (true)
{

TcpClient client = tcpServer.AcceptTcpClient();


while (true)
{
try
{

int i = client.Client.Receive(bytes);

Console.WriteLine(DateTime.Now.ToString("G") + "接受:" + Encoding.UTF8.GetString(bytes));
string Mes = Encoding.UTF8.GetString(bytes);

client.Client.Send(msg);
}

catch
{
break;
}

}
client.Close();

Thread.Sleep(1000);//10000单位是毫秒,系统在运行过程中,稍微有点停顿,个人感觉会更好一点。

}
}
}
}
————————————————————————————————————————-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Threading;

namespace WpfApplication31
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>


public partial class MainWindow : Window
{
private static TcpClient client = new TcpClient();
public MainWindow()
{
TcpClient tcp = new TcpClient();
/cp.Connect("127.0.0.1", 333);

Thread t1 = new Thread(ExceMethod);
t1.IsBackground = true;
t1.Start();








}
private void ExceMethod()
{
while (true)
{

if (!Client.Connected)
{
Test.Content = "xxxx";

}
byte[] data = Encoding.UTF8.GetBytes("客户端数据");


Socket socket = client.Client;
socket.Send(data, data.Length, SocketFlags.None);
//Console.WriteLine("发送成功" + Encoding.UTF8.GetString(data));

socket.Receive(data, SocketFlags.None);
Console.WriteLine("接受数据" + Encoding.UTF8.GetString(data));
Thread.Sleep(1000);

}

}

}


}
那行代码出现了问题,提示 由于套接字没有连接并且(当使用一个 sendto 调用发送数据报套接字时)没有提供地址,发送或接收数据的请求没有被接受。我很纳闷,查了半天网上,都没有解决,求各位大神帮忙看看
...全文
5394 10 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
existpan 2019-04-08
  • 打赏
  • 举报
回复
之前遇到过一次这种错误提示,错误原因是没有一个socket 类来接收客户端发送过来的数据。
yahle 2016-05-30
  • 打赏
  • 举报
回复
引用
if (!Client.Connected) { Test.Content = "xxxx"; }
瞅瞅代码是不是执行到里面去了,如果是,检查一下问什么无法连接。 因为你这里没有continue,会导致下面的socket发送的时候会抛出异常。
小程序员 2016-05-30
  • 打赏
  • 举报
回复
while (true) { TcpClient client = tcpServer.AcceptTcpClient(); //打印client终结点,看看有没有连接进来 while (true) {
qq_35093621 2016-05-28
  • 打赏
  • 举报
回复
引用 4 楼 shingoscar 的回复:
你贴出来的东西错误百出,确定能跑起来?
大神帮帮我吧,我还没学过,找了一周了,还是不能运行,老师要的项目就差网络了
qq_35093621 2016-05-28
  • 打赏
  • 举报
回复
引用 5 楼 lovelj2012 的回复:
我也在怀疑撸主的代码能编译通过?
TcpClient tcp = new TcpClient(); //tcp.Connect("127.0.0.1", 333);打错了,应该是TcpClient tcp = new TcpClient(); tcp.Connect("127.0.0.1", 333);
江南小鱼 2016-05-28
  • 打赏
  • 举报
回复
我也在怀疑撸主的代码能编译通过?
Poopaye 2016-05-28
  • 打赏
  • 举报
回复
你贴出来的东西错误百出,确定能跑起来?
qq_35093621 2016-05-28
  • 打赏
  • 举报
回复
顶一下,大神来帮忙
qq_35093621 2016-05-28
  • 打赏
  • 举报
回复
引用 1 楼 Forty2 的回复:
错误信息很明显啊。 你为什么把连接的那行注释掉呢? TcpClient tcp = new TcpClient(); //tcp.Connect("127.0.0.1", 333);
那个是粘贴错了,没有注释掉
Forty2 2016-05-28
  • 打赏
  • 举报
回复
错误信息很明显啊。 你为什么把连接的那行注释掉呢? TcpClient tcp = new TcpClient(); //tcp.Connect("127.0.0.1", 333);

111,093

社区成员

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

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

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