HttpClient: only one use of each socket address is allowed (protocol / network address / port)

using (var client = new HttpClient()) { client.BaseAddress = new Uri(Url); client.DefaultRequestHeaders.Accept.Clear(); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/xml")); using (var task = client.PostAsJsonAsync(Url, body)) { if (task.Result.StatusCode != HttpStatusCode.OK) throw new Exception(task.Result.ReasonPhrase); } 

}

Not sure why we get only one use of each socket address (protocol / network address / port), usually xx.xxx.xxx.xx is allowed: 80 error

 System.AggregateException: One or more errors occurred. ---> System.Net.Http.HttpRequestException: An error occurred while sending the request. ---> System.Net.WebException: Unable to connect to the remote server ---> System.Net.Sockets.SocketException: Only one usage of each socket address (protocol/network address/port) is normally permitted xx.xx.xx.xx:80 at System.Net.Sockets.Socket.EndConnect(IAsyncResult asyncResult) at System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, Socket s4, Socket s6, Socket& socket, IPAddress& address, ConnectSocketState state, IAsyncResult asyncResult, Exception& exception) --- End of inner exception stack trace --- at System.Net.HttpWebRequest.EndGetRequestStream(IAsyncResult asyncResult, TransportContext& context) at System.Net.Http.HttpClientHandler.GetRequestStreamCallback(IAsyncResult ar) --- End of inner exception stack trace --- --- End of inner exception stack trace --- at System.Threading.Tasks.Task`1.GetResultCore(Boolean waitCompletionNotification) 
+5
source share
2 answers

The error in question is WSAEADDRINUSE (10048):

The address is already in use.
Typically, only one use of each socket address (protocol / IP address / port). This error occurs if the application tries to associate a socket with an IP address / port that is already being used for an existing socket or socket that has not been closed properly, or one that is still in the process of being closed. For server applications that need to bind multiple sockets to the same port number, consider using setsockopt (SO_REUSEADDR). client applications usually do not need to bind calls to all-connect, selects not used port automatically. When bind is called with a wildcard address (using ADDR_ANY), the WSAEADDRINUSE error can be delayed until a specific address is reached. This can happen with a call to another function later, including connecting, listening, WSAConnect or WSAJoinLeaf.

This means that you either have multiple HttpClient objects trying to HttpClient with the same local IP / port at the same time, or another application is using the IP port that is trying to use HttpClient .

Most likely, you are probably sending HTTP requests too often and perhaps not completely consuming the responses, which will prevent ASP aggregation and reuse of connections and thus run out of port over time.

+7
source

I hit the same thing in my load generator tool, which was trying to send 200 requests / sec to one server.

I switched from a new instance of HttpClient for each request to singleton, and it accessed it.

One note - at first I hit 2 requests / sec with a bottleneck, but set DefaultConnectionLimit to 500, resolving it:

 ServicePointManager.DefaultConnectionLimit = 500; 
0
source

Source: https://habr.com/ru/post/1204931/


All Articles