简体   繁体   中英

Send a string with sockets in C++ (Winsock TCP/IP)

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

Here's the code of the client:

#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();

}

And here's the code of the server:

#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();

}

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

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.

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';

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

std::string receivedString{buffer, newData};

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

Instead of:

cout << newData << endl;

do:

cout << buffer << endl;

The recv function returns the number of bytes read and the buffer holds the bytes read.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM