Reputation: 4275
I'm trying to code a simple FTP client with Winsock.
I have the following code:
using namespace std;
#include <iostream>
#include <cstring>
#include <cstdio>
#include <winsock.h>
#include <windows.h>
int main() {
const int MAX_TRIES = 10;
char * host = "localhost";
int port = 21;
char * userName = "b8_8780454";
char * pass = "test";
char * testFileSource = "C:\\Windows\\notepad.exe";
WSADATA WSAData;
SOCKADDR_IN server;
SOCKET sock;
WSAStartup(MAKEWORD(2,2), &WSAData);
sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
if (sock == INVALID_SOCKET) {
cout<<"fail";
return -1;
}
server.sin_family = PF_INET;
server.sin_port = htons(port);
server.sin_addr = *((struct in_addr *)gethostbyname(host)->h_addr);
memset(server.sin_zero,0,8);
int errorCode = connect(sock, (LPSOCKADDR)&server, sizeof(struct sockaddr));
int tries = 0;
while (errorCode == SOCKET_ERROR) {
if (tries >= MAX_TRIES) {
cout<<"fail 2";
return -1;
}
errorCode = connect(sock, (LPSOCKADDR)&server, sizeof(struct sockaddr));
tries++;
}
char serverMsg[2048];
Sleep(1000);
cout<<"Waiting for server response..."<<endl;
int r = recv(sock,serverMsg,2048,0);
serverMsg[r] = '\0';
cout<<endl<<endl<<"Server said: "<<endl<<serverMsg<<endl<<endl;
char userB[1024] = "USER ";
strcat(userB,userName);
cout<<"Sending... "<<userB<<endl;
cout<<"sended: "<<send(sock, userB, strlen(userB), 0)<<" bytes"<<endl;
Sleep(1000);
cout<<"Waiting for server response..."<<endl;
serverMsg[0] = '\0';
recv(sock,serverMsg,2048,0); // <-- program keeps lock here
cout<<endl<<endl<<"Server said: "<<endl<<serverMsg<<endl<<endl;
getchar();
return 0;
}
I think the send is not working properly, nevertheless it is returning >0 but on the server side i can't see this client sending any data. I think i maybe a problem with the conection setup, but i have been checking some sites and I am not able to catch the error
This is what the program prints:
Waiting for server response...
Server said:
220-FileZilla Server version 0.9.31 beta
220-written by Tim Kosse ([email protected])
220 Please visit http://sourceforge.net/projects/filezilla/
Sending... USER b8_8780454
sended: 15 bytes
Waiting for server response...
Server said:
421 Login time exceeded. Closing control connection.
by Tim Kosse ([email protected])
220 Please visit http://sourceforge.net/projects/filezilla/
In my FTP server I can't see this client sending any data to the server. Any clue?
Upvotes: 1
Views: 1908
Reputation: 26930
You need a new line (\n
) after your username.
strcat(userB,userName);
strcat(userB,"\n");
Upvotes: 2