2011-12-31 17 views
0

私はTcpClientとストリームを使ってC#で書かれたサーバを持っています。 Winsockを使用してC++アプリケーションでデータに接続しようとすると、データは受信できますが、データはありますが、他のデータも表示されます。ところで、私はcoutを使ってコンソールにバッファリングするように印刷しています。C#ソケットとC#サーバの接続

サーバーが送信する内容を確認する場合は、http://onenetworks.us:12345にアクセスしてください。サーバーから文字列が送信されます。私にとっては、 "16READY"を送信します。これは私のクライアントに読ませようとしているものです。

What is displayed when you connect.

ここに私のコードは、私は、MSDNのページのオフ作業コードファイルをコピーしました。

#define WIN32_LEAN_AND_MEAN 

#include <winsock2.h> 
#include <Ws2tcpip.h> 
#include <stdio.h> 
#include <iostream> 
// 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), &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*) &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 << recvbuf <<std::endl; 
     if (iResult > 0) 
      printf("Bytes received: %d\n", iResult); 
     else if (iResult == 0) 
      printf("Connection closed\n"); 
     else 
      printf("recv failed: %d\n", WSAGetLastError()); 

    } while(iResult > 0); 

    // cleanup 
    closesocket(ConnectSocket); 
    WSACleanup(); 
    std::cin.ignore(); 
    //return 0; 
} 
+0

あなたのCOUT(ここでは 'のstd :: coutの<< recvbuf <<はstd :: ENDLは;')あなたのメッセージがあるため、特定の長さだけを出力する必要がありますおそらくヌル終了しません。 –

+0

どうすればいいですか? – tr0yspradling

+0

これはおそらく助けになるかもしれません:http://cboard.cprogramming.com/cplusplus-programming/74737-cout-fixed-length-string.html –

答えて

0

バッファの長さはDEFAULT_BUFLENに設定されているため、ソケットを介して受信するものはその長さになります。値0(または '\ 0')が見つかるまで、すべての文字をループすることができます。

+0

メッセージ変数はnullで終了することは保証されていません。これはどのように役立ちますか? –

+0

あなたのsendbufの使い方によって異なります。例えば、c#stringはnullで終了しますが、C++ char *は一般的にはNULLではありません。しかし、もしあなたがchar * sendbuf = "some text"のようなものを使用するならば。ストリームで埋めると、nullで終了します。 Simon Mourierの回答は、sendbuf charをcharで埋めた場合に当てはまります。受信側では、sendbufのサイズや終了位置を知りません。 – Bijan

0

あなたはこのような何かを行うことができます:

... 
iResult = recv(ConnectSocket, recvbuf, recvbuflen, 0); 
recvbuf[iResult] = 0; // add this to zero terminate the receive buffer 
std::cout << recvbuf <<std::endl; 
... 

は文字のみが受信

1

印刷ではなく、掲載バッファ全体(もちろん、それはiResultがDEFAULT_BUFLENよりも少ないであると想定します)。 iResultは、受信したデータの長さが含まれています:

iResult = recv(ConnectSocket, recvbuf, recvbuflen, 0); 
    if (iResult > 0) 
    { 
     printf("Bytes received: %d\n", iResult); 
     std::cout << std::string(recvbuf, recvbuf+iResult) <<std::endl; 

    } 
    else if (iResult == 0) 
     printf("Connection closed\n"); 
    else 
     printf("recv failed: %d\n", WSAGetLastError()); 
関連する問題