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
    关注
    评论

报告相同问题?

悬赏问题

  • ¥20 求用stm32f103c6t6在lcd1206上显示Door is open和password:
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类