My question is this: During TFTP, when a client connects to a server, the default port for intitial data transmission is port 69. When the server receives a WRQ (write request) packet from the client, it returns an ACK(acknowledgement) packet. However this ACK packet is sent via a different port back to the client. below is a function which I am using to connect to the server. Note that server_port is 69.
int TFTPClient::connectToServer() {
socket_descriptor = socket(PF_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);
client_address.sin_addr.s_addr = inet_addr(this->server_ip);
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;
}
int TFTPClient::sendPacket(TFTP_Packet* packet) {
return send(socket_descriptor, (char*)packet->getData(), packet->getSize(), 0);
}
int TFTPClient::waitForPacket(TFTP_Packet* packet, int timeout_ms) {
packet->clear();
int receive_status;
int iResult;
u_long iMode = 0;
//-----------------------------------------------
// Set the socket I/O mode: In this case FIONBIO
// enables or disables the blocking mode for the
// socket based on the numerical value of iMode.
// If iMode = 0, blocking is enabled;
// If iMode != 0, non-blocking mode is enabled.
//-----------------------------------------------
iResult = ioctlsocket(socket_descriptor, FIONBIO, &iMode);
if (iResult != NO_ERROR){
printf("ioctlsocket failed with error: %ld\n", iResult);
}
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;
}
I am using Wire Shark to determine data transmission over the socket. I am sending a WRQ packet which is successfully reaching the server. An ACK packet(acc. to Wireshark) is sent back to the client successfully(on a different port). However, my application hangs indefinitely when I hit the 'recv()' function. I fear that this may be due to the fact that I am calling the same socket('socket_descriptor') in the recv() function that I called in the send() function. Does the port 69 specification of 'socket_descriptor' cause this application stall?? Or does recv() accept packets from the server IP over other ports?? Can anyone shed light on this please??