我正在尝试做一个简单的多线程客户端/服务器聊天,但我有一个问题,程序进入一个循环而没有显示任何消息,我只有消息“收到:”,仅此而已。 这里的代码 服务器代码:
#pragma comment(lib, "WS2_32.lib") // link with Ws2_32.lib
#include <Winsock2.h>
#include <Windows.h>
#include <iostream>
#include <cstdio>
#include <pthread.h>
#define DEFAULT_PORT 54345
using namespace std;
void *receive(void *)
{
SOCKET client;
long app;
char buff[256];
memset(&buff,0,sizeof(buff));
while(1)
{
memset(&buff,0,sizeof(buff));
app=recv(client,buff,sizeof(buff), 0);
if(app<0) cout<<" Can't get message"<<endl;
else cout<<"Client: "<<buff<<endl;
}
}
int main()
{
pthread_t threadA[5];
cout<<"Server TCP/IP"<<endl;
char recvbuf[256]; //buff ti get message
char serverAddrStr[256]={'\0'}; //Contain the IP address
#ifdef WIN32
WSADATA wsaData;
WSAStartup (0x0101,&wsaData);
#endif
long res;
WSADATA wsadata;
res = WSAStartup(MAKEWORD(2, 1), &wsadata); //Initialize winsock
if(res==0) cout<<"Winsocket initialized!"<<endl;
else cout<<"Error initializing winsocket"<<endl;
SOCKET slisten,client; //Socket Descriptor
cout<<"IP address to connect: ";
cin.getline(serverAddrStr,256);
slisten=socket(AF_INET,SOCK_STREAM,0);
if(slisten!=INVALID_SOCKET) cout<<"Socket created!"<<endl;
else cout<<"Error creating socket"<<endl;
//info of the socket
sockaddr_in info; //sockaddr
info.sin_addr.s_addr=INADDR_ANY; //accept anyone
info.sin_family=AF_INET;
info.sin_port=htons(54345); //Convert the porta in the order (big-endian) of TCP/IP
int infolen=sizeof(info); //Contain dimension ofsockaddr
res=bind(slisten,(struct sockaddr*)&info, infolen);
if(res!=SOCKET_ERROR) cout<<"Connessione estabilished!"<<endl;
else cout<<"Connection error..."<<endl;
res=listen(slisten,SOMAXCONN);
if(res!=SOCKET_ERROR) cout<<"I'm on the port 54345"<<endl;
else cout<<"Can't listen on port 54345"<<endl;
sockaddr_in clientinfo; //Info of the client
int clientinfolen=sizeof(clientinfo);
int nthread=0;
while(nthread<5)
{
clientinfolen= sizeof(clientinfo);
cout<<"I'm waiting a client"<<endl;
client=accept(slisten,(struct sockaddr*)&clientinfo,&clientinfolen);
if(client<0)
{
cout<<"Can't accept connection"<<endl;
return 0;
}
cout<<"Connection estabilished"<<endl;
res=pthread_create(&threadA[nthread],NULL,receive,NULL);
if(res!=0) cout<<"Error creating thread"<<endl;
nthread++;
}
for(int i=0;i<5;i++) pthread_join(threadA[i],NULL);
closesocket(client);
closesocket(slisten);
WSACleanup();
system("PAUSE");
return 0;
}
这里是客户端代码:
#pragma comment(lib,"ws2_32.lib")
#include <cstdio>
#include <iostream>
#include <WinSock2.h>
#include <Windows.h>
using namespace std;
int main()
{
cout<<"Client TCP/IP"<<endl;
char sendbuf[256]; //Buffer to send
long res;
WSADATA wsadata;
SOCKET sConnect;
sockaddr_in conpar;
res=WSAStartup(MAKEWORD(2,0),&wsadata);
if(res==0) cout<<"Winsock initialized!"<<endl;
else cout<<"Winsock initializing failed"<<endl;
sConnect=socket(AF_INET,SOCK_STREAM,0); //Create the socket
if(sConnect!=INVALID_SOCKET) cout<<"Socket created!"<<endl;
else cout<<"Errore creating socket, error: "<<WSAGetLastError()<<endl;
hostent *serverInfo; //Info of the host
char serverAddrStr[256]; //IP address
cout<<"IP address: ";
cin.getline(serverAddrStr,256);
conpar.sin_addr.S_un.S_addr=inet_addr(serverAddrStr); //IP address to connect
conpar.sin_family=AF_INET;
conpar.sin_port=htons(54345); //Port to connect in big endian format
int conparlen=sizeof(conpar);
res=connect(sConnect,(struct sockaddr*)&conpar,conparlen); //Connection
if(res!=SOCKET_ERROR) cout<<"Connessione estabilished!"<<endl;
else cout<<"Connection failed, error: "<<WSAGetLastError()<<endl;
while(1)
{
memset(&sendbuf,0,sizeof(sendbuf));
cout<<"Send: ";
cin.getline(sendbuf,256);; //Get message
res=send(sConnect,sendbuf,strlen(sendbuf),0);
}
closesocket(sConnect);
WSACleanup();
system("PAUSE");
return 0;
}
我在Windows系统中使用posix线程并安装了软件包。 谢谢大家的帮助:))
答案 0 :(得分:0)
您的receive
函数从未将client
设置为合理的值。你有很多其他问题,但这是最明显的问题。
void *receive(void *)
{
SOCKET client; // ** Create variable
long app;
char buff[256];
memset(&buff,0,sizeof(buff));
while(1)
{
memset(&buff,0,sizeof(buff));
app=recv(client,buff,sizeof(buff), 0); // ** Use variable
if(app<0) cout<<" Can't get message"<<endl;
else cout<<"Client: "<<buff<<endl;
}
}
它在哪里设定为合理的值?
您似乎也错误地认为在TCP连接上调用recv
会收到一条消息。那不是真的。如果你想要一个接收消息的函数,你必须写一个。