我正试图在局域网上的程序之间建立一个简单的聊天程序。我的问题是我可以轻松地连接并从服务器发送消息到客户端,但不是相反。我试图让它像这样工作。服务器< - >客户端在两个程序之间不断发送和接收,显然这将需要一个循环连续输入,但我不太确定如何去做。这是服务器和客户端的代码。服务器 - >客户端之间的C++ Winsock通信,反之亦然
//THIS IS THE SERVER
int main(int argc, char *argv[])
{
WSADATA wsaData;
int starterr = WSAStartup(MAKEWORD(2,2), &wsaData);
if (starterr != 0)
{
cout << "WSADATA Failed to startup!" << endl;
cout << "Error Code: " << WSAGetLastError() << endl;
system("pause >nul");
WSACleanup();
return 0;
}
cout << "WSADATA Startup Successful!" << endl;
SOCKET mysock = socket(AF_INET, SOCK_STREAM, 0);
if (mysock == INVALID_SOCKET)
{
cout << "Socket Creation Failed!" << endl;
cout << "Error Code: " << WSAGetLastError() << endl;
system("pause >nul");
WSACleanup();
return 0;
}
cout << "Socket Creation Successful!" << endl;
sockaddr_in sin;
sin.sin_port = htons(80);
sin.sin_addr.s_addr = INADDR_ANY;
sin.sin_family = AF_INET;
if (bind(mysock,(sockaddr*)&sin, sizeof(sin)) == SOCKET_ERROR)
{
cout << "Socket failed to bind!" << endl;
cout << "Error Code: " << WSAGetLastError() << endl;
system("pause >nul");
WSACleanup();
return 0;
}
cout << "Socket Binded Successfuly!" << endl;
//Listen to the socket until successful on receiving input
while (listen(mysock, SOMAXCONN) == SOCKET_ERROR);
SOCKET client;
int lin = sizeof(sin);
client = accept(mysock,(sockaddr*) &sin, &lin);
cout << "Connection Established!" << endl;
char buf[200] = "Message from server to client\n";
//Send an initial message to the client
send(client, buf, sizeof(buf), 0);
//but then how do I wait for a message from the client again here??
//ive tried recv here before with no luck :/
closesocket(mysock);
closesocket(client);
WSACleanup();
system("pause >nul");
return 0;
}
//Client program here
int main(int argc, char *argv[])
{
WSADATA wsaData;
int starterr = WSAStartup(MAKEWORD(2,2), &wsaData);
if (starterr != 0)
{
cout << "WSADATA startup has failed!" << endl;
cout << "Error Code: " << WSAGetLastError() << endl;
system("pause >nul");
WSACleanup();
return 0;
}
cout << "WSADATA Startup Successful!" << endl;
SOCKET mysock = socket(AF_INET, SOCK_STREAM, 0);
if (mysock == INVALID_SOCKET)
{
cout << "Socket Creation Failed!" << endl;
cout << "Error Code: " << WSAGetLastError() << endl;
system("pause >nul");
WSACleanup();
return 0;
}
cout << "Socket Creation Successful!" << endl;
sockaddr_in sin;
sin.sin_port = htons(80);
sin.sin_addr.s_addr = inet_addr("127.0.0.1");//ip for connection
sin.sin_family = AF_INET;
if (connect(mysock,(sockaddr*)&sin, sizeof(sin)) == INVALID_SOCKET)
{
cout << "Socket Connection Failed" << endl;
cout << "Error Code: " << WSAGetLastError() << endl;
system("pause >nul");
closesocket(mysock);
WSACleanup();
return 0;
}
cout << "Socket Has Connected Successfuly!" << endl;
//Same applies here, receive message but exactly how do I send one back
char buf[200];
recv(mysock, buf, sizeof(buf), 0);
cout << buf;
//send here seemed to bring up a bunch of symbols
system("pause >nul");
WSACleanup();
closesocket(mysock);
return 0;
}
我遵循你的建议,并尝试了非阻塞IO,我现在得到它很好地工作,谢谢你的伟大答案 – user1143896 2012-02-28 18:34:52