问题描述
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;usingSystem.Windows.Media;usingSystem.Windows.Media.Imaging;usingSystem.Windows.Navigation;usingSystem.Windows.Shapes;usingSystem.Net;usingSystem.Net.Sockets;usingSystem.IO;usingSystem.Threading;namespaceWpfApplication32{///<summary>///MainWindow.xaml的交互逻辑///</summary>publicpartialclassMainWindow:Window{publicMainWindow(){IPAddressiPAddress=IPAddress.Any;tcpServer=newTcpListener(iPAddress,333);//999是端口号,可以随便改0-1024,主要不要和什么80,8080之类的常用端口号相冲突哦。tcpServer.Start();Threadt1=newThread(ExceMethod);t1.IsBackground=true;t1.Start();}privatestaticTcpListenertcpServer=null;privatestaticbyte[]bytes=newbyte[256];privatevoidExceMethod(){byte[]msg=Encoding.UTF8.GetBytes("服务端数据");while(true){TcpClientclient=tcpServer.AcceptTcpClient();while(true){try{inti=client.Client.Receive(bytes);Console.WriteLine(DateTime.Now.ToString("G")+"接受:"+Encoding.UTF8.GetString(bytes));stringMes=Encoding.UTF8.GetString(bytes);client.Client.Send(msg);}catch{break;}}client.Close();Thread.Sleep(1000);//10000单位是毫秒,系统在运行过程中,稍微有点停顿,个人感觉会更好一点。}}}}————————————————————————————————————————-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;usingSystem.Windows.Media;usingSystem.Windows.Media.Imaging;usingSystem.Windows.Navigation;usingSystem.Windows.Shapes;usingSystem.Net;usingSystem.Net.Sockets;usingSystem.IO;usingSystem.Threading;namespaceWpfApplication31{///<summary>///MainWindow.xaml的交互逻辑///</summary>publicpartialclassMainWindow:Window{privatestaticTcpClientclient=newTcpClient();publicMainWindow(){TcpClienttcp=newTcpClient();/cp.Connect("127.0.0.1",333);Threadt1=newThread(ExceMethod);t1.IsBackground=true;t1.Start();}privatevoidExceMethod(){while(true){if(!Client.Connected){Test.Content="xxxx";}byte[]data=Encoding.UTF8.GetBytes("客户端数据");Socketsocket=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调用发送数据报套接字时)没有提供地址,发送或接收数据的请求没有被接受。我很纳闷,查了半天网上,都没有解决,求各位大神帮忙看看
解决方案
解决方案二:
错误信息很明显啊。你为什么把连接的那行注释掉呢?TcpClienttcp=newTcpClient();//tcp.Connect("127.0.0.1",333);
解决方案三:
引用1楼Forty2的回复:
错误信息很明显啊。你为什么把连接的那行注释掉呢?TcpClienttcp=newTcpClient();//tcp.Connect("127.0.0.1",333);
那个是粘贴错了,没有注释掉
解决方案四:
顶一下,大神来帮忙
解决方案五:
你贴出来的东西错误百出,确定能跑起来?
解决方案六:
我也在怀疑撸主的代码能编译通过?
解决方案七:
引用5楼lovelj2012的回复:
我也在怀疑撸主的代码能编译通过?
TcpClienttcp=newTcpClient();//tcp.Connect("127.0.0.1",333);打错了,应该是TcpClienttcp=newTcpClient();tcp.Connect("127.0.0.1",333);
解决方案八:
引用4楼shingoscar的回复:
你贴出来的东西错误百出,确定能跑起来?
大神帮帮我吧,我还没学过,找了一周了,还是不能运行,老师要的项目就差网络了
解决方案九:
引用
if(!Client.Connected){Test.Content="xxxx";}
瞅瞅代码是不是执行到里面去了,如果是,检查一下问什么无法连接。因为你这里没有continue,会导致下面的socket发送的时候会抛出异常。
解决方案十:
while(true){TcpClientclient=tcpServer.AcceptTcpClient();//打印client终结点,看看有没有连接进来while(true){