vc++ - socket is not writing into the port
I am trying to create a ftp client with uploading a file capability. But the socket is not sending the message into the server, even though it got the connection with the server. See the code below
#include <iostream>
#include <string>
#include <tchar.h>
#include <Winsock2.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdio.h>
#include <stdlib.h>
int main(int argc,wchar_t * argv[])
{
WSADATA wsaData = {0};
int iResult = 0;
iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (iResult != 0)
{
wprintf(L"WSAStartup failed: %d\n", iResult);
return 1;
}
SOCKET sock= socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
if (sock == INVALID_SOCKET)
wprintf(L"socket function failed with error = %d\n", WSAGetLastError() );
else
std::wcout << " Success occured in socket creation " << std::endl ;
SOCKADDR_IN sock_Addr={0};
sock_Addr.sin_family=AF_INET;
sock_Addr.sin_port=htons(21);
sock_Addr.sin_addr.S_un.S_addr=inet_addr("10.0.0.105");
char recv_Buf[1000]={'\0'};
if(connect(sock,(SOCKADDR *)&sock_Addr,sizeof(SOCKADDR_IN)) != 0)
std::cout << " Error in connection " << std::endl ;
else
{
std::string tmp;
u_long mode =1; // non blocking socket
ioctlsocket(sock,FIONBIO,&mode);
int result = recv(sock,recv_Buf,sizeof(recv_Buf),0);
while( result > 0 )
{
tmp=tmp+recv_Buf;
result = recv(sock,recv_Buf,sizeof(recv_Buf),0);
}
std::cout << "connection - success - " << tmp << std::endl ;
}
ioctlsocket(sock,FIONBIO,0);
st开发者_StackOverflow中文版d::string buf="USER administrator";
if(send(sock,buf.c_str(),buf.size(),0)== SOCKET_ERROR)
std::cout << " sending user - error " << WSAGetLastError() << std::endl ;
else
{
std::string tmp;
u_long mode =1; // non blocking socket
ioctlsocket(sock,FIONBIO,&mode);
int result = recv(sock,recv_Buf,sizeof(recv_Buf),0);
while( result > 0 )
{
tmp=tmp+recv_Buf;
result = recv(sock,recv_Buf,sizeof(recv_Buf),0);
std::cout << "received " << recv_Buf << std::endl ;
}
std::cout << "user - success - " << tmp << std::endl ;
}
}
Note : I am using filezilla To check this . It will show the incoming connections and the commands. So if anybody guide me in this issue, I will be thankful to you.
Edit Below is the client side(my side) command prompt result
Success occured in socket creation
connection - success - 220-FileZilla Server version 0.9.37 beta
220-written by Tim Kosse (Tim.Kosse@gmx.de)
220 Please visit http://sourceforge.net/projects/filezilla/
user - success -
Press any key to continue . . .
This one was displayed at the server side
(000007)5/17/2011 23:32:02 PM - (not logged in) (10.0.0.105)> Connected, sending welcome message...
(000007)5/17/2011 23:32:02 PM - (not logged in) (10.0.0.105)> 220-FileZilla Server version 0.9.37 beta
(000007)5/17/2011 23:32:02 PM - (not logged in) (10.0.0.105)> 220-written by Tim Kosse (Tim.Kosse@gmx.de)
(000007)5/17/2011 23:32:02 PM - (not logged in) (10.0.0.105)> 220 Please visit http://sourceforge.net/projects/filezilla/
(000007)5/17/2011 23:32:02 PM - (not logged in) (10.0.0.105)> disconnected.
FTP commands are terminated by a carriage return - line feed pair (\r\n). You need to add \r\n to your USER command string.
Edit: you should consult RFC 959 for more information (there's more FTP-related RFCs that update and extend RFC 959 too). It has a section detailing the base FTP commands and shows the expected format, including <CRLF> pair.
精彩评论