我用winsock2.h找到了这个套接字代码http://www.tidytutorials.com/2009/12/c-winsock-example-using-client-server.html这是我的客户的代码。
int hsock;
int * p_int ;
hsock = socket(AF_INET, SOCK_STREAM, 0);
///
char buffer[1024];
int buffer_len = 1024;
int bytecount;
int c;
memset(buffer, '\0', buffer_len);
for(char* p=buffer ; (c=getch())!=13 ; p++){
printf("%c", c);
*p = c;
}
if( (bytecount=send(hsock, buffer, strlen(buffer),0))==SOCKET_ERROR){
服务器的代码是用 Qt QTcpServer 类完成的:
void FortuneThread::run()
{
QTcpSocket tcpSocket;
if (!tcpSocket.setSocketDescriptor(socketDescriptor)) {
emit error(tcpSocket.error());
cout<<"socket error"<<endl;
return;
}
char buffer[1024];
int buffer_len = 1024;
memset(buffer, 0, buffer_len);
strcat(buffer, " SERVER ECHO");
QByteArray block;
QDataStream out(&block, QIODevice::WriteOnly);
out.setVersion(QDataStream::Qt_4_0);
out << &socketDescriptor;
out << buffer;
out << strlen(buffer);
out << 0;
tcpSocket.write(block);
上面的服务器代码是我尝试将服务器数据与客户端数据匹配失败。怎么了?泰