当前位置: 技术问答>linux和unix
ftp client一问(一定给分,在线等待。)
来源: 互联网 发布时间:2015-01-02
本文导语: 以下是一个简单的测试程序。目的是为了。连接ftp server.然后登陆。 但是在获取了ftp server的欢迎信息后。用 char send_msg[] = "USER anonymouse"; if ((numreceive = send(socketfd, send_msg, sizeof(send_msg), 0)) == -1){ printf ("sen...
以下是一个简单的测试程序。目的是为了。连接ftp server.然后登陆。
但是在获取了ftp server的欢迎信息后。用
char send_msg[] = "USER anonymouse";
if ((numreceive = send(socketfd, send_msg, sizeof(send_msg), 0)) == -1){
printf ("send message errorn");
exit(-1);
}
if ((numreceive = recv(socketfd, buffer, sizeof(buffer), 0)) == -1){ //receive message from a socket
printf("receive message errorn");
exit(-1);
}
buffer_end = sizeof(buffer) + 1;
buffer[buffer_end] = '';
printf("%sn" , buffer);
却出现了问题。无法获得服务器的返回信息。高手给看看应该怎么写这个client才好!
///////////////////////////////////////////
///
#include
#include
#include
#include
#include
#include
#define PORT 21
int main(int argc, char *argv[])
{
int socketfd , numreceive;
struct hostent* server;
struct sockaddr_in server_addr;
char buffer[5000];
char hostname[] = "real.nwu.edu.cn";
if ((server = gethostbyname(hostname)) == NULL){ //DNS
printf("Get Host errorn");
exit(-1);
}
if ((socketfd = socket(AF_INET, SOCK_STREAM, 0)) == -1){ //initlization socket
printf("socket initliaztion errorn");
exit(-1);
}
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(PORT);
server_addr.sin_addr = *((struct in_addr*)server->h_addr);
bzero(&(server_addr.sin_zero) , 8);
if (connect(socketfd, (struct sockaddr *)&server_addr, sizeof(struct sockaddr)) == -1){ //connect;
printf("connect errorn");
exit(-1);
}
int i = 1;
while(i!=2){
if ((numreceive = recv(socketfd, buffer, sizeof(buffer), 0)) == -1){ //receive message from a socket
printf("receive message errorn");
exit(-1);
}
i++;
}
int buffer_end = sizeof(buffer) + 1;
buffer[buffer_end] = '';
printf("%sn" , buffer);
char send_msg[] = "USER anonymouse";
if ((numreceive = send(socketfd, send_msg, sizeof(send_msg), 0)) == -1){
printf ("send message errorn");
exit(-1);
}
if ((numreceive = recv(socketfd, buffer, sizeof(buffer), 0)) == -1){ //receive message from a socket
printf("receive message errorn");
exit(-1);
}
buffer_end = sizeof(buffer) + 1;
buffer[buffer_end] = '';
printf("%sn" , buffer);
close(socketfd);
return 0;
}
但是在获取了ftp server的欢迎信息后。用
char send_msg[] = "USER anonymouse";
if ((numreceive = send(socketfd, send_msg, sizeof(send_msg), 0)) == -1){
printf ("send message errorn");
exit(-1);
}
if ((numreceive = recv(socketfd, buffer, sizeof(buffer), 0)) == -1){ //receive message from a socket
printf("receive message errorn");
exit(-1);
}
buffer_end = sizeof(buffer) + 1;
buffer[buffer_end] = '';
printf("%sn" , buffer);
却出现了问题。无法获得服务器的返回信息。高手给看看应该怎么写这个client才好!
///////////////////////////////////////////
///
#include
#include
#include
#include
#include
#include
#define PORT 21
int main(int argc, char *argv[])
{
int socketfd , numreceive;
struct hostent* server;
struct sockaddr_in server_addr;
char buffer[5000];
char hostname[] = "real.nwu.edu.cn";
if ((server = gethostbyname(hostname)) == NULL){ //DNS
printf("Get Host errorn");
exit(-1);
}
if ((socketfd = socket(AF_INET, SOCK_STREAM, 0)) == -1){ //initlization socket
printf("socket initliaztion errorn");
exit(-1);
}
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(PORT);
server_addr.sin_addr = *((struct in_addr*)server->h_addr);
bzero(&(server_addr.sin_zero) , 8);
if (connect(socketfd, (struct sockaddr *)&server_addr, sizeof(struct sockaddr)) == -1){ //connect;
printf("connect errorn");
exit(-1);
}
int i = 1;
while(i!=2){
if ((numreceive = recv(socketfd, buffer, sizeof(buffer), 0)) == -1){ //receive message from a socket
printf("receive message errorn");
exit(-1);
}
i++;
}
int buffer_end = sizeof(buffer) + 1;
buffer[buffer_end] = '';
printf("%sn" , buffer);
char send_msg[] = "USER anonymouse";
if ((numreceive = send(socketfd, send_msg, sizeof(send_msg), 0)) == -1){
printf ("send message errorn");
exit(-1);
}
if ((numreceive = recv(socketfd, buffer, sizeof(buffer), 0)) == -1){ //receive message from a socket
printf("receive message errorn");
exit(-1);
}
buffer_end = sizeof(buffer) + 1;
buffer[buffer_end] = '';
printf("%sn" , buffer);
close(socketfd);
return 0;
}
|
char send_msg[] = "USER anonymouse";
USER anonymouse后面加回车:
char send_msg[] = "USER anonymousen";
USER anonymouse后面加回车:
char send_msg[] = "USER anonymousen";
|
同意x86的说法
说点无关的,"anonymouse"好象多了个e吧!
说点无关的,"anonymouse"好象多了个e吧!