gpt4 book ai didi

c# - 连接到 C# 服务器的 C++ 套接字

转载 作者:行者123 更新时间:2023-11-28 08:12:23 29 4
gpt4 key购买 nike

我有一个使用 TcpClient 和流用 C# 编写的服务器。当我尝试连接到它并使用 C++ 应用程序使用 Winsock 接收数据时,我可以接收数据,但它有数据,但它也显示了一堆其他数据。顺便说一下,我正在打印以使用 cout 缓冲到控制台。

如果您想查看服务器应发送的内容,请转到 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;
}

最佳答案

只打印接收到的字符,而不是整个缓冲区。 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());

关于c# - 连接到 C# 服务器的 C++ 套接字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8686680/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com