Marcos Santos
Member
- Joined
- Nov 11, 2021
- Messages
- 11
- Programming Experience
- 1-3
I have a vps with multiples IP and every request I send to my API I use a different IP (I specify on the request, see the method).
The problem is, if I bind on port 0 only, port gets so slow due, but If I use a different port I get the error:
Error
HttpClient client = HttpController.GetHttpClient(ip, port);
The problem is, if I bind on port 0 only, port gets so slow due, but If I use a different port I get the error:
Error
C#:
Unknown Error => System.Net.Http.HttpRequestException: Only one usage of each socket address (protocol/network address/port) is normally permitted. (myapi:443)
---> System.Net.Sockets.SocketException (10048): Only one usage of each socket address (protocol/network address/port) is normally permitted.
at System.Net.Sockets.Socket.UpdateStatusAfterSocketErrorAndThrowException(SocketError error, String callerName)
at System.Net.Sockets.Socket.DoBind(EndPoint endPointSnapshot, SocketAddress socketAddress)
at System.Net.Sockets.Socket.Bind(EndPoint localEP)
http client:
public static HttpClient GetHttpClient(string ip, int i)
{
Random rnd = new Random();
IPAddress ipAddress = IPAddress.Parse(ip);
if (IPAddress.Any.Equals(ipAddress))
return new HttpClient();
SocketsHttpHandler handler = new SocketsHttpHandler();
handler.ConnectCallback = async (context, cancellationToken) =>
{
Socket socket = new Socket(SocketType.Stream, ProtocolType.Tcp);
socket.Bind(new IPEndPoint(ipAddress, 3));
socket.NoDelay = true;
try
{
await socket.ConnectAsync(context.DnsEndPoint, cancellationToken).ConfigureAwait(false);
return new NetworkStream(socket, true);
}
catch
{
socket.Dispose();
throw;
}
};
return new HttpClient(handler);
}
HttpClient client = HttpController.GetHttpClient(ip, port);