Why TCP communication between ports on local machine is not working on different Windows OS's?

235 views Asked by At

I wrote a program to communicate with TcpListener's and TcpClients. A program can be a receiver or a sender. Receiver's order of using TcpListener's/TcpClient's is analogue to sender's order.

On my Windows 10 machine, where I wrote the code, code works fine, but on Windows 7 machine, code does not work.

I get the following error from sender: No connection could be made because the target machine actively refused it 127.0.0.1:8000.

So what is the cause of this problem? Does it have to do with speed of the computer, because W10 machine has an i7 and W7 has an older i5?

Here is the senders code:

private void sendingThreadFunction()
        {
            try
            {
                using (ECDiffieHellmanCng sendingMode = new ECDiffieHellmanCng())
                {
                    using (TextReader reader = File.OpenText("sendersReceivingPort.txt"))
                    {
                        sendersReceivingPort = int.Parse(reader.ReadLine());
                        sendersReceivingPort++;
                    }
                    using (StreamWriter sw = new StreamWriter("sendersReceivingPort.txt"))
                    {
                       sw.WriteLine(sendersReceivingPort.ToString());
                    }
                    sendingMode.KeyDerivationFunction = ECDiffieHellmanKeyDerivationFunction.Hash;
                    sendingMode.HashAlgorithm = CngAlgorithm.Sha256;
                    sendersPublicKey = sendingMode.PublicKey.ToByteArray(); 


                    IPEndPoint ipLocalEndPoint = new IPEndPoint(IPAddress.Parse(ipAddress), sendersReceivingPort);


                    sendingSendPublicKeyNotification = new TcpClient(ipLocalEndPoint);
                    sendingSendPublicKeyNotification.Connect(IPAddress.Parse(ipAddress), receiversReceivingPort);
                    NetworkStream dataStreamNotification = sendingSendPublicKeyNotification.GetStream();
                    byte[] notification = Encoding.UTF8.GetBytes("#PK");
                    dataStreamNotification.Write(notification, 0, notification.Length);
                    dataStreamNotification.Close();
                    sendingSendPublicKeyNotification.Close(); 

                    Thread.Sleep(400);

                    sendingPublicKey = new TcpClient();
                    sendingPublicKey.Connect(IPAddress.Parse(ipAddress), receiversReceivingPort);
                    NetworkStream dataStreamPublicKey = sendingPublicKey.GetStream();
                    dataStreamPublicKey.Write(sendersPublicKey, 0, sendersPublicKey.Length);
                    dataStreamPublicKey.Close();
                    sendingPublicKey.Close(); //poslje public key

                    Thread.Sleep(400);

                    sendingReceivePublicKeyListener = new TcpListener(IPAddress.Parse(ipAddress), sendersReceivingPort);
                    sendingReceivePublicKeyListener.Start();

                    sendingReceivePublicKeyNotificationClient = sendingReceivePublicKeyListener.AcceptTcpClient();
                    NetworkStream dataStreamReceivedNotification = sendingReceivePublicKeyNotificationClient.GetStream();
                    byte[] notificationReceived = new byte[3];
                    dataStreamReceivedNotification.Read(notificationReceived, 0, notificationReceived.Length);
                    dataStreamReceivedNotification.Close();
                    sendingReceivePublicKeyListener.Stop();

                    if(Encoding.UTF8.GetString(notificationReceived) != "#PK")
                    {
                        senderMode.Join();
                    } 


                    sendersListener = new TcpListener(IPAddress.Parse(ipAddress), sendersReceivingPort);
                    sendersListener.Start();

                    sendersNewClient = sendersListener.AcceptTcpClient();
                    NetworkStream dataStreamReceive = sendersNewClient.GetStream();
                    byte[] receiversPublicKey = new byte[1024];
                    dataStreamReceive.Read(receiversPublicKey, 0, receiversPublicKey.Length);
                    dataStreamReceive.Close();
                    receiversPublicKey = Decode(receiversPublicKey);
                    sendersListener.Stop(); //prejmemo prejemnikov public key


                    CngKey secretKey = CngKey.Import(receiversPublicKey, CngKeyBlobFormat.EccPublicBlob);
                    sendersKey = sendingMode.DeriveKeyMaterial(CngKey.Import(receiversPublicKey, CngKeyBlobFormat.EccPublicBlob));




                    byte[] encryptedFile = null;
                    byte[] ivFile = null;
                    byte[] fileBytes = File.ReadAllBytes(fileToSendPath);
                    Encryption(sendersKey, fileBytes, out encryptedFile, out ivFile); 

                    byte[] encryptedFD = null;
                    byte[] ivFD = null;
                    byte[] fdInformation = Encoding.UTF8.GetBytes("#FD|" + fileToSendName + "|" + encryptedFile.Length.ToString());
                    Encryption(sendersKey, fdInformation, out encryptedFD, out ivFD); // kriptira podatke o datoteki

                    sendingFD = new TcpClient();
                    sendingFD.Connect(IPAddress.Parse(ipAddress), receiversReceivingPort);
                    NetworkStream dataStreamFD = sendingFD.GetStream();
                    var mergedFDData = new byte[ivFD.Length + encryptedFD.Length];
                    ivFD.CopyTo(mergedFDData, 0);
                    encryptedFD.CopyTo(mergedFDData, ivFD.Length);
                    dataStreamFD.Write(mergedFDData, 0, mergedFDData.Length);
                    dataStreamFD.Close();
                    sendingFD.Close(); 

                    Thread.Sleep(400); 

                    sendingFile = new TcpClient();
                    sendingFile.Connect(IPAddress.Parse(ipAddress), receiversReceivingPort);
                    NetworkStream dataStreamFile = sendingFile.GetStream();
                    var mergedFileData = new byte[ivFile.Length + encryptedFile.Length];
                    ivFile.CopyTo(mergedFileData, 0);
                    encryptedFile.CopyTo(mergedFileData, ivFile.Length);
                    dataStreamFile.Write(mergedFileData, 0, mergedFileData.Length);
                    dataStreamFile.Close();
                    sendingFile.Close(); //poslje file
                }
            } catch(Exception e)
            {
                MessageBox.Show("Sender - " + e.Message);
                senderMode.Join();
            }
            senderMode.Join();
        }
3

There are 3 answers

0
mac.1 On BEST ANSWER

I have no knowledge in C#, but i doubt that it has something to do with your computer hardware. I would check the following 2 things:

  1. Is any Programm already running on your Windows 7 machine and listening on port 8000 ? Could be a Webserver, a game, any third party software. Or even an older version of your program already running. Try to restart the computer and check again.

  2. Is any firewall running on your windows machine ? May be Windows firewall or a Firewall installed by an antivirus program.

0
GavinF On

*EDIT Just noticed your comment above TIMED_WAIT indicates that the port IS in use.

Its going to be either: A, Windows firewall, go to defender and turn it off to test if it works, if it does you can add an exception and turn it back on. B, The port is in use.

A quick searched revealed that using:

IPEndPoint ipLocalEndPoint = new IPEndPoint(IPAddress.Loopback, sendersReceivingPort);

Will work with the firewall (Atleast for testing purposes) on localhost, you are opening up your computer so its understandable that you must explicitely tell your firewall you want to open up your network!

0
Venom Code On

port 8000 is more than likely used by another program. Especially if it works on one computer but actively refuses on another(one computer may have a program installed that the other doesnt). Try another port(25568 for example)