C# / .NET socket connection for multiple parts with same IP / socket binding to network card/ip

0

We have 2 PCB's we want to send data to individually, both PCB share the same IP and same Port (192.168.249.2:4000). Now the only difference its that PCB1 its connected to "Ethernet1" card on my PC and PCB2 its connected to "Etherne2t".

PC---------Ethernet 1 (ip 192.168.249.11) ---------- PCB 1 ( ip:192.168.249.2:4000)
PC---------Ethernet 2 (ip 192.168.249.12) ---------- PCB 2 ( ip:192.168.249.2:4000)

How can i bind the code from beyond to only send data to the PCB via Eth1 or Eth2 ?

using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.NetworkInformation;
using System.Runtime.InteropServices;
using System.Net;
using System.Net.Sockets;
using System.IO;

namespace tbxComSrv
{
    [ComVisible(true), GuidAttribute("C905E3B5-4F39-4a29-871C-2E32AA98EE43")]


    public interface ICom
    {
        uint upload(string filename, int port, string ip, int timeout);
        string getErrMsg();
        byte[] getResponse();
    }
    [ComVisible(true), GuidAttribute("B3CF3330-820A-46de-AC42-32BAAB963739")]
    [ProgId("TBX.App")]


    public class TBX: ICom
    {

        string ErrMsg;
        byte[] receiveBytes = new byte[6];
        public string getErrMsg()
        {
            if (ErrMsg == null)
                return "No Error";
            else 
                return ErrMsg;

        }


        public byte[] getResponse()
        {

            return receiveBytes;


        }



        public uint upload(string filename,int port, string ip, int timeout)
        {
            int addressBytesNumber = 4;
            int CheckSum_Bytes = 0;

            byte[] ff = File.ReadAllBytes(filename); // this holds all bytes from the toolbox file
                                                  
            Int32 dataSize = (ff[5] << 24) | (ff[4] << 16) | (ff[3] << 8) | ff[2]; // read data size from toolbox 

            byte[] out_buffer = new byte[dataSize + 2 + addressBytesNumber + CheckSum_Bytes];

            // first 4 bytes indicate how much data we send via ethernet
            out_buffer[0] = (byte)(dataSize & 0x000000FF); // LSB
            out_buffer[1] = (byte)((dataSize & 0x0000FF00) >> 8);
            out_buffer[2] = (byte)((dataSize & 0x00FF0000) >> 16);
            out_buffer[3] = (byte)((dataSize & 0xFF000000) >> 24); // MSB
            out_buffer[0] = (byte)(out_buffer[0] + 2 + addressBytesNumber + CheckSum_Bytes); // add extra 2 bytes

            Int32 bufferSize = (out_buffer[3] << 24) | (out_buffer[2] << 16) | (out_buffer[1] << 8) | out_buffer[0];


            Int32 j = 0;

            for (Int32 i = 4; i < bufferSize; i++)
            {
                out_buffer[i] = ff[j];
                j++;
            }

         
            IPAddress iaddr = IPAddress.Parse(ip);
            IPEndPoint remote = new IPEndPoint(iaddr, port);
            Socket soc = new Socket(iaddr.AddressFamily, SocketType.Stream, ProtocolType.Tcp);

            soc.ReceiveTimeout = timeout;
            soc.SendTimeout = timeout;
            soc.SendBufferSize = 0x0004FFFF;
            soc.ReceiveBufferSize = 4000;
            soc.NoDelay = true;



            try
            {
                soc.Connect(remote);
            
            }
            catch (Exception e)
            {
                ErrMsg = "Socket Connection Error";
                soc.Shutdown(SocketShutdown.Both);
                soc.Close();
                return 2;

            }

            try
            {
                int bytesSent = soc.Send(out_buffer);
            }
            catch (SocketException se)
            {
               
                ErrMsg = "Socket sending data Error";
                soc.Shutdown(SocketShutdown.Both);
                soc.Close();
                return 3;
            }

            for (int v = 0; v < receiveBytes.Length; v++)
                                             receiveBytes[v] = 0;  // clear old values from receive buffer.... just in case

            try
            {
                int bytesRec = soc.Receive(receiveBytes);
              //  Console.WriteLine("Received " + bytesRec.ToString() + " bytes");
                soc.Shutdown(SocketShutdown.Both);
                soc.Close();
                
            }
            catch (SocketException se)
            {
                soc.Shutdown(SocketShutdown.Both);
                soc.Close();
                ErrMsg = "Socket read timeout Error";
                return 4;

            }
           return 0;
        }

    }
}
c#
.net
tcpclient
asked on Stack Overflow Aug 7, 2020 by Raul-Ioan Pop • edited Aug 7, 2020 by Andy

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0