[英]C++ Client socket receives only first letter of message
在過去的幾天里,我一直被困在一個問題上。 我需要在 C++ 中設計一個音樂流媒體應用程序。 但是,我發現很難發送從目錄中讀取文件時獲得的文件名。 文件名是“sample.wav”和“sample1.wav”,但客戶端每次都會收到 output 只是字母 S。 雖然如果我手動發送從服務器分配給變量的字符串消息,客戶端會收到它只是找到。 你能為我指出正確的方向嗎?
提前致謝。
服務器端
sockaddr_in hint;
hint.sin_family = AF_INET;
hint.sin_port = htons(54000);
hint.sin_addr.S_un.S_addr = INADDR_ANY;
// for local ip: inet_pton
bind(listening, (sockaddr*)&hint, sizeof(hint));
// max number of open connections (SOMAXCONN)
listen(listening, SOMAXCONN);
// waiting for connection
sockaddr_in client;
int clientSize = sizeof(client);
SOCKET clientSocket = accept(listening, (sockaddr*)&client, &clientSize);
SOCKET* client_ptr = &clientSocket;
char host[NI_MAXHOST];
// client's remote name
char service[NI_MAXSERV];
// the port on which the client connects
ZeroMemory(host, NI_MAXHOST);
//ZeroMemory(service, NI_MAXHOST);
// cleaning memory
sockList.push_back(&clientSocket);
if (getnameinfo((sockaddr*)&client, sizeof(client), host, NI_MAXHOST, service, NI_MAXSERV, 0) == 0)
{
for (int i = 0; i < 2; i++) {
int postList = send(clientSocket, mainList.GetSongName(i).c_str(), mainList.GetSongName(i).size() + 1,0);
}
}
else {
inet_ntop(AF_INET, &client.sin_addr, host, NI_MAXHOST);
cout << host << " connected on port " << ntohs(client.sin_port) << endl;
}
closesocket(listening);
}
客戶端:
string ipAddress = "127.0.0.1";
int port = 54000;
WSAData data;
WORD ver = MAKEWORD(2, 2);
int wsResult = WSAStartup(ver, &data);
if (wsResult != 0) {
cerr << "Can't start Winsock, Err #:" << wsResult << endl;
return 0;
}
SOCKET sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock == INVALID_SOCKET) {
cerr << "Can't create socket. Err #:" << WSAGetLastError << endl;
return 0;
}
sockaddr_in hint;
hint.sin_family = AF_INET;
hint.sin_port = htons(port);
inet_pton(AF_INET, ipAddress.c_str(), &hint.sin_addr);
//hint.sin_addr.S_un.S_addr = INADDR_ANY;
int connResult = connect(sock, (sockaddr*)&hint, sizeof(hint));
bind(sock, (sockaddr*)&hint, sizeof(hint));
if (connResult == SOCKET_ERROR) {
cerr << "Can't connect to Server, Err #:" << WSAGetLastError() << endl;
closesocket(sock);
WSACleanup();
}
char buffer[4096];
string userInput;
ZeroMemory(buffer, 4096);
int msglen;
int numbytes = 0;
int welcomemsg = recv(sock, buffer, sizeof(buffer), 0);
cout << sizeof(buffer) << endl;
cout << "Server >>" << string(buffer, 0, welcomemsg) << endl;
do {
welcomemsg = recv(sock, buffer, 4096, 0);
if (welcomemsg > 0) {
cout << "Server >>" << string(buffer, 0, welcomemsg) << endl;
}
else {
cerr << "Client disconnected" << endl;
}
} while (welcomemsg > 0);
songList object 構造函數:
wchar_t* w_Path = (wchar_t*)malloc(strlen(filePath) * sizeof(wchar_t));
mbstowcs(w_Path, filePath, strlen(filePath) + 1);
HANDLE hFind;
WIN32_FIND_DATA data;
LPCWSTR m_Path = w_Path;
memset(&data, 0, sizeof(WIN32_FIND_DATA));
hFind = FindFirstFile(m_Path, &data);
if (hFind != INVALID_HANDLE_VALUE) {
int i = 0;
do {
printf("\n %S", data.cFileName);
songNames[i] = (char*)data.cFileName;
i++;
} while (FindNextFile(hFind, &data));
FindClose(hFind);
}
else {
cout << "No songs found in directory" << endl;
}
您對字符串構造函數的使用不正確
cout << "Server >>" << string(buffer, 0, welcomemsg) << endl;
應該
cout << "Server >>" << string(buffer, welcomemsg) << endl;
問題是你在 Unicode 模式下工作,結構WIN32_FIND_DATA
是typedef
d to WIN32_FIND_DATAW
,所以songNames[i] = (char*)data.cFileName;
將wchar_t
字符串重新解釋為單字節字符。
被視為多字節,一個whar_t
字符的字符串看起來像一個以空字符結尾的 1 個字符的字符串。
作為一個快速和骯臟的修復,將您的服務器項目模式更改為多字節(注意 - 代碼中可能存在更多錯誤,因此可能需要更多修復才能使其正常工作)。
但更好的是,繼續使用 Unicode 模式並調整代碼以使用wchar_t
字符而不是char
。 這意味着將char buffer[]
更改為wchar_t buffer[]
並在必要時調整大小值。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.