【发布时间】:2020-07-09 15:10:19
【问题描述】:
我正在使用 WinSock2 在 C++ (Visual Studio 2017) 中编写客户端-服务器应用程序。我看了 YouTube 教程,做了一个服务器和客户端。启动时,连接已建立。 但是来自服务器的消息并没有传输到客户端。
这是我的服务器。
//Server
#pragma comment(lib, "ws2_32.lib")
#define _WINSOCK_DEPRECATED_NO_WARNINGS
#include <WinSock2.h>
#include <iostream>
int main() {
//WinSock Startup
WSAData wsaData;
WORD DllVersion = MAKEWORD(2, 1);
if (WSAStartup(DllVersion, &wsaData) != 0) { //If WSAStartup returns anything ither than 0, than that means an error has occured in the WinSock Startup
MessageBoxA(NULL, "WinSock startup failed", "Error", MB_OK | MB_ICONERROR);
exit(1);
}
SOCKADDR_IN addr; //Address tha the will bind our listening socket to
int addrlen = sizeof(addr); //lenght of the address (required for accept call)
addr.sin_addr.s_addr = inet_addr("127.0.0.1"); //Broadcast locally
addr.sin_port = htons(1111); //Port
addr.sin_family = AF_INET; //IPv4 Socket
SOCKET sListen = socket(AF_INET, SOCK_STREAM, NULL); //Create socket to listen for new connections
bind(sListen, (SOCKADDR*)&addr, sizeof(addr)); //Bind the address to the socket
listen(sListen, SOMAXCONN); //Places sListen socket in a state in which is in listening for an incoming connection. Note: SOMAXCONN = Socket Outstanding Max Connections
SOCKET newConnection; // Socket to hold the client's connection
newConnection = accept(sListen, (SOCKADDR*)&addr, &addrlen); //Accept a new connection
if (newConnection == 0) { //If accepting the client's connection failed
std::cout << "Failed to accept the client's connection" << std::endl;
}
else { //If client connection properly accepted
std::cout << "Client connected!" << std::endl;
char MOID[256] = "Welcome! This is a Message of the Day."; //Create buffer with message of the day
send(newConnection, MOID, sizeof(MOID), NULL); //Send MOID buffer
}
system("pause");
return 0;
}
这里就是这样一个客户。
//Client
#pragma comment (lib, "ws2_32.lib")
#define _WINSOCK_DEPRECATED_NO_WARNINGS
#include <WinSock2.h>
#include <iostream>
int main() {
//WinSock Startup
WSAData wsaData;
WORD DllVersion = MAKEWORD(2, 1);
if (WSAStartup(DllVersion, &wsaData) != 0) { //If WSAStartup returns anything ither than 0, than that means an error has occured in the WinSock Startup
MessageBoxA(NULL, "WinSock startup failed", "Error", MB_OK | MB_ICONERROR);
exit(1);
}
SOCKADDR_IN addr; //Address to be binded to our Connection socket
int sizeofaddr = sizeof(addr); //Need sizeofaddr for the connect function
addr.sin_addr.s_addr = inet_addr("127.0.0.1"); //Address = localhoct (this pc)
addr.sin_port = htons(1111); //Port = 1111
addr.sin_family = AF_INET; //IPv4 Socket
SOCKET Connection = socket(AF_INET, SOCK_STREAM, NULL); //Set connection socket
if (connect(Connection, (SOCKADDR*)&addr, sizeofaddr) != 0) { //If we are unable to connect...
MessageBoxA(NULL, "Failed to connect", "Error", MB_OK | MB_ICONERROR);
return 0; //Failed to connect
}
std::cout << "Connected!" << std::endl;
char MOID[256];
recv(Connection, MOID, sizeof(MOID), NULL); //Recieve Message of the day buffer into MOID array
std::cout << "MOID:" << std::endl;
system("pause");
return 0;
}
应发送此消息:“欢迎!这是今日消息。”请告诉我为什么消息没有传输,我该如何解决这个问题?
【问题讨论】:
-
会发生什么?
-
在客户端,我必须写
std::cout << "MOID:" << MOID << std::endl;而不是std::cout << "MOID:" << std::endl;,对不起。