首页 > 解决方案 > C#/.NET 套接字连接具有相同 IP 的多个部分/套接字绑定到网卡/ip

问题描述

我们有 2 个 PCB 需要单独发送数据,两个 PCB 共享相同的 IP 和相同的端口(192.168.249.2:4000)。现在唯一的区别是PCB1连接到我PC上的“Ethernet1”卡,PCB2连接到“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)

我如何绑定代码以仅通过 Eth1 或 Eth2 将数据发送到 PCB?

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#.nettcpclient

解决方案


推荐阅读