#include #include #include #include #include using namespace std; SOCKET Socket; SOCKADDR_IN addr; int main(){ string m_host = "192.168.0.90"; string m_port = "50290"; WSADATA wsa; int rc = WSAStartup(MAKEWORD(2, 0), &wsa); if(rc == SOCKET_ERROR){ cout << "Error while starting server... \n"; } Socket = socket(AF_INET, SOCK_STREAM, 0); if(Socket == INVALID_SOCKET){ std::cout << "Error while building socket. \n"; } addr.sin_addr.s_addr = inet_addr(m_host.c_str()); addr.sin_port = htons(atoi(m_port.c_str())); addr.sin_family = AF_INET; if(connect(Socket, (SOCKADDR*)&addr, sizeof(SOCKADDR)) == 0){ cout << "Connection with server " << inet_ntoa(addr.sin_addr) << " established." << endl << endl; } else { cout << "Impossible to connect to server " << inet_ntoa(addr.sin_addr) << ":" << addr.sin_port << ". Please check host and port. \n"; return 1; } // Everything okay - start communicating with server char buffersend[1024]; char bufferrecv[1024]; int bufpos = 0; int bufIndex = 0; char c; fd_set fdSetRead; TIMEVAL timeout; bool mainloop = true; while(rc != SOCKET_ERROR && mainloop){ FD_ZERO(&fdSetRead); FD_SET(Socket, &fdSetRead); timeout.tv_sec = 0; timeout.tv_usec = 0; //RECEIVE DATA while((rc=select(0,&fdSetRead,NULL,NULL,&timeout)) != 0) { u_long iNumBytesPresent; u_long iMode = 1; // switch socket to non-blocking rc = ioctlsocket(Socket, FIONBIO, &iMode ); //recv (returns if there is no data) rc = recv(Socket, bufferrecv,1023, 0); iNumBytesPresent = rc; // if first recv call returned immediately while(iNumBytesPresent <= 0) { rc = ioctlsocket(Socket, FIONREAD ,&iNumBytesPresent); if(iNumBytesPresent > 0) { break; } // Read available number of bytes rc = recv(Socket, bufferrecv, iNumBytesPresent, 0); } bufferrecv[rc] = '\0'; cout << "Message from server: " << bufferrecv << '\n'; } //SEND DATA while(kbhit()){ if (c==27){ //Esc-Button mainloop = false; break; } c = getch(); cout<