I'm writing a client in C++ for my AI project that communicates with a server written in Java, to which I do not have access to the code. They use TCP sockets to communicate: the server sends me a message and the client performs an action accordingly, such as generating a move or updating the board or declaring victory, defeat and so on. Now I have noticed that sometimes the socket gets stuck and the server sends back a timeout message, even though the client has never generated a move and never entered in the "YOUR_MOVE" else if, which is weird, because it computes the move in no time (a handful of milliseconds) and if I add a 50 milliseconds sleep either before send or receive it works like a charm. I wonder what could possibly go wrong. That's the code:
#include <iostream>
#include <cstring> // Needed for memset
#include <sys/socket.h> // Needed for the socket functions
#include <netdb.h> // Needed for the socket functions
#include <unistd.h>
#include <string>
#include <boost/algorithm/string/predicate.hpp>
#include <boost/lexical_cast.hpp>
#include <chrono>
#include "AI.cpp"
#include <locale>
#include <thread>
using namespace::std;
void error(const char *msg)
{
perror(msg);
exit(0);
}
class Client{
private:
AI* ai;
public:
int socketfd;
Client(const char* serverAddress, const char* port){
int status;
struct sockaddr_in serv_addr;
struct hostent *server;
portno = 8901;
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
error("ERROR opening socket");
server = gethostbyname("127.0.0.1");
if (server == NULL) {
fprintf(stderr,"ERROR, no such host\n");
exit(0);
}
bzero((char *) &serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
bcopy((char *)server->h_addr,
(char *)&serv_addr.sin_addr.s_addr,
server->h_length);
serv_addr.sin_port = htons(portno);
if (connect(sockfd,(struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0)
error("ERROR connecting");
ai = new AI();
(*ai).distance();
}
~Client(){
freeaddrinfo(host_info_list);
close(socketfd);
delete ai;
}
bool startsWith(char c1[], char c2[]){
int i=0;
while(c2[i] != 0){
if(c1[i]==0)
return false;
if(c1[i] != c2[i])
return false;
i++;
}
return true;
}
void substr(char c[], int pos, char* s){
int i;
for(i=pos; c[i]!=0; i++){
s[i-pos] = c[i];
}
s[i-pos] = 0;
}
void play(){
bzero(buffer,256);
n = read(sockfd,buffer,255);
if(startsWith(buffer, (char*)"WELCOME"))
printf(buffer);
string colour;
if(buffer[9]=='l')
colour="black";
else
colour="white";
char* msg = new char[256];
while(true){
bzero(buffer,256);
n = read(sockfd,buffer,255);
//printf(buffer);
if(startsWith(buffer, (char*)"VALID_MOVE"))
cout << "Valid move, please wait" << endl;
else if(startsWith(buffer, (char*)"OPPONENT_MOVE")){
substr(buffer, 14, msg);
ai->updateBoard(msg);
cout << "Opponent move: " << msg << endl;
}
else if(startsWith(buffer, (char*)"VICTORY")){
cout << "You win" << endl;
break;
}
else if(startsWith(buffer, (char*)"DEFEAT")){
cout << "You lose" <<endl;
break;
}
else if(startsWith(buffer, (char*)"TIE")){
cout << "You tied" << endl;
break;
}
else if(startsWith(buffer, (char*)"YOUR_TURN")){
cout << "YOUR_TURN branch" << endl;
string move = ai->generateNextMove(colour, 3);
cout << "Your move is: " << move << endl;
int i;
for(i=0;i<8; i++){
msg[i] = move.at(i);
}
msg[i] = 0;
sprintf(buffer, "MOVE %s\n", msg);
std::this_thread::sleep_for(std::chrono::milliseconds(40));
n = write(sockfd,buffer,strlen(buffer));
ai->updateBoard(move);
}
else if(startsWith(buffer, (char*)"TIMEOUT")){
cout << "Time out" << endl;
break;
}
else if(startsWith(buffer, (char*)"MESSAGE")){
substr(buffer, 8, msg);
cout << msg << endl;
}
}
close(sockfd);
}
};
int main()
{
const char serverAddr[]="127.0.0.1";
const char port[]="8901";
Client * client = new Client(serverAddr, port);
client->play();
delete client;
return 0;
}
P.S. This is my first post, please show forgiveness if I have involuntarily forgotten to provide some other information. Thank you.
For what I can see you are taking for granted that when you perform socket read with
n = read(sockfd,buffer,255);
You seem to be sure that you always receive an entire command, but think what happens if you receive only "YOUR TU" instead of "YOUR TURN"
You always clean the buffer and the next loop you receive just "RN" so you never process the command and the server remains waiting for your move.
What you should do is clean the buffer ONLY when you have received a full meaningful command.
If you uncomment the:
//printf(buffer);
You can check if my theory is correct.
Related
I'm using libev + non-blocking sockets to send a request to a server. I'm using Keep Alive because I need to send future requests to the destination over this same connection.
Behavior
Run the program and it fetches the URL and logs to console, as expected.
After doing this, wait and don't push ctrl+c to exit the program.
Expected
App should stay open because event loop is waiting for future responses but should not console log anything after the initial response.
Actual
Leave the app running. After 30+ seconds, it will start to console log the same response over and over and over again without end.
Question
Why is libev calling my callback (example_cb) repeatedly when no new request was sent and no new response data was received? How can I fix this?
#include <ev.h>
#include <stdio.h>
#include <iostream>
#include <ctype.h>
#include <cstring>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <fcntl.h>
#include <netdb.h>
#include <netinet/in.h>
#include <unistd.h>
#include <sstream>
#include <fstream>
#include <string>
using namespace std;
void sendRequest(int sockfd)
{
puts("------");
puts("sendRequest() was called");
stringstream ss;
ss << "GET /posts/11 HTTP/1.1\r\n"
<< "Host: jsonplaceholder.typicode.com\r\n"
<< "Accept: application/json\r\n"
<< "\r\n";
string request = ss.str();
if (send(sockfd, request.c_str(), request.length(), 0) != (int)request.length()) {
cout << "Error sending request." << endl;
exit(1);
}
cout << "Request sent. No err occured." << endl;
}
static void delay_cb(EV_P_ ev_timer *w, int revents)
{
puts("------");
puts("delay_cb() was called");
sendRequest(3);
}
static void example_cb(EV_P_ ev_io *w, int revents)
{
puts("------");
puts("example_cb() was called");
int sockfd = 3;
size_t len = 80*1024, nparsed; // response must be <= 80 Kb
char buf[len];
ssize_t recved;
recved = recv(sockfd, &buf, len, 0);
if (recved < 0) {
perror("recved was <1");
}
// don't process keep alives
if (buf[0] != '\0') {
std::cout << buf << std::endl;
}
// clear buf
buf[0] = '\0';
std::cout << "buf after clear attempt: " << buf << std::endl;
}
int example_request()
{
std::string hostname = "jsonplaceholder.typicode.com";
int PORT = 80;
struct sockaddr_in client;
struct hostent * host = gethostbyname(hostname.c_str());
if (host == NULL || host->h_addr == NULL) {
cout << "Error retrieving DNS information." << endl;
exit(1);
}
bzero(&client, sizeof(client));
client.sin_family = AF_INET;
client.sin_port = htons( PORT );
memcpy(&client.sin_addr, host->h_addr, host->h_length);
// create a socket
int sockfd = socket(PF_INET, SOCK_STREAM, 0);
if (sockfd < 0) {
cout << "Error creating socket." << endl;
exit(1);
}
cout << "Socket created" << endl;
// enable keep alive
int val = 1;
setsockopt(sockfd, SOL_SOCKET, SO_KEEPALIVE, &val, sizeof val);
if (connect(sockfd, (struct sockaddr *)&client, sizeof(client)) < 0) {
close(sockfd);
cout << "Could not connect" << endl;
exit(1);
}
cout << "Socket connected" << endl;
// make non-blocking
int status = fcntl(sockfd, F_SETFL, fcntl(sockfd, F_GETFL, 0) | O_NONBLOCK);
if (status == -1) {
perror("ERROR making socket non-blocking");
}
std::cout << "Socket set to non-blocking" << std::endl;
std::cout << "Sockfd is: " << sockfd << std::endl;
return sockfd;
}
int main(void)
{
// establish socket connection
int sockfd = example_request();
struct ev_loop *loop = EV_DEFAULT;
ev_io example_watcher;
ev_io_init(&example_watcher, example_cb, sockfd, EV_READ);
ev_io_start(loop, &example_watcher);
// used to send the request 2 sec later
ev_timer delay_watcher;
ev_timer_init(&delay_watcher, delay_cb, 2, 0.0);
ev_timer_start(loop, &delay_watcher);
ev_run(loop, 0);
return 0;
}
Edit: Code updated with suggestions from comments
The source of the problem is that you do not check recved == 0 condition which corresponds to the other side closing the connection. When that happens the OS sets the socket into "closed mode" which (at least under linux) is always ready for reading and subsequent calls to recv will always return 0.
So what you need to do is to check for that condition, call close(fd); on the file descriptor (possibly with shutdown before) and ev_io_stop on the associated watcher. If you wish to continue at that point then you have to open a new socket and eo_io_start new watcher.
I need to send a string back to client that includes the cost of vehicle and the vehicle with modifier(carStyling). I want to return a string containing the sline and the cost to the client. Something like;
Your Sedan Offroad will cost $150000.
Paragraph below contains the code necessary.
#include <arpa/inet.h>
#include <netdb.h>
#include <netinet/in.h>
#include <unistd.h>
#include <iostream>
#include <cstring>
#include <stdlib.h>
#include <stdio.h>
#include <string>
#include <sstream>
#define MAX_MSG 100
#define LINE_ARRAY_SIZE (MAX_MSG+1)
using namespace std;
int main()
{
int listenSocket, connectSocket, i;
unsigned short int listenPort;
socklen_t clientAddressLength
;
struct sockaddr_in clientAddress, serverAddress;
char line[LINE_ARRAY_SIZE];
cout << "Enter port number to listen on (between 1500 and 65000): ";
cin >> listenPort;
// Create socket for listening for client connection
// requests.
listenSocket = socket(AF_INET, SOCK_STREAM, 0);
if (listenSocket < 0) {
cerr << "cannot create listen socket";
exit(1);
}
// Bind listen socket to listen port. First set various
// fields in the serverAddress structure, then call
// bind().
// htonl() and htons() convert long integers and short
// integers (respectively) from host byte order (on x86
// this is Least Significant Byte first) to network byte
// order (Most Significant Byte first).
serverAddress.sin_family = AF_INET;
serverAddress.sin_addr.s_addr = htonl(INADDR_ANY);
serverAddress.sin_port = htons(listenPort);
if (bind(listenSocket,
(struct sockaddr *) &serverAddress,
sizeof(serverAddress)) < 0) {
cerr << "cannot bind socket";
exit(1);
}
// Wait for connections from clients. This is a
// non-blocking call; i.e., it registers this program with
// the system as expecting connections on this socket, and
// then this thread of execution continues on.
listen(listenSocket, 5);
while (1) {
cout << "Waiting for TCP connection on port " << listenPort << " ...\n";
// Accept a connection with a client that is requesting
// one. The accept() call is a blocking call; i.e., this
// thread of execution stops until a connection comes
// in. connectSocket is a new socket that the system
// provides, separate from listenSocket. We *could*
// accept more connections on listenSocket, before
// connectSocket is closed, but this program doesn't do
// that.
clientAddressLength = sizeof(clientAddress);
connectSocket = accept(listenSocket,
(struct sockaddr *) &clientAddress,
&clientAddressLength);
if (connectSocket < 0) {
cerr << "cannot accept connection ";
exit(1);
}
// Show the IP address of the client.
// inet_ntoa() converts an IP address from binary form to the
// standard "numbers and dots" notation.
cout << " connected to " << inet_ntoa(clientAddress.sin_addr);
// Show the client's port number.
// ntohs() converts a short int from network byte order (which is
// Most Significant Byte first) to host byte order (which on x86,
// for example, is Least Significant Byte first).
cout << ":" << ntohs(clientAddress.sin_port) << "\n";
// Read lines from socket, using recv(), storing them in the line
// array. If no messages are currently available, recv() blocks
// until one arrives.
// First set line to all zeroes, so we'll know where the end of
// the string is.
memset(line, 0x0, LINE_ARRAY_SIZE);
while (recv(connectSocket, line, MAX_MSG, 0) > 0) {
cout << " -- " << line << "\n";
// Convert line to upper case.
for (i = 0; line[i] != '\0'; i++)
line[i] = toupper(line[i]);
// creating an object to direct line to a string array
std::string delimiter[2];
int i = 0;
double cost = 0;
std::string carType;
std::string carStyling;
std::string sline;
sline = line;
stringstream ssin(sline);
while (ssin.good() && i < 2){
ssin >> delimiter[i];
++i;
}
for(i = 0; i < 2; i++){
cout << delimiter[i] << endl;
}
if(i==0) {
carType = delimiter[0];
if(carType.compare("SEDAN")==0){
sline = "Your Sedan";
cost = 100000;
std::copy(sline.begin(), sline.end(), line);
line[sline.size()] = '\0';
}
else if(carType.compare("MPV")==0){
sline = "MPV";
cost = 120000;
std::copy(sline.begin(), sline.end(), line);
line[sline.size()] = '\0';
}
else if(carType.compare("SUV")==0){
sline = "SUV";
cost = 140000;
std::copy(sline.begin(), sline.end(), line);
line[sline.size()] = '\0';
}
else if(carType.compare("LUXURY")==0){
sline = "LUXURY";
cost = 180000;
std::copy(sline.begin(), sline.end(), line);
line[sline.size()] = '\0';
}
if(i==2) {
carStyling = delimiter[1];
if(carStyling.compare("SPORTY")==0){
sline += "Sporty";
cost = cost * 1.5;
}
else if(carStyling.compare("OFFROAD")==0){
sline += "Offroad";
cost = cost * 1.3;
}
}
}
// Send converted line back to client.
if (send(connectSocket, line, strlen(line) + 1, 0) < 0)
cerr << "Error: cannot send modified data";
memset(line, 0x0, LINE_ARRAY_SIZE); // set line to all zeroes
}
}
}
The other one here is the code for the client.cc
#include <netdb.h>
#include <netinet/in.h>
#include <unistd.h>
#include <iostream>
#include <cstring>
#include <stdlib.h>
#define MAX_LINE 100
#define LINE_ARRAY_SIZE (MAX_LINE+1)
using namespace std;
int main()
{
int socketDescriptor;
unsigned short int serverPort;
struct sockaddr_in serverAddress;
struct hostent *hostInfo;
char buf[LINE_ARRAY_SIZE], c;
cout << "Enter server host name or IP address: ";
cin.get(buf, MAX_LINE, '\n');
// gethostbyname() takes a host name or ip address in "numbers and
// dots" notation, and returns a pointer to a hostent structure,
// which we'll need later. It's not important for us what this
// structure is actually composed of.
hostInfo = gethostbyname(buf);
if (hostInfo == NULL) {
cout << "problem interpreting host: " << buf << "\n";
exit(1);
}
cout << "Enter server port number: ";
cin >> serverPort;
cin.get(c); // dispose of the newline
// Create a socket. "AF_INET" means it will use the IPv4 protocol.
// "SOCK_STREAM" means it will be a reliable connection (i.e., TCP;
// for UDP use SOCK_DGRAM), and I'm not sure what the 0 for the last
// parameter means, but it seems to work.
socketDescriptor = socket(AF_INET, SOCK_STREAM, 0);
if (socketDescriptor < 0) {
cerr << "cannot create socket\n";
exit(1);
}
// Connect to server. First we have to set some fields in the
// serverAddress structure. The system will assign me an arbitrary
// local port that is not in use.
serverAddress.sin_family = hostInfo->h_addrtype;
memcpy((char *) &serverAddress.sin_addr.s_addr,
hostInfo->h_addr_list[0], hostInfo->h_length);
serverAddress.sin_port = htons(serverPort);
if (connect(socketDescriptor,
(struct sockaddr *) &serverAddress,
sizeof(serverAddress)) < 0) {
cerr << "cannot connect\n";
exit(1);
}
cout << "\nWelcome to Car Customization Server. What are your orders?\n";
cout << ">> Type of vehicle: Sedan, MPV, SUV, Luxury\n";
cout << ">> Type of Styling: Sporty, Offroad\n";
cout << ">> Eg. To order type: MPV Sporty\n";
// Prompt the user for input, then read in the input, up to MAX_LINE
// charactars, and then dispose of the rest of the line, including
// the newline character.
cout << "Enter Order: ";
cin.get(buf, MAX_LINE, '\n');
while (cin.get(c) && c != '\n')
; //Loop does nothing except consume the spare bytes
// Stop when the user inputs a line with just a dot.
while (strcmp(buf, ".")) { //strcmp returns 0 when the two strings
//are the same, so this continues when
//they are different
// Send the line to the server.
if (send(socketDescriptor, buf, strlen(buf) + 1, 0) < 0) {
cerr << "cannot send data ";
close(socketDescriptor); //Note this is just like using files...
exit(1);
}
// Zero out the buffer.
memset(buf, 0x0, LINE_ARRAY_SIZE);
// Read the modified line back from the server.
if (recv(socketDescriptor, buf, MAX_LINE, 0) < 0) {
cerr << "didn't get response from server?";
close(socketDescriptor);
exit(1);
}
cout << "results: " << buf << "\n";
// Prompt the user for input, then read in the input, up to MAX_LINE
// charactars, and then dispose of the rest of the line, including
// the newline character. As above.
cout << "Enter Order: ";
cin.get(buf, MAX_LINE, '\n');
while (cin.get(c) && c != '\n')
; //again, consuming spare bytes
}
close(socketDescriptor);
return 0;
}
So there, if anyone knows how to send back both the string and the cost. Please reply. Thank you.
You can pack your std::string sline into sending buffer by copying bytes:
memcpy(line, sline.c_str(), strlen(sline.c_str()))
send it, and then on the client side unpack it the same way.
edit:
Try code below for you server, is this what you wanted?
#include <arpa/inet.h>
#include <netdb.h>
#include <netinet/in.h>
#include <unistd.h>
#include <iostream>
#include <cstring>
#include <stdlib.h>
#include <stdio.h>
#include <string>
#include <sstream>
#define MAX_MSG 100
#define LINE_ARRAY_SIZE (MAX_MSG+1)
using namespace std;
int main()
{
int listenSocket, connectSocket, i;
unsigned short int listenPort;
socklen_t clientAddressLength;
struct sockaddr_in clientAddress, serverAddress;
char line[LINE_ARRAY_SIZE];
cout << "Enter port number to listen on (between 1500 and 65000): ";
cin >> listenPort;
listenSocket = socket(AF_INET, SOCK_STREAM, 0);
if (listenSocket < 0) {
cerr << "cannot create listen socket";
exit(1);
}
serverAddress.sin_family = AF_INET;
serverAddress.sin_addr.s_addr = htonl(INADDR_ANY);
serverAddress.sin_port = htons(listenPort);
if (bind(listenSocket, (struct sockaddr *) &serverAddress,
sizeof(serverAddress)) < 0) {
cerr << "cannot bind socket";
exit(1);
}
listen(listenSocket, 5);
while (1) {
cout << "Waiting for TCP connection on port " << listenPort << " ...\n";
clientAddressLength = sizeof(clientAddress);
connectSocket = accept(listenSocket, (struct sockaddr *) &clientAddress,
&clientAddressLength);
if (connectSocket < 0) {
cerr << "cannot accept connection ";
exit(1);
}
cout << " connected to " << inet_ntoa(clientAddress.sin_addr);
cout << ":" << ntohs(clientAddress.sin_port) << "\n";
memset(line, 0x0, LINE_ARRAY_SIZE);
while (recv(connectSocket,line, MAX_MSG, 0) > 0) {
cout << " -- " << line << "\n";
std::string delimiter[2];
int i = 0;
double cost = 0;
std::string carType;
std::string carStyling;
std::string sline;
sline = line;
stringstream ssin(sline);
while (ssin.good() && i < 2){
ssin >> delimiter[i];
++i;
}
sline = "";
for(i = 0; i < 2; i++){
cout << delimiter[i] << endl;
}
sline += "Your ";
carType = delimiter[0];
if(carType.compare("Sedan")==0){
sline += "Sedan";
cost = 100000;
}
else if(carType.compare("MPV")==0){
sline += "MPV";
cost = 120000;
}
else if(carType.compare("SUV")==0){
sline += "SUV";
cost = 140000;
}
else if(carType.compare("Luxury")==0){
sline += "Luxury";
cost = 180000;
}
carStyling = delimiter[1];
if(carStyling.compare("Sporty")==0){
sline += " Sporty ";
cost = cost * 1.5;
}
else if(carStyling.compare("Offroad")==0){
sline += " Offroad ";
cost = cost * 1.3;
}
sline += "will cost ";
std::ostringstream ss;
ss << cost;
sline += ss.str();
sline.copy(line, sline.length());
if (send(connectSocket, line, strlen(line) + 1, 0) < 0)
cerr << "Error: cannot send modified data";
memset(line, 0x0, LINE_ARRAY_SIZE); // set line to all zeroes
}
}
}
I have no idea why send data is 48 bytes 010,0,0..., someone can explain? the problem is buffer for data received, I don't know how big he should be, and even if I receive data, how to make normal time from it?
Here's the code:
#include <iostream>
#define _WINSOCK_DEPRECATED_NO_WARNINGS
#define WIN32_MEAN_AND_LEAN
#include <winsock2.h>
#include <windows.h>
#include <time.h>
using namespace std;
class HRException
{
public:
HRException() :
m_pMessage("") {}
virtual ~HRException() {}
HRException(const char *pMessage) :
m_pMessage(pMessage) {}
const char * what() { return m_pMessage; }
private:
const char *m_pMessage;
};
const int REQ_WINSOCK_VER = 2; // Minimum winsock version required
const char DEF_SERVER_NAME[] = "0.pl.pool.ntp.org";
const int SERVER_PORT = 123;
const int TEMP_BUFFER_SIZE = 128;
const char msg[48] = { 010,0,0,0,0,0,0,0,0 };
// IP number typedef for IPv4
typedef unsigned long IPNumber;
IPNumber FindHostIP(const char *pServerName)
{
HOSTENT *pHostent;
// Get hostent structure for hostname:
if (!(pHostent = gethostbyname(pServerName)))
throw HRException("could not resolve hostname.");
// Extract primary IP address from hostent structure:
if (pHostent->h_addr_list && pHostent->h_addr_list[0])
return *reinterpret_cast<IPNumber*>(pHostent->h_addr_list[0]);
return 0;
}
void FillSockAddr(sockaddr_in *pSockAddr, const char *pServerName, int portNumber)
{
// Set family, port and find IP
pSockAddr->sin_family = AF_INET;
pSockAddr->sin_port = htons(portNumber);
pSockAddr->sin_addr.S_un.S_addr = FindHostIP(pServerName);
}
bool RequestHeaders(const char *pServername)
{
SOCKET hSocket = INVALID_SOCKET;
char tempBuffer[TEMP_BUFFER_SIZE];
sockaddr_in sockAddr = { 0 };
bool bSuccess = true;
try
{
// Lookup hostname and fill sockaddr_in structure:
cout << "Looking up hostname " << pServername << "... ";
FillSockAddr(&sockAddr, pServername, SERVER_PORT);
cout << "found.\n";
// Create socket
cout << "Creating socket... ";
if ((hSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == INVALID_SOCKET)
throw HRException("could not create socket.");
cout << "created.\n";
// Connect to server
cout << "Attempting to connect to " << inet_ntoa(sockAddr.sin_addr)
<< ":" << SERVER_PORT << "... ";
if (connect(hSocket, reinterpret_cast<sockaddr*>(&sockAddr), sizeof(sockAddr)) != 0)
throw HRException("could not connect.");
cout << "connected.\n";
cout << "Sending request... ";
// send request part 1
if (send(hSocket, msg, sizeof(msg) , 0) == SOCKET_ERROR)
throw HRException("failed to send data.");
cout << "request sent.\n";
cout << "Dumping received data...\n\n";
// Loop to print all data
recv(hSocket, tempBuffer, sizeof(tempBuffer), 0); // <-- the problem
///
//part where we take time out of tempBuffer
///
}
catch (HRException e)
{
cerr << "\nError: " << e.what() << endl;
bSuccess = false;
}
if (hSocket != INVALID_SOCKET)
{
closesocket(hSocket);
}
return bSuccess;
}
int main(int argc, char* argv[])
{
int iRet = 1;
WSADATA wsaData;
cout << "Initializing winsock... ";
if (WSAStartup(MAKEWORD(REQ_WINSOCK_VER, 0), &wsaData) == 0)
{
// Check if major version is at least REQ_WINSOCK_VER
if (LOBYTE(wsaData.wVersion) >= REQ_WINSOCK_VER)
{
cout << "initialized.\n";
// Set default hostname:
const char *pHostname = DEF_SERVER_NAME;
// Set custom hostname if given on the commandline:
if (argc > 1)
pHostname = argv[1];
iRet = !RequestHeaders(pHostname);
}
else
{
cerr << "required version not supported!";
}
cout << "Cleaning up winsock... ";
// Cleanup winsock
if (WSACleanup() != 0)
{
cerr << "cleanup failed!\n";
iRet = 1;
}
cout << "done.\n";
}
else
{
cerr << "startup failed!\n";
}
int x;
cin >> x;
return iRet;
}
Most part of code is from madwizard.org
Ok it works, main part of code:
const char msg[48] = { 010,0,0,0,0,0,0,0,0 };
if (send(hSocket, msg, sizeof(msg) , 0) == SOCKET_ERROR)
throw HRException("failed to send data.");
cout << "request sent.\n";
cout << "Dumping received data...\n\n";
char tempBuffer[1024];
int bytes = recv(hSocket, tempBuffer, sizeof(tempBuffer), 0);
cout << "bytes received: " << bytes << endl;
time_t tmit;
tmit = ntohl(((time_t*)tempBuffer)[4]);
tmit -= 2208988800U;
cout << ctime(&tmit);
No idea why data that we send is
msg[48] = { 010,0,0,0,0,0,0,0,0 };
and why received data contains many numbers? for example if change code to
tmit = ntohl(((time_t*)tempBuffer)[6]);
I will get date 2008y, why?
Guys why so many minuses?, still waiting for an explanation :D
Here's whole code http://pastebin.com/Sv3ERGfV , dont forget to link ws2_32.lib
Similar to my issue when trying to query the time from a self-hostet Windows-NTP-Server with the C++ library NTPClient which uses boost for the network tasks, msg[48] = { 010,0,0,0,0,0,0,0,0 }; configures the ntp.flags.mode. After comparing the network traffic of w32tm /stripchart /computer:10.159.96.65 using Wireshark, flag 27 or 11 seem to be the choices for my usecase:
Comparison of NTP network packages
tmit = ntohl(((time_t*)tempBuffer)[6]); extracts the data from the received package. It looks like
4 yields the reference time (last sync with timeserver I assume),
8 the time when server received request and
10 the transmit time (which should be almost equal).
In C++, how to detect if a connection to client has been lost for some seconds, lets say 30 seconds. Even if the client dont't respond for more than 30 seconds (for example, the server is waiting for the client to reply), it's not a lost connection as long as the connection is still established.
#include <string.h>
#include <unistd.h>
#include <stdio.h>
#include <netdb.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <iostream>
#include <fstream>
#include <strings.h>
#include <stdlib.h>
#include <string>
#include <pthread.h>
#include <unistd.h>
#include <vector>
#include <thread>
using namespace std;
static int connFd;
void error(const char *msg){
perror(msg);
exit(1);
}
void task1 (int connFd){
//CEstablish timeut connection for client
struct timeval timeout;
timeout.tv_sec = 30;
timeout.tv_usec = 0;
if (setsockopt (connFd, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout,
sizeof(timeout)) < 0)
error("setsockopt failed\n");
if (setsockopt (connFd, SOL_SOCKET, SO_SNDTIMEO, (char *)&timeout,
sizeof(timeout)) < 0)
error("setsockopt failed\n");
cout << "Thread No: " << pthread_self() << endl;
char test[256];
bzero(test, 256);
bool loop = false;
int n = write(connFd,"I got your message",18);
if (n < 0) error("ERROR writing to socket");
while(!loop){
bzero(test, 256);
int n = read(connFd, test, 255);
if (n < 0) {
//error("ERROR reading from socket");
cout << " ERROR " << endl;
break;
}
printf("Here is the message: %s\n",test);
//n = write(connFd,"I got your message",18);
//if (n < 0) error("ERROR end to socket");
//string tester (test);
//cout << tester << endl;
//if(tester == "exit")
//break;
}
cout << "\nClosing thread and conn" << endl;
close(connFd);
}
int main(int argc, char* argv[]){
int pId, portNo, listenFd;
socklen_t len; //store size of the address
bool loop = false;
struct sockaddr_in svrAdd, clntAdd;
pthread_t threadA[3];
std::vector<std::thread> threads;
if (argc < 2)
{
cerr << "Syntam : ./server <port>" << endl;
return 0;
}
portNo = atoi(argv[1]);
if((portNo > 65535) || (portNo < 2000))
{
cerr << "Please enter a port number between 2000 - 65535" << endl;
return 0;
}
//create socket
listenFd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
if(listenFd < 0)
{
cerr << "Cannot open socket" << endl;
return 0;
}
bzero((char*) &svrAdd, sizeof(svrAdd));
svrAdd.sin_family = AF_INET;
svrAdd.sin_addr.s_addr = INADDR_ANY;
svrAdd.sin_port = htons(portNo);
//bind socket
if(bind(listenFd, (struct sockaddr *)&svrAdd, sizeof(svrAdd)) < 0)
{
cerr << "Cannot bind" << endl;
return 0;
}
listen(listenFd, 5);
int noThread = 0;
while (noThread < 3)
{
socklen_t len = sizeof(clntAdd);
cout << "Listening" << endl;
//this is where client connects. svr will hang in this mode until client conn
connFd = accept(listenFd, (struct sockaddr *)&clntAdd, &len);
if (connFd < 0) {
cerr << "Cannot accept connection" << endl;
return 0;
}
else {
cout << "Connection successful" << endl;
}
///thread t(&task1,connFd);
threads.push_back(std::thread(task1,connFd));
//pthread_create(&threadA[noThread], NULL, task1, NULL);
noThread++;
}
for(auto && t : threads)
t.join();
/*for(int i = 0; i < 3; i++)
{
pthread_join(threadA[i], NULL);
}*/
}
The problem with above code is, if a client don't send any reply to server in 30 seconds, it will be considered as connection lost even the connection is still established.
After setting the timeout socket options, your reading loop does this:
while(!loop){
bzero(test, 256);
int n = read(connFd, test, 255);
if (n < 0) {
cout << " ERROR " << endl;
break;
}
printf("Here is the message: %s\n",test);
}
cout << "\nClosing thread and conn" << endl;
close(connFd);
Now, on timeout read() will return -1, so you break and close(connFd);. That explains the problem you describe:
The problem with above code is, if a client don't send any reply to server in 30 seconds, it will be considered as connection lost even the connection is still established.
It's not the socket library closing the connection - it's your close call. Instead, handle read() returning -1 with errno or EAGAIN or EWOULDBLOCK in some other way that doesn't close the connection.
The problem with above code is, if a client don't send any reply to server in 30 seconds, it will be considered as connection lost even the connection is still established.
Only because you're making zero attempt to distinguish between error conditions.
If you get a read timeout, errno == EAGAIN/EWOULDBLOCK, the connection is still alive, and you should take whatever action is appropriate to a read timeout.
If you get any other error from recv(), then you should consider the connection broken.
If you get any error from send() you should also consider the connection closed, which you aren't doing at all, you're ignoring it.
Another problem with your code is that you aren't detecting end of stream. If recv() returns zero, the peer has disconnected cleanly, and you should close the socket and stop reading.
I use to code in Python. Now I'm trying C++. When I run the program I see the target address (w/ Wireshark) reverse, even if I use htonl. In Python this same program worked fine.
Follow the code. At the bottom I printed the result.
I'm using Ubuntu 12.04LTS and g++ (Ubuntu/Linaro 4.6.3).
//UdpClient.cpp
#include <iostream>
#include <string>
#include <sstream>
#include <sys/socket.h>
#include <sys/types.h>
#include <netdb.h>
#include <cstdlib>
#include <arpa/inet.h>
#include <typeinfo>
using namespace std;
int main(int argc, char* argv[])
{
int s, p, rb,rs;
int bytesend;
char buf[1024];
int len;
char ent[16];
char Porta[5];
unsigned long EndServ;
struct sockaddr_in UdpServer, UdpClient;
int UdpServerLen = sizeof(UdpServer);
//do text
string msg("The quick brown fox jumps over the lazy dog\n");
len = msg.copy(buf, msg.size(), 0);
buf[len] = '\0';
//do socket
s = socket(AF_INET, SOCK_DGRAM, 0);
if (s == -1){
cout << "No socket done\n";
}
else {
cout << "Socket done\n";
}
//populate UdpClient struct
UdpClient.sin_family = AF_INET;
UdpClient.sin_addr.s_addr = INADDR_ANY;
UdpClient.sin_port = 0;
//populate UdpServer struct
UdpServer.sin_family = AF_INET;
UdpServer.sin_addr.s_addr = inet_addr(argv[1]);
//check if addres is correct
cout << "ServerAddress: " << hex << UdpServer.sin_addr.s_addr << endl;
UdpServer.sin_port = htons(atoi(argv[2]));
//bind socket
rb = bind(s, (struct sockaddr *)&UdpClient, sizeof(UdpClient));
if (rb == 0){
cout << "Bind OK!\n";
}
else {
cout << "Bind NOK!!!\n";
close(s);
exit(1);
}
//send text to Server
cout << "UdpServSiz: " << sizeof(UdpServer) << endl;
rs = sendto(s, buf, 1024, 0, (struct sockaddr *)&UdpServer, sizeof(UdpServer));
if (rs == -1){
cout << "Message NOT sent!!!\n";
}
else {
cout << "Message SENT!!!\n";
}
close(s);
return 0;
}
/*
edison#edison-AO532h:~/CmmPGMs$ ./UdpClient 127.0.0.1 6789
Socket done
ServerAddress: 100007f (using htonl or not!!)
Bind OK!
Message SENT!!!
edison#edison-AO532h:~/CmmPGMs$
*/
Sounds like you're on ARM (Linaro)? In which case the endianness of the processor matches network order, so htonl and ntohl basically do nothing.