hallo,
ich habe neulich angefangen mit der Socket-Pragrammierung mit C++ in .Net.
hier sind meine Testcodes:
Server Code:
Client Code:
jedes Mal wenn ich clientSocket->Send(sendBuffer); ausführe, kann der server sofort akzeptieren, aber gleich danach bekomme ich die Fehlermeldung:
was mache ich da fasch?
vielen Dank für eure Hilfe.
viele Grüsse
Marek
ich habe neulich angefangen mit der Socket-Pragrammierung mit C++ in .Net.
hier sind meine Testcodes:
Server Code:
Code:
IPEndPoint ^ localEndpoint; int port = 20000; Socket^ listenSocket; localEndpoint = gcnew IPEndPoint(IPAddress::Any, port); listenSocket = gcnew Socket(localEndpoint->AddressFamily, SocketType::Stream, ProtocolType::Tcp); listenSocket->Bind(localEndpoint); // start the server with a listen backlog of 100 connections listenSocket->Listen(100); // post accepts on the listening socket Console::WriteLine("Press any key to terminate the server process...."); array<Byte>^ msg = Encoding::UTF8->GetBytes( "This is a test" ); array<Byte>^ msgbufferRecv = Encoding::UTF8->GetBytes( " test" ); array<Byte>^ bytes = gcnew array<Byte>(256); listenSocket->Accept(); try { int byteCount = listenSocket->Receive( bytes ); if ( byteCount > 0 ) { Console::WriteLine( System::Text::Encoding::UTF8->GetString( bytes ) ); } } catch ( SocketException^ e ) { Console::WriteLine( "{0} Error code: {1}.", e->Message, e->ErrorCode.ToString() ); } int byteCount = listenSocket->Send( msg ); Console::WriteLine( "Sent {0} bytes.", byteCount.ToString() ); Console::ReadKey();
Code:
// Create a socket corresponding to the address family of the resolved address tmpS= gcnew Socket(addr->AddressFamily, sockType, sockProtocol); Console::WriteLine("Client: Socket() is OK..."); try { // Create the endpoint that describes the destination Console::WriteLine("Client: Creating the destination endpoint..."); destination = gcnew IPEndPoint(addr, remotePort); tmpS->Connect(destination); if ( tmpS->Connected ) { clientSocket = tmpS; Console::WriteLine("Client: Connect() is OK..."); break; } else { continue; } } catch (SocketException^ err) { // Connect failed so close the socket and try the next address if (clientSocket !=nullptr) clientSocket->Close(); Console::WriteLine("Client: Client socket closed. Error code: " + err->ErrorCode); Console::WriteLine("Client: " + err->Message); clientSocket = nullptr; continue; } } if ((clientSocket != nullptr) && (destination != nullptr)) { rc = clientSocket->Send(sendBuffer); Console::WriteLine("Client: TCP - send() is OK..."); Console::WriteLine("Client: Sent request of {0} bytes", rc); // For TCP, shutdown sending on our side since the client won't send any more data if (sockProtocol == ProtocolType::Tcp) { clientSocket->Shutdown(SocketShutdown::Send); Console::WriteLine("Client: Shutdown() is OK..."); } }
Eine nicht behandelte Ausnahme des Typs "System.Net.Sockets.SocketException" ist in System.dll aufgetreten.
Zusätzliche Informationen: Eine Anforderung zum Senden oder Empfangen von Daten wurde verhindert, da der Socket nicht verbunden ist und (beim Senden über einen Datagrammsocket mit einem sendto-Aufruf) keine Adresse angegeben wurde
Zusätzliche Informationen: Eine Anforderung zum Senden oder Empfangen von Daten wurde verhindert, da der Socket nicht verbunden ist und (beim Senden über einen Datagrammsocket mit einem sendto-Aufruf) keine Adresse angegeben wurde
vielen Dank für eure Hilfe.
viele Grüsse
Marek
Comment