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

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
{
///
/// MainWindow.xaml 的交互逻辑
///

public partial class MainWindow : Window
{
    private static TcpClient client = new TcpClient();
    public MainWindow()
    {
        //TcpClient tcp = new TcpClient();
        //tcp.Connect("127.0.0.1", 333);
        //if (!tcp.Connected)
        //{
           // Test.Content = "xxxx";

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








    }
    private void ExceMethod()
    {
        while (true)
        {
            TcpClient Client = new TcpClient();
            Client.Connect("127.0.0.1", 333);

            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);

        }

    }

}

}
——————————————————————————————

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
{
///
/// MainWindow.xaml 的交互逻辑
///
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单位是毫秒,系统在运行过程中,稍微有点停顿,个人感觉会更好一点。

        }
    }
}

}

1个回答

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐