这是在linux上顺便说一句...
我从我的代码中收到此错误
m-server.cpp: In function âvoid* SocketHandler(void*)â:
m-server.cpp:187: error: invalid conversion from âcharâ to âconst void*â
m-server.cpp:187: error: initializing argument 2 of âssize_t send(int, const void*, size_t, int)â
给出错误的部分:
char welcomemsg;
cout << "Set the welcome message: ";
cin >> welcomemsg;
//send initial welcome data || msg & src
send(*csock, welcomemsg, sizeof(welcomemsg), 0); //this is line 187
更新
我改变了主意,而不是 cIN 然后 msg,我只想像下面的代码一样设置它。
我从我的代码中收到此错误
m-server.cpp: In function âvoid* SocketHandler(void*)â:
m-server.cpp:181: error: invalid conversion from âconst char*â to âcharâ
m-server.cpp:230: error: jump to label âFINISHâ
m-server.cpp:177: error: from here
m-server.cpp:181: error: crosses initialization of âchar welcomemsgâ
m-server.cpp:230: error: jump to label âFINISHâ
m-server.cpp:161: error: from here
m-server.cpp:181: error: crosses initialization of âchar welcomemsgâ
m-server.cpp:230: error: jump to label âFINISHâ
m-server.cpp:149: error: from here
m-server.cpp:181: error: crosses initialization of âchar welcomemsgâ
给出错误的部分:
char welcomemsg = "@hello_! bro?"; //can allow symbols?
//send initial welcome data || msg & src
send(*csock, (void *)&welcomemsg, sizeof(welcomemsg), 0);
我是 C++ 新手,这里有什么问题?怎么修?其背后的原因是什么?
更新
我将其更改为此服务器后,服务器没有任何错误。
char *welcomemsg = "hello";
//send initial welcome data || msg & src
send(*csock, welcomemsg, sizeof(welcomemsg), 0);
但是现在客户端在收到欢迎消息时崩溃了。为什么?
这是我与服务器一起使用的代码,客户端正在阅读欢迎消息。
send(*csock, "Hello_Word", 10, 0); //was a working code
更新
我已经检查并且客户端在 recv() 上崩溃了
此代码与所有建议有什么区别?为什么这段代码在没有崩溃客户端的情况下运行良好?
send(*csock, "Temporarily Offline_Server will open sockets on Jan. 14, 2013", 61, 0); //was a working code
更新
我的 recv() 代码:
response = "";
resp_leng= BUFFERSIZE;
while (resp_leng == BUFFERSIZE)
{
resp_leng= recv(sock, (char*)&buffer, BUFFERSIZE, 0);
if (resp_leng>0)
response+= string(buffer).substr(0,resp_leng);
}