c# - 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;
}
}
}
解决方案
推荐阅读
- node.js - AWS lambda 无法找到 aws-sdk 模块
- clojure - 参数化 hsql/原始字符串格式
- flutter-plugin - 多个二进制信使/方法通道可以通过相同的通道名称发送消息吗?
- apache-beam-io - Apache Beam - 分页读取
- c# - Swashbuckle.AspNetCore 5.Xx 或 6.xx 尝试在我的 .net 5 项目中安装时失败
- leaflet - 如何获得 react-leaflet 或 Leaflet 以动态更改 LayersControl.Overlay 的名称
- javascript - JavaScript 在清楚地正确编写时无法工作
- java - 在 TableView JavaFX 中显示文件中的数据
- c - 如何输出 64 位到 128 位范围内的数字(如 2^64、3^64、4^64 等)?
- android - 无法将数据写入 Firebase 实时数据库 Android