从服务器发送和接收数据的简单TCP示例

时间:2016-03-02 04:50:16

标签: c# sockets tcp

出于某种原因,我很难从服务器发送和接收数据。无论如何,这是我的客户代码:

 int recv = server.Receive(data);
    stringData = Encoding.ASCII.GetString(data, 0, recv);
    Console.WriteLine(stringData);

    while (true)
    {
        input = Console.ReadLine();
        if (input == "exit")
            break;
        server.Send(Encoding.ASCII.GetBytes(input));
        data = new byte[1024];
        recv = server.Receive(data);
        stringData = Encoding.ASCII.GetString(data, 0, recv);
        Console.WriteLine(stringData);
    }
    Console.WriteLine("Disconnecting from server...");
    server.Shutdown(SocketShutdown.Both);
    server.Close();

基本上我想使用获取一些关于网络的属性。我知道如何获取属性,但当我尝试将它与客户端一起使用时,它不起作用。

这是服务器代码:

    while (true)
    {
        data = new byte[1024];
        recv = client.Receive(data);
        if (recv == 0)
            break;

        Console.WriteLine(Encoding.ASCII.GetString(data, 0, recv));
        client.Send(data, recv, SocketFlags.None);
    }

    Console.WriteLine("Disconnected from {0}",clientep.Address);

这是我的属性代码:

 ("SettingID: " + mo[0]["SettingID"]
  ("DNSHostName: " + mo[0]["DNSHostName"]
   Console.WriteLine("new Blocking: {0}",test.Blocking);
  Console.WriteLine("Connected: {0}", test.Connected);

  test.Bind(ie);
  IPEndPoint iep = (IPEndPoint)test.LocalEndPoint;
  Console.WriteLine("Local EndPoint: {0}",
                  iep.ToString());

0 个答案:

没有答案