在Windows 8.1 TcpClient不发送TcpClient not sending on Windows 8.1

- 此内容更新于:2015-01-06
主题:

原文:

I've got an application using a TCP connection to communicate. When using this application on a Windows 7 machine it will send the data. But when I use the same application on a Windows 8.1 machine it will connect to the server, but it will not send the data.

I've checked how the application connects to the server and on Windows 7 and 8.1 this is the same.

Any ideas why this problem occurs?

Here is the code for the connection class:

public class TcpCon
{
    private TcpClient tcpClient;

    public bool Connected
    {
        get 
        { 
            if(tcpClient != null)
                return tcpClient.Connected;
            return false; 
        }            
    }


    public void Connect(String ipAddr, int port)
    {
        if (tcpClient == null)
        {
            tcpClient = new TcpClient();

            tcpClient.Connect(ipAddr, port);

        }

    }

    public void Disconnect()
    {
        if (tcpClient != null)
        {
            tcpClient.Close();
        }

    }

    public void Send(Byte[] byteArr)
    {
        NetworkStream strWrite = tcpClient.GetStream();
        if (strWrite.CanWrite)
        {
            strWrite.Write(byteArr, 0, byteArr.Length);
            strWrite.Flush();
        }
    }

}
Peter Duniho的回复:

(原文:You didn't even include any code that actually tries to send anything, never mind did you provide a complete code example (which for networking issues needs to include both a server and a client). See stackoverflow.com/help/mcve for details, including the importance of a concise, minimal code example that is still complete. The bottom line for your specific question, given the information so far: if you can successfully connect, but have other problems, it is almost certain you have a bug either in the client or in the server. Lacking the code, no one can point out the bug.)

解决方案:
原文:

Have you checked the firewall settings?

Have you tried running it elevated with admin rights to see if there is a permission issue?

Use WireShark, tcpmon, Fiddler etc to check for issues in the connnection.

Try adding a status check on the connection before sending: http://msdn.microsoft.com/en-us/library/system.net.sockets.tcpclient.connected%28v=vs.110%29.aspx

Try adding a keepalive to your connection:

TcpClient client = ... ;
client.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true);

Wrap an exception handler around the code to check for errors.

In Windows 8.1 are you running it in a Windows App? In this case you need to request network permission? Look for networks in: http://msdn.microsoft.com/en-gb/library/windows/apps/hh464936.aspx

Further investigation this evening and your code works fine on Windows 8.1. I connected from one Windows 8.1 machine to another Windows 8.1 machine on a separate physical machine over the same private network.

I was able to replicate your unresponsive connection and other failures in the following ways:

1) By firewalling - make sure the relevant firewall settings are open on both the "client" and the "server".

2) Connecting to the wrong socket and port.

3) Hosting on the wrong socket or port.

4) By not sending a proper terminator to the server (incorrect encoding for GetBytes). Check that the data you are sending through is correctly formatted to your server's requirements to get the relevant acknowledgement back.

The only other place I can see a potential issue is here:

 if (strWrite.CanWrite)
  {...}

If this is returning false, then your bytes will not get sent, even if your connection succeeds.

That is all I have - good luck!

Mike van der Kaaij的回复:防火墙已经关闭了这个问题。应用程序管理员,它不是一个Windows应用程序。

(原文:The firewall has been turned off for this issue. The application has administrator right and its not an Windows App.)

Mike van der Kaaij的回复:我# 39;Wireshark看起来。在那里我看到连接。只有必须发送的数据不会出现。在我赢得7机。

(原文:I've looked with Wireshark. There I see that the connection is made. Only the data that has to be send wont show up. On my Win 7 machine it is.)

Mike van der Kaaij的回复:我# 39;米使用的IP地址。和服务器可以达到通过腻子。然后发送数据。所以我# 39;m期待变# 39;s的东西。净Win8中的。

(原文:I'm using the IP address. And the server can be reached via putty. Then the data is send. So i'm expecting it's something with .NET on Win8.)

Murray Foxcroft的回复:今天晚上回答更新后进一步调查。好运!

(原文:Answer updated after further investigation this evening. Good luck!)