我正在使用VC ++ 2012 Express创建一个多线程聊天客户端 - 服务器。代码改编自此处:http://www.codeproject.com/Articles/14032/Chat-Client-Server
服务器似乎工作正常。但是,某种程度上客户端无法启动线程来接收消息。调用时GetLastError()
函数返回8.
以下是客户端代码的片段:
#include "Chatter_Client.hpp"
CMessenger MessObj;
//... CMessenger::Init initializes Winsock, socket, and connection.
int CMessenger::RecMessage()
{
char buffRetData[4096];
int stat;
stat = recv(conn, buffRetData, 4096, 0);
if(stat == -1) {
cout << "Message not received!" << endl;
return 1; // gagal
} else {
cout << "-->" << buffRetData << "\n";
return 0; // looping lagi~
}
}
DWORD WINAPI MessageRecThread(LPVOID pParam)
{
for(;;) {
Sleep(50);
if(MessObj.RecMessage())
break;
}
return 0;
}
int main()
{
string buf;
DWORD RecThreadID;
string sServerAddress;
int iPort;
for(;;) {
cout << "Server address: ";
cin >> sServerAddress;
if (sServerAddress.size() == 0) {
cout << "No Address entered!" << endl;
} else break;
}
cout << "Server port: ";
cin >> iPort;
MessObj.Init(sServerAddress.c_str(), iPort);
if(!MessObj.IsConnected()) {
cout << "Connection error!";
_getch();
return -1;
}
Sleep(30);
HANDLE RecThread = CreateThread(NULL, 20000, MessageRecThread, NULL, 0, &RecThreadID);
if(RecThread == NULL) {
cout << "Listener thread cannot be created! ERROR CODE: " << GetLastError() << endl;
_getch();
return 1;
} else {
cout << "Yey masuk!" << endl;
}
for(;;) {
cin >> buf;
if(MessObj.SendMessage(buf)) {
cout << "Connection lost!" << endl;
break;
}
}
cout << "Terminating client...";
_getch();
return 0;
}
和标题:
#include <cstdio>
#include <winsock2.h>
#include <conio.h>
#include <iostream>
#include <string>
#include <windows.h>
#pragma comment(lib, "WS2_32.lib")
using namespace std;
class CMessenger
{
public:
CMessenger();
~CMessenger();
void Init(string iIP, int iPort);
int SendMessage(string sMessage);
int RecMessage();
bool IsConnected();
private:
bool ConnStatus;
string sIPAddress;
int sPort;
SOCKET conn;
};
我尝试使用C ++ 11的<thread>
,但是当调用线程类时,调试器立即调用abort()
。
任何线索?
PS:此代码编译。
这是新::Init
的摘要,其中包含一些修改内容:
void CMessenger::Init(const string& iAddress, const string& iPort)
{
// init winsock
WSAData wsData;
int stat = WSAStartup(MAKEWORD(2,0), &wsData);
if (stat != 0) {
cerr << "ERROR: WINCODE " << WSAGetLastError() << endl;
return;
}
// get server info
addrinfo hints, *res, *p_hints;
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
stat = getaddrinfo(iAddress.c_str(), iPort.c_str(), &hints, &res);
if(stat != 0) {
cerr << "ERROR: " << gai_strerror(stat) << endl;
return;
}
void *addr;
char ipstr[INET6_ADDRSTRLEN];
p_hints = res;
sockaddr_in *ip = (struct sockaddr_in*)p_hints->ai_addr;
addr = &(ip->sin_addr);
// convert to string and print it
inet_ntop(p_hints->ai_family, addr, ipstr, sizeof ipstr);
cout << "Server IP: " << ipstr << endl;
// init socket
conn = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
if (conn == INVALID_SOCKET) {
cout << "SOCKET ERROR\n";
return;
}
// connect to server
stat = connect(conn, res->ai_addr, res->ai_addrlen);
if(stat != 0) {
cerr << "ERROR: " << WSAGetLastError() << endl;
return;
}
// connect OK
sPort = atoi(iPort.c_str());
sAddress = iAddress;
cout << "Connection established." << endl << endl;
ConnStatus = true;
Sleep(30);
return;
}
仍有线程问题..
答案 0 :(得分:2)
刚试过你的代码,用存根替换丢失的方法,并且创建了没有任何问题的脚步。 确保你的MessObj :: Init()方法没有损害(内存损坏)。
还有一个补充:
CMessanger
{
...
void Init(string iIP, int iPort)
...
}
string sServerAddress;
...
MessObj.Init(sServerAddress.c_str(), iPort);
对于许多字符串对象副本和字符串 - &gt;这是不好的样式。 char * - &gt;字符串转换。 如果传递的字符串没有被复制&#39;在CMessenger :: Init()方法中,该方法应该更改为
void Init(const string& iIP, int iPort)
并将其命名为
MessObj.Init(sServerAddress, iPort);
如果字符串参数在方法中复制,那么您仍然按值传递参数,但在方法中使用可移动语义,如:
void Init(string iIP, int iPort)
{
m_ip = std::move(iIP);
}