字符串"G"未显示 [英] String 'G' is not showing
问题描述
这是client.cpp文件.所以有什么问题?我声明此字符串为'G',然后输入昵称,然后在此处'cout<<G<<:"<<sbuffer.Message<
P.S .:我不需要发布server.cpp文件,对吗?
So this is the client.cpp file. So what's the problem? I declare this string 'G', I enter the nickname and then here ' cout << G << ":" << sbuffer.Message <
P.S.: I don't think I need to post the server.cpp file, do I?
#pragma comment(lib, "Ws2_32.lib")
#include <WinSock2.h>
#include <Windows.h>
#include <iostream>
#include <string>
using namespace std;
SOCKADDR_IN addr;
SOCKET sConnect;
string G;
struct Buffer
{
int ID;
char Message[256];
};
int ClientThread()
{缓冲区缓冲;
char buffer[sizeof(sbuffer)] = {0};
for(;; Sleep(10))
{
if(recv(sConnect, buffer, sizeof(sbuffer), NULL))
{
memcpy(&sbuffer, buffer, sizeof(sbuffer));
cout << G << ":" << sbuffer.Message <<endl;
}
}
return 0;
}
int main()
{system("cls");
{ system("cls");
int RetVal = 0;
WSAData wsaData;
WORD DllVersion = MAKEWORD(2,1);
RetVal = WSAStartup(DllVersion, &wsaData);
if(RetVal != 0)
{
MessageBoxA(NULL, "Winsock startup failed", "Error", MB_OK | MB_ICONERROR);
exit(1);
}
sConnect = socket(AF_INET, SOCK_STREAM, NULL);
addr.sin_addr.s_addr = inet_addr("127.0.0.1");
addr.sin_port = htons(1234);
addr.sin_family = AF_INET;
cout << "Connect to Masterserver? [ENTER]" <<endl;
getchar();
RetVal = connect(sConnect, (SOCKADDR*)&addr, sizeof(addr));
if(RetVal != 0)
{
MessageBoxA(NULL, "Could not connect to server", "Error", MB_OK | MB_ICONERROR);
main();
}
else
{
string G;
cout << "Nickname: " << endl;
cin >> G;
CreateThread(NULL, NULL, (LPTHREAD_START_ROUTINE) ClientThread, NULL, NULL, NULL);
for(;; Sleep(10))
{
char* buffer = new char[256];
ZeroMemory(buffer, 256);
cin >> buffer;
getchar();
send(sConnect, buffer, 256, NULL);
}
}
return 0;
}
推荐答案
您在全局范围顶部声明的字符串G
被字符串G
遮盖了您在 main
范围内声明的内容,因此 ClientThread
方法无法使用您读入的内容.删除主作用域中的声明.
The string G
you declare in the global scope at the top is over shadowed by the string G
that you declare in the main
scope and thus the one you read into isn't available to the ClientThread
method. Delete the declaration in the main scope.
这篇关于字符串"G"未显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!