vc++ - 套接字未写入端口
我正在尝试创建一个具有上传文件功能的 ftp 客户端。但是套接字没有将消息发送到服务器,即使它与服务器建立了连接。请参阅下面的代码
#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);
std::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 ;
}
}
注意:我正在使用 filezilla 来检查这一点。它将显示传入的连接和命令。 因此,如果有人在这个问题上指导我,我将非常感谢你。
编辑 下面是客户端(我这边)命令提示符结果
成功创建套接字
连接 - 成功 - 220-FileZilla Server 版本 0.9.37 beta
220-writing作者:Tim Kosse ([电子邮件受保护])
220 请访问http://sourceforge.net/projects/filezilla/
用户 - 成功 -
按任意键继续。 。 .
服务器端显示
(000007)5/17/2011 23:32:02 PM - (未登录) (10.0.0.105)>已连接,正在发送欢迎消息...
(000007)5/17/2011 23:32:02 PM -(未登录)(10.0.0.105)> 220-FileZilla Server 版本 0.9.37 beta
(000007)5/17/2011 23:32:02 PM -(未登录)(10.0.0.105)> 220-作者:Tim Kosse ([电子邮件受保护])
(000007)5 /17/2011 23:32:02 PM - (未登录)(10.0.0.105)> 220 请访问http://sourceforge.net/projects/filezilla/
(000007)5/17/ 2011 23:32:02 PM -(未登录)(10.0.0.105)>已断开连接。
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);
std::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 ([email protected])
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 ([email protected])
(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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
FTP 命令以回车换行对 (\r\n) 终止。您需要将 \r\n 添加到 USER 命令字符串中。
编辑:您应该查阅 RFC 959 了解更多信息(还有更多与 FTP 相关的 RFC 会更新)并扩展 RFC 959)。它有一个部分详细介绍了基本 FTP 命令并显示了预期的格式,包括和。一对。
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.