Note that there are some explanatory texts on larger screens.

plurals
  1. POC++ sockets connecting to C# server
    primarykey
    data
    text
    <p>I have a server written in C# using a TcpClient and streams. When I try to connect to it and receive data with a C++ application using Winsock, I can receive the data but it has the data but it also displays a bunch of other data. By the way I'm printing to buffer to the console using cout. </p> <p>If you want to see what the server is supposed to send, go to <a href="http://onenetworks.us:12345" rel="nofollow noreferrer">http://onenetworks.us:12345</a>. There the server will send you a string. For me it sends "16READY" Which is what I'm trying to get my client to only read.</p> <p><img src="https://i.stack.imgur.com/VlWe6.png" alt="What is displayed when you connect."></p> <p>Here's my code, I copied a working code file off of the MSDN page.</p> <pre><code> #define WIN32_LEAN_AND_MEAN #include &lt;winsock2.h&gt; #include &lt;Ws2tcpip.h&gt; #include &lt;stdio.h&gt; #include &lt;iostream&gt; // Link with ws2_32.lib #pragma comment(lib, "Ws2_32.lib") #define DEFAULT_BUFLEN 512 #define DEFAULT_PORT "12345" int main() { //---------------------- // Declare and initialize variables. WSADATA wsaData; int iResult; SOCKET ConnectSocket = INVALID_SOCKET; struct sockaddr_in clientService; char *sendbuf = ""; char recvbuf[DEFAULT_BUFLEN]; int recvbuflen = DEFAULT_BUFLEN; //---------------------- // Initialize Winsock iResult = WSAStartup(MAKEWORD(2,2), &amp;wsaData); if (iResult != NO_ERROR) { printf("WSAStartup failed: %d\n", iResult); return 1; } //---------------------- // Create a SOCKET for connecting to server ConnectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if (ConnectSocket == INVALID_SOCKET) { printf("Error at socket(): %ld\n", WSAGetLastError() ); WSACleanup(); return 1; } //---------------------- // The sockaddr_in structure specifies the address family, // IP address, and port of the server to be connected to. clientService.sin_family = AF_INET; clientService.sin_addr.s_addr = inet_addr( "199.168.139.14" ); clientService.sin_port = htons( 12345 ); //---------------------- // Connect to server. iResult = connect( ConnectSocket, (SOCKADDR*) &amp;clientService, sizeof(clientService) ); if ( iResult == SOCKET_ERROR) { closesocket (ConnectSocket); printf("Unable to connect to server: %ld\n", WSAGetLastError()); WSACleanup(); return 1; } // Send an initial buffer iResult = send( ConnectSocket, sendbuf, (int)strlen(sendbuf), 0 ); if (iResult == SOCKET_ERROR) { printf("send failed: %d\n", WSAGetLastError()); closesocket(ConnectSocket); WSACleanup(); return 1; } printf("Bytes Sent: %ld\n", iResult); // shutdown the connection since no more data will be sent iResult = shutdown(ConnectSocket, SD_SEND); if (iResult == SOCKET_ERROR) { printf("shutdown failed: %d\n", WSAGetLastError()); closesocket(ConnectSocket); WSACleanup(); return 1; } // Receive until the peer closes the connection do { iResult = recv(ConnectSocket, recvbuf, recvbuflen, 0); std::cout &lt;&lt; recvbuf &lt;&lt;std::endl; if ( iResult &gt; 0 ) printf("Bytes received: %d\n", iResult); else if ( iResult == 0 ) printf("Connection closed\n"); else printf("recv failed: %d\n", WSAGetLastError()); } while(iResult &gt; 0); // cleanup closesocket(ConnectSocket); WSACleanup(); std::cin.ignore(); //return 0; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload