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

qq_35093621 qq_35093621 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区

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 调用发送数据报套接字时)没有提供地址,发送或接收数据的请求没有被接受。我很纳闷,查了半天网上,都没有解决,求各位大神帮忙看看
...全文
评论 10
收藏 已收藏
分享
返回顶部
qq_35093621
C# 111,128 成员 642,537 内容