0

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

}
  • 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 https://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. – Peter Duniho Jan 05 '15 at 20:28

1 Answers1

0

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!

Murray Foxcroft
  • 12,785
  • 7
  • 58
  • 86