Please help me with problem. I want to program Ftp client. I use non-blocking socket.
The initial settings of the socket and connect to Ftp are shown below.
SOCKET make_socket_ftp(const IPN ip, int port, int tm_conn) { SOCKET newSocket = INVALID_SOCKET; int operationResult; struct sockaddr_in serverAddr; int addrLen; bzero(&serverAddr, sizeof(serverAddr)); newSocket = socket(AF_INET, SOCK_STREAM, 0); if (newSocket != INVALID_SOCKET) { serverAddr.sin_family = AF_INET; serverAddr.sin_addr.s_addr = ip; serverAddr.sin_port = htons(port); addrLen = sizeof(serverAddr); int blockingState = 0; operationResult = setsockopt(newSocket, SOL_SOCKET, SO_BLOCKING, &blockingState, sizeof( blockingState)); if (operationResult == 0 ) //success setsockopt { operationResult = connect(newSocket, (PSA) &serverAddr, addrLen); if (operationResult == 0) //success connect { return (newSocket); } if (fdError() == EINPROGRESS) { fd_set wrsd; struct timeval tv; int maxfd = 2; FD_ZERO(&wrsd); FD_SET(newSocket, &wrsd); tv.tv_sec = tm_conn; tv.tv_usec = 0; operationResult = fdSelect(maxfd, NULL, &wrsd, NULL, &tv ); if (operationResult > 0) { int err, lenErr; lenErr = sizeof(err); operationResult = getsockopt(newSocket, SOL_SOCKET, SO_ERROR, &err, &lenErr); if ((operationResult == 0) && // success getsockopt (err == 0)) { return newSocket; } } } } } if (newSocket != INVALID_SOCKET) fdClose(newSocket); return (INVALID_SOCKET); }
function for receiving data through socket here:
int readSocketData(SOCKET socket, char *buf, int sizeBuf, int timeoutValSec) { fd_set rdsd; int operationResult; struct timeval tv; operationResult = recv(socket, buf, sizeBuf, 0); if (operationResult >= 0) return(operationResult); if (fdError() == EWOULDBLOCK) { int maxfd = 2; FD_ZERO(&rdsd); FD_SET(socket, &rdsd); tv.tv_sec = timeoutValSec; tv.tv_usec = 0; operationResult = fdSelect(maxfd, &rdsd, NULL, NULL, &tv); if (operationResult > 0) { operationResult = recv(socket, buf, sizeBuf, 0); if (operationResult >= 0) return(operationResult); } } return(-1); }
function for sending data here:
int writeSocketData(SOCKET socket, char *buf, int sizeBuf, int timeoutValSec) { fd_set wrsd; int operationResult; struct timeval tv; operationResult = send(socket, buf, sizeBuf, MSG_DONTWAIT); if (operationResult >= 0) { printf("sended %d bytes\n", operationResult); return (operationResult); } if (fdError() == EWOULDBLOCK) { int maxfd = 2; FD_ZERO(&wrsd); FD_SET(socket, &wrsd); tv.tv_sec = timeoutValSec; tv.tv_usec = 0; operationResult = fdSelect(maxfd, NULL, &wrsd, NULL, &tv ); if(operationResult > 0) { operationResult = send(socket, buf, sizeBuf, 0); if(operationResult >= 0) { error = 0; setsockopt(socket, SOL_SOCKET, SO_BLOCKING, &error, err_len ); return (operationResult); } } } else { printf("error in write data to socket"); } return (-1); }
Connection to the server and receiving welcome message are occured successfully. And then when I sent request for login ftp (ex. "USER mc\n") to external FTP server, data don't come to server and in console I had retransmit timeout. If I send request to local server, I don't have this problem.The difference between the external and local server consists only in varying workload.
What could be the reason?