I am implementing a TFTP Client application in MFC. Below are two functions:
1) Connect to server function..
int TFTPClient::connectToServer() {
cout << "Connecting to " << server_ip << " on port " << server_port << endl;
socket_descriptor = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (socket_descriptor == -1) {
throw new ETFTPSocketCreate;
}
client_address.sin_family = AF_INET;
client_address.sin_port = htons(server_port); //- taip pat turi buti ir serveryje!
client_address.sin_addr.s_addr = inet_addr(this->server_ip);
#ifdef WIN32
//memset(client_address.sin_zero, 0, sizeof(client_address.sinzero);
//- suvienodinam SOCKADDR_IN ir SOCKADDR strukturu dydzius
#endif
connection = connect(socket_descriptor, (const struct sockaddr *)&client_address, sizeof(client_address));
if (connection != 0) {
cout << "Unable to connect to an address\n";
return -1;
}
DEBUGMSG("Successfully connected");
AfxMessageBox("Successfully connected");
return 1;
}
2) Wait for response...
int TFTPClient::waitForPacket(TFTP_Packet* packet, int timeout_ms) {
packet->clear();
fd_set fd_reader;
timeval connection_timer;
connection_timer.tv_sec = timeout_ms / 1000; // = 2
connection_timer.tv_usec = 0;
FD_ZERO(&fd_reader);
FD_SET(socket_descriptor, &fd_reader);
int select_ready = select(socket_descriptor + 1, &fd_reader, NULL, NULL, &connection_timer);
if (select_ready == -1) {
#ifdef WIN32
cout << "Error in select(), no: " << WSAGetLastError() << endl;
#else
cout << "Error in select(): " << endl;
#endif
return TFTP_CLIENT_ERROR_SELECT;
} else if (select_ready == 0) {
DEBUGMSG("Timeout");
return TFTP_CLIENT_ERROR_TIMEOUT;
}
int receive_status;
receive_status = recv(socket_descriptor, (char*)packet->getData(), TFTP_PACKET_MAX_SIZE, 0);
if (receive_status == 0) {
cout << "Connection was closed by server\n";
return TFTP_CLIENT_ERROR_CONNECTION_CLOSED;
}
if (receive_status == SOCKET_ERROR) {
DEBUGMSG("recv() error in waitForPackage()");
return TFTP_CLIENT_ERROR_RECEIVE;
}
packet->setSize(receive_status);
return TFTP_CLIENT_ERROR_NO_ERROR;
}
The select function in function 2 returns 0 every time I run the application, which indicates a timeout. My question: Is there another way I can implement this whole function without using select()? or is this function necessary before I call recv? I am not sure why there is a timeout and I am aware that I only need to receive an ack from the server before sending the next data packet and loop until the file is fully transferred.
If you only have a single file descriptor to worry about and you want to wait indefinitely, don't bother with select
. Just call recv
, which will block until you get data.