在 C++ (Winsock TCP/IP) 中发送带有套接字的字符串 [英] Send a string with sockets in C++ (Winsock TCP/IP)

查看:27
本文介绍了在 C++ (Winsock TCP/IP) 中发送带有套接字的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将带有套接字的字符串从客户端发送到服务器.

I'd like to send a string with sockets from a client to a server.

客户端代码如下:

#include <iostream>
#include <winsock.h>
#include <unistd.h>

using namespace std;

int main()
{

//Load socket
WSADATA wsaData;
WSAStartup(0x0202, &wsaData);

//Create first socket
int thisSocket;
struct sockaddr_in destination;

destination.sin_family = AF_INET;
thisSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (thisSocket < 0)
{
    cout << "Socket Creation FAILED!" << endl;
    return 0;
}

//Connect to a host
destination.sin_port = htons(13374);
destination.sin_addr.s_addr = inet_addr("127.0.0.1");
if (connect(thisSocket,(struct sockaddr *)&destination,sizeof(destination))!=0){
    cout << "Socket Connection FAILED!" << endl;
    if (thisSocket) close(thisSocket);
    return 0;
}
cout << "Connected!" << endl;

//Send the socket
//char *buffer = "Hello, this is a socket!";
string buffer = "Hello, this is a socket!";
//send(thisSocket, buffer, (int)strlen(buffer), 0);
send(thisSocket, buffer.c_str(), buffer.length(), 0);

//Close the socket
closesocket(thisSocket);
WSACleanup();

}

这是服务器的代码:

#include <iostream>
#include <winsock.h>
#include <unistd.h>

using namespace std;

int main()
{

//Load the socket
WSADATA wsaData;
WSAStartup(0x0202, &wsaData);

//Create the first socket
int thisSocket;
struct sockaddr_in destination;

destination.sin_family = AF_INET;
thisSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (thisSocket < 0)
{
    cout << "Socket Creation FAILED!" << endl;
    return 0;
}

//Bind to a socket
destination.sin_port = htons (13374);
destination.sin_addr.s_addr = INADDR_ANY;
if (bind(thisSocket, (struct sockaddr *)&destination, sizeof(destination))     <0){
    cout << "Binding Socket FAILED!" << endl;
    if (thisSocket) close(thisSocket);
    return 0;
}

//Listen on a socket
cout << "Listening on 13374..." << endl;
if (listen(thisSocket, 5)<0){
    cout << "Listening on Socket FAILED!" << endl;
    if (thisSocket) close(thisSocket);
    return 0;
}

//Accept a connection
struct sockaddr_in clientAddress;
int clientSize = sizeof(clientAddress);
thisSocket= accept(thisSocket, (struct sockaddr *)&clientAddress, (int *) &clientSize);
if (thisSocket<0)
{
    cout << "Socket Connection FAILED!" << endl;
    if (thisSocket) close(thisSocket);
    return 0;
}
cout <<"Connection Established!" << endl;

//Receive the socket

char buffer[512];
int newData;
newData = recv(thisSocket, buffer, 512, 0);
cout << newData << endl;

//Close the socket
closesocket(thisSocket);
WSACleanup();

}

如您所见,服务器将收到数字24".我怎样才能得到真正的字符串呢?

As you can image, the server will receive the number "24". How can I get the real string instead?

推荐答案

recv 读取的数据最终在 buffer 中.recv 函数返回它接收到的字节数,如果连接关闭则返回零,如果出现错误则返回负值.

The data read by recv end up in buffer. The recv function returns how many bytes it received, zero if the connection was closed and a negative value if there was an error.

阅读recv 参考 会告诉你这一切以及更多.

Reading a recv reference would have told you all this and more.

请注意,数据不像字符串那样终止.要么将其作为字符串终止,如

Do note that the data is not terminated like a string. Either terminate it as a string like

buffer[newData] = '\0';

在检查 recv 函数确实收到了一些东西之后.或者你可以直接构造一个 std::string 对象:

after checking that the recv function actually received something. Or you could construct a std::string object directly:

std::string receivedString{buffer, newData};

也不要这样做,除非 recv 函数确实收到了一些东西.

Also don't do it unless the recv function actually received something.

这篇关于在 C++ (Winsock TCP/IP) 中发送带有套接字的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆