TCP实验报告(14)
发布时间:2021-06-06
发布时间:2021-06-06
err = connect(sockClient,(SOCKADDR*)&addrSrv,sizeof(SOCKADDR));//请求连接服务器进程 if ( err == INVALID_SOCKET ) { cout<<"connect() fail:"<<WSAGetLastError()<<endl; return 0; } char sendBuf[1024],hostname[100]; if (gethostname(hostname,100) != 0) //获取主机名称 strcpy(hostname,"None"); strcpy(sendBuf,hostname); strcat(sendBuf," have conneted to you!"); err = send(sockClient,sendBuf,strlen(sendBuf)+1,0); //发送数据 if (err == SOCKET_ERROR) { cout<<"send() fail:"<<WSAGetLastError()<<endl; return 0; } char recvBuf[1024]; iLen = recv(sockClient,recvBuf,1024,0); //接收数据 if (iLen == 0) return 0; else if (iLen == SOCKET_ERROR) { cout<<"recv() fail:"<<WSAGetLastError()<<endl; return 0; } else { recvBuf[iLen] = '\0'; cout<<recvBuf<<endl; } closesocket(sockClient); //关闭套接字 WSACleanup();
}
下一篇:军事理论教材复习提纲