How to Obtain a Source IP Address When Load Balancer Is Present in .NET

Asked 2 years ago, Updated 2 years ago, 97 views

Prerequisites

I am currently creating a TCP server application with the following configuration:

PC (client)<--Internet-->Load balancer-----------Server1
                        -- --------------------------- Server 2
                        -- --------------------------- Server 3

The server application uses the .NET Framework Socket class.

The parameters specified for the socket class are

  • AddressFamily#InterNetwork
  • SocketType#Stream
  • ProtocolType#Tcp

When generating an instance, you set the above (TCP transmit and receive settings).

The environment is also as follows:

  • Client PCs:Windows 7
  • servers:Windows Server 2012
  • Load Balancer:BIG-IP
  • Development Language: C++/CLI
  • Run Environment: .NET Framework 4.5
  • Programming Environment: Visual Studio Pro 2012

The following code was supposed to be able to get the IP address of the connected client, but the IP address of the load balancer is obtained.

The following is a pseudo code

Socket ClientSocket=ServerSocket.EndAccept(iAsyncResult);
String IpAddress=(IPEndPoint)ClientSocket.RemoteEndPoint).Address.ToString();
Console.WriteLine (IpAddress);

--------------
output:load balancer IP address
--------------

After monitoring and investigating packets using WireShark, we found that the IP header contained the IP address of the client PC.Perhaps it's a bad way to get it from the program, but I don't know how to get the IP address listed in the IP header.

I would appreciate it if you could let me know if you have any tips.

windows .net socket

2022-09-30 19:11

2 Answers

Possible

That's about it (out of order).

There was a similar question in the English version, but there was an answer asking if it was proxy.

Socket.RemoteEndPoint returns gateway address

In some cases, it overlaps with the comments, but in general, list the items and methods you would like to review.

Trace information output can be set to enabled to trace Socket.

System.Net.Sockets Information:0:[4292] Socket#45653674-192.168.1.2:58849 to 192.168.1.1:22222.
 C:\>netstat-n
  Active Connections

  Protocol Local Address External Address State
  TCP 127.0.0.1:2222127.0.0.1:51819 ESTABLISHED
  TCP 192.168.1.1:2222192.168.1.2:58849 ESTABLISHED

In this example, there are two connections to the server port (22222), although unnecessary parts are omitted.

Is the client-server connection displayed correctly?Also, are there no load balancer server connections?

Obtain and verify packets with a network analyzer such as Wireshark.

One thing to be aware of at this time is to pay attention to other than "packet as you intend."

In this example, a packet destined for a PC-server is a "intended packet", but does the packet actually have no connection or other communication between the load balancer and the server?

The load balancer translates client or server addresses by (design) configuration.See what the settings are.

Also, if you are performing a health check, the load balancer connects to the server.This may cause unintended errors.(e.g. socket is filled)


2022-09-30 19:11

Since I use Socket instead of WebRequest, I wonder if it's a non-HTTP protocol, but if it's HTTP, I'll put an X-Forwarded-For header in the HTTP header when BIG-IP changes addresses.

http://ja.wikipedia.org/wiki/X-Forwarded-For

If the HTTP header contains information, you can retrieve it from the WebRequest.Headers property.(I didn't try...)


2022-09-30 19:11

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.