我在.NET 2.0/3.5环境中使用以下代码数月(没有问题):
string server="192.168.1.3";
IPHostEntry ipe = System.Net.Dns.GetHostEntry(server);
IPAddress ipa = ipe.AddressList[0];
IPEndPoint ipep = new IPEndPoint(ipa, (int)UdpServices.Domain);
Run Code Online (Sandbox Code Playgroud)
在这里,服务器硬编码为IP地址,但在我的应用程序中,它可能类似于"server.test.com".
将我的项目转换为.NET 4.0时,此代码在直接传递IP地址时仍然停止工作(仍然使用主机名).它崩溃了这个例外:
System.Net.Sockets.SocketException was unhandled
Message=The requested name is valid, but no data of the requested type was found
Source=System
ErrorCode=11004
NativeErrorCode=11004
StackTrace:
at System.Net.Dns.InternalGetHostByAddress(IPAddress address, Boolean includeIPv6)
at System.Net.Dns.GetHostEntry(String hostNameOrAddress)
Run Code Online (Sandbox Code Playgroud)
因为我需要的只是生成的IPEndPoint,我可以通过使用IPAddress.Parse来生成IPAddress对象来解决这个问题,但我想知道你们是否知道为什么在.NET 4.0中这种行为发生了变化?(如果我们无法从IP地址解析主机名,则会抛出异常).
我有一个非常简单的异步UDP侦听器,设置为服务,它现在已经运行了一段时间,但它最近在SocketException上崩溃了An existing connection was forcibly closed by the remote host.我有三个问题:
我的代码如下所示:
private Socket udpSock;
private byte[] buffer;
public void Starter(){
//Setup the socket and message buffer
udpSock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
udpSock.Bind(new IPEndPoint(IPAddress.Any, 12345));
buffer = new byte[1024];
//Start listening for a new message.
EndPoint newClientEP = new IPEndPoint(IPAddress.Any, 0);
udpSock.BeginReceiveFrom(buffer, 0, buffer.Length, SocketFlags.None, ref newClientEP, DoReceiveFrom, udpSock);
}
private void DoReceiveFrom(IAsyncResult iar){
try{
//Get the received message.
Socket recvSock = (Socket)iar.AsyncState;
EndPoint clientEP …Run Code Online (Sandbox Code Playgroud) 在我的Android应用程序中,我想读取音频流并解析其shoutcast元数据.我在NPR项目中使用StreamProxy工作:http: //code.google.com/p/npr-android-app/source/browse/Npr/src/org/npr/android/news/StreamProxy.java? R = e4984187f45c39a54ea6c88f71197762dbe10e72
问题是它不能通过数据连接工作.NPR也遇到了同样的问题.
总而言之,此代理作为本地Web服务器运行,在应用程序和远程流之间建立接口.它使用生成的自定义端口在localhost 127.0.0.1上运行.当服务器正在读取流数据时,它会将其复制到应用程序,因为您可以在StreamProxy中的第223行读取:
client.getOutputStream().write(buff, 0, readBytes);
Run Code Online (Sandbox Code Playgroud)
但是,该行通过数据连接产生此异常:
java.net.SocketException: sendto failed: EPIPE (Broken pipe)
Run Code Online (Sandbox Code Playgroud)
我不是套接字和流媒体方面的专家.在这种情况下,我没有得到wifi和数据连接之间的区别.
尝试连接 LDAP 时出现套接字异常。这是我的示例代码。我在 java 8 中看到了这个问题。我从未在早期的 java 版本中观察到这个问题。
public static DirContext getDirectoryContext() throws NamingException {
Properties env = new Properties();
env.put(Context.INITIAL_CONTEXT_FACTORY,
Common.getProperty("ldap.context.factory"));
env.put(Context.PROVIDER_URL,
Common.getProperty("ldap.provider.url"));
env.put(Context.SECURITY_AUTHENTICATION,
Common.getProperty("ldap.security.authentication"));
env.put(Context.SECURITY_PRINCIPAL,
Common.getProperty("ldap.security.principal"));
env.put(Context.SECURITY_CREDENTIALS,
Common.getProperty("ldap.security.credential"));
context = new InitialDirContext(env);
log.debug("NamingContext Initialized");
return context;
}
context = getDirectoryContext();
Run Code Online (Sandbox Code Playgroud)
我对所有 LDAP 调用使用相同的上下文。
private NamingEnumeration getResultsFromLdap(String searchFilter) {
NamingEnumeration results = null;
try {
// Getting the list from LDAP matching the given filter
SearchControls sControls = new SearchControls();
sControls.setSearchScope(SearchControls.SUBTREE_SCOPE);
String baseContext = Common.getProperty("ldap.base.context");
results = context.search(baseContext, searchFilter, …Run Code Online (Sandbox Code Playgroud) 我正在开发一个我正在上课的游戏,它完成了大约99%.但是,我意识到存在一个问题:如果服务器和客户端断开连接,并且客户端尝试重新连接(并且服务器已备份且运行正常),则客户端不会建立新连接.
服务器和客户端都是多线程的.如果我向客户端发送Kick消息,会发生什么是客户端将关闭其套接字.如果我重新连接,我得到一个SocketException:socket关闭,即使每次按下连接时,都会构造一个新的Client,它基本上只是一个创建套接字并使用get/send线程连接到服务器的类.
你觉得我有什么不合逻辑吗?它就像它试图使用构造的旧Socket和Client,但是如果我做了一些println调用,我可以看到它确实构造了一个新的并且它们位于不同的内存位置.
谢谢!
我将客户端的Object发送给服务器,在服务器端修改该对象并将其重新发送到客户端.从客户端发送对象到服务器很好,它正常工作,但当我发送对象时,它给出异常Socket关闭.这是代码.IntString和ParentObj是我正在发送对象的类.
Client1类:
import java.net.*;
import java.io.*;
public class Client1 {
public static void main(String args[]) {
int arr[] = new int[10];
int length = 6, i, counter_1;
ParentObj obj1;
for (i = 0; i < length; i++) {
arr[i] = i + 10;
}
try {
Socket s = new Socket("localhost", 6789);
IntString obj = new IntString(arr, length);
/*
* OutputStream os = s.getOutputStream();
* ObjectOutputStream oos = new ObjectOutputStream(os);
*
* oos.writeObject(obj);
* oos.close();
* os.close();
*/
Send_recv snd …Run Code Online (Sandbox Code Playgroud) 我已经看到很多关于处理套接字而没有对象处理异常的问题,所以我决定对它进行一次破解,看看是否可以完成.以下是我的发现.
您有一段代码使用来自System.Net.Sockets的Socket作为服务器套接字.问题是你想要关闭套接字,每次尝试时都会遇到ObjectDisposedException.
您的代码可能如下所示:
private static ManualResetEvent allDone = new ManualResetEvent(false);
private Socket ear;
public void Start()
{
new Thread(() => StartListening()).Start();
}
private void StartListening()
{
IP = Dns.GetHostAddresses(Dns.GetHostName()).Where(address => address.AddressFamily == AddressFamily.InterNetwork).First();
port = 11221;
IPEndPoint localEndPoint = new IPEndPoint(IP, Port);
ear = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
ear.Bind(localEndPoint);
ear.Listen(100);
try
{
while (true)
{
allDone.Reset();
ear.BeginAccept(new AsyncCallback(AcceptCallback), ear);
allDone.WaitOne();
}
}
catch (ObjectDisposedException e)
{
Console.WriteLine("Socket Closed");
}
}
private void AcceptCallback(IAsyncResult ar)
{
allDone.Set();
Socket listener = …Run Code Online (Sandbox Code Playgroud) 我有一个小的WebApp,它由一个小表单组成,用户必须输入一些凭据,并将其发布到我的瓶子应用程序.输入得到验证(通过一堆sql调用),如果一切正常,他会收到一封邮件.问题是:运行服务器后一切都很好单元我得到这样的错误:
Traceback (most recent call last): File "/usr/local/lib/python2.7/SocketServer.py", line 295, in
_handle_request_noblock
self.process_request(request, client_address) File "/usr/local/lib/python2.7/SocketServer.py", line 321, in process_request
self.finish_request(request, client_address) File "/usr/local/lib/python2.7/SocketServer.py", line 334, in finish_request
self.RequestHandlerClass(request, client_address, self) File "/usr/local/lib/python2.7/SocketServer.py", line 651, in __init__
self.finish() File "/usr/local/lib/python2.7/SocketServer.py", line 710, in finish
self.wfile.close() File "/usr/local/lib/python2.7/socket.py", line 279, in close
self.flush() File "/usr/local/lib/python2.7/socket.py", line 303, in flush
self._sock.sendall(view[write_offset:write_offset+buffer_size])
error: [Errno 32] Broken pipe
Run Code Online (Sandbox Code Playgroud)
我GET和POST侧面的功能看起来像这样:
@get("/myroute")
def show_form():
form = CreateVoucherForm()
return generate_form(form)
@post("/myroute")
def validate_storno(): …Run Code Online (Sandbox Code Playgroud) 我在不同的手机上有这些例外,一些正确的方法来处理它们。
IOS:
安卓:
我有一个应用程序将图像上传到服务时超时,我认为这会产生这些异常,尽管我确实有一个 try 块,但在生产中仍然会生成异常。谢谢你的帮助
今天我们将 Flutter 项目升级到了 3.13.0。项目在 iOS 上运行良好。在 Android 上,我在 VS 代码中调试项目时遇到了这个未捕获的异常。该网址有效。有趣的是,该应用程序似乎确实能够正常访问这些 URL 和功能。但不确定为什么我们突然遇到这个异常。
SocketException (SocketException: Failed host lookup: '*************' (OS Error: No address associated with hostname, errno = 7))
尝试将我们的版本降级回 3.10.1,问题就消失了。然而,我们的构建管道在此版本上开始失败。我们用于intl本地化,需要更新我们的管道才能运行。最终,尝试了解如何解决 SocketException 并继续使用最新版本的 Flutter。