Visual C++: Socket Anfängerfrage
-
hi,
ich habe erst vor ein paar Tagen mit Visual C++ angefangen und wollte jetzt mal eine Verbindung zu einem IRC-Server mit Hilfe der Socket-Funktionen herstellen.
Leider scheint in der Verbindungs-Methode irgendwas schie zu laufen, jedenfalls hängt sich das Programm beim Ausführen der Methode auf.
Ich habe die Methode aus der VC++ Hilfe entnommen und etwas angepasst.
this->Server enthällt den Server-Host, z.B. de.quakenet.org
this->Port ist der Port, z.B. 6667Hier die Methode:
void CConn::connect() { Socket^ s = nullptr; IPHostEntry^ hostEntry = nullptr; // Get host related information. hostEntry = Dns::Resolve( this->Server ); // Loop through the AddressList to obtain the supported AddressFamily. This is to avoid // an exception that occurs when the host IP Address is not compatible with the address family // (typical in the IPv6 case). IEnumerator^ myEnum = hostEntry->AddressList->GetEnumerator(); while ( myEnum->MoveNext() ) { IPAddress^ address = safe_cast<IPAddress^>(myEnum->Current); IPEndPoint^ endPoint = gcnew IPEndPoint( address,this->Port ); Socket^ tmpS = gcnew Socket( endPoint->AddressFamily,SocketType::Stream,ProtocolType::Tcp ); tmpS->Connect( endPoint ); if ( tmpS->Connected ) { s = tmpS; break; } else { continue; } } this->sock = s; }Vielleicht findet ja jemand den Fehler.
Gruß
-
Hi,
Lies die Dokumentation. Da steht, dass Socket::Connect(..) per default blockierend ist. Das kann jedoch umgestellt werden.
http://msdn2.microsoft.com/en-us/library/ych8bz3x.aspxGruss Simon
EDIT: Ausserdem ist das das falsche Forum, das richtige wäre C++/CLI
-
Vielen Dank.
Leider weis ich immernoch nicht genau wie ich die Blokade aufheben kann.
Gruß
-
Warum nicht?
Hast Du die Dokumentation gelesen? Dort steht es nämlich.
Simon
-
Naja ich habs mit
tmpS->Blocking = false;probiert, aber beim aufrufen der Methode gibts nen Ausnahmefehler und das Programm stürtz ab.
-
The Connect method will block, unless you specifically set the Blocking property to false prior to calling Connect. If you are using a connection-oriented protocol like TCP and you do disable blocking, Connect will throw a SocketException because it needs time to make the connection. Connectionless protocols will not throw an exception because they simply establish a default remote host. You can use SocketException..::.ErrorCode to obtain the specific error code. After you have obtained this code, refer to the Windows Sockets version 2 API error code documentation in the MSDN library for a detailed description of the error. If the error returned WSAEWOULDBLOCK, the remote host connection has been initiated by a connection-oriented Socket, but has not yet completed successfully. Use the Poll method to determine when the Socket is finished connecting.
Exception fangen, testen obs WSAEWOULDBLOCK error ist.