qq_35148019 2016-05-28 07:52 采纳率: 0%
浏览 5962

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条回答 默认 最新

  • devmiao 2016-05-28 13:12
    关注
    评论

报告相同问题?

悬赏问题

  • ¥15 如何修改pca中的feature函数
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况
  • ¥15 画两个图 python或R
  • ¥15 在线请求openmv与pixhawk 实现实时目标跟踪的具体通讯方法