HttpClient:通常只允许每个套接字地址(协议/网络地址/端口)使用一种 [英] HttpClient: Only one usage of each socket address (protocol/network address/port) is normally permitted

查看:173
本文介绍了HttpClient:通常只允许每个套接字地址(协议/网络地址/端口)使用一种的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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);
 }

}

不确定为什么我们通常只能允许每个套接字地址(协议/网络地址/端口)使用一次xx.xxx.xxx.xx:80错误

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)

解决方案

有问题的错误是WSAEADDRINUSE(10048):

地址已被使用.
通常,每个插槽只能使用一种 地址(协议/IP地址/端口)是允许的.如果发生此错误 应用程序尝试将套接字绑定到 已经用于现有的套接字,或者尚未用于 已正确关闭,或者仍在关闭过程中.为了 需要将多个套接字绑定到同一服务器的服务器应用程序 端口号,请考虑使用setsockopt(SO_REUSEADDR).客户 应用程序通常根本不需要调用绑定-连接选择一个 自动使用未使用的端口.使用通配符地址调用bind时 (涉及ADDR_ANY),则WSAEADDRINUSE错误可能会延迟到 具体地址已提交.可能会发生以下情况: 稍后再提供另一个功能,包括连接,侦听,WSAConnect或 WSAJoinLeaf.

这意味着您或者有多个HttpClient对象试图同时将自己绑定到相同的本地IP/端口,或者另一个应用程序正在使用HttpClient试图同时使用的IP/端口. /p>

更有可能的是,您可能过于频繁地发布HTTP请求,并且可能没有完全占用响应,这将阻止ASP池化和重用连接,从而随着时间的推移遇到端口耗尽的情况.

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 the Only one usage of each socket address (protocol/network address/port) is normally permitted xx.xxx.xxx.xx: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)

解决方案

The error in question is WSAEADDRINUSE (10048):

Address already in use.
Typically, only one usage of each socket address (protocol/IP address/port) is permitted. This error occurs if an application attempts to bind a socket to an IP address/port that has already been used for an existing socket, or a socket that was not closed properly, or one that is still in the process of closing. For server applications that need to bind multiple sockets to the same port number, consider using setsockopt (SO_REUSEADDR). Client applications usually need not call bind at all—connect chooses an unused port automatically. When bind is called with a wildcard address (involving ADDR_ANY), a WSAEADDRINUSE error could be delayed until the specific address is committed. This could happen with a call to another function later, including connect, listen, WSAConnect, or WSAJoinLeaf.

Which means you either have multiple HttpClient objects trying to bind themselves to the same local IP/Port at the same time, or another app is using an IP/Port that an HttpClient is trying to also use.

More likely, you are probably posting HTTP requests too often, and maybe not fully consuming the responses, which would prevent ASP from pooling and reusing connections and thus encountering port exhaustion over time.

这篇关于HttpClient:通常只允许每个套接字地址(协议/网络地址/端口)使用一种的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆