English 中文(简体)
C# / NET中多个部分的连接,与对网络卡/管道具有约束力的IP /袖珍
原标题:C# / .NET socket connection for multiple parts with same IP / socket binding to network card/ip

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)

如何将代码从外加到只通过Eth1或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;
        }

    }
}
问题回答

我正在遇到同样的问题,你是否解决这个问题?





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签