代码: 全选
/* header files */
#include <stdio.h>
#include <stdlib.h>
#include <netdb.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/stat.h>
//#include <netinet.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <fcntl.h>
#include<string.h>
#include <netdb.h> /* getservbyname(), gethostbyname() */
#include <errno.h> /* for definition of errno */
/* define macros*/
#define MAXBUF 1024
#define STDIN_FILENO 0
#define STDOUT_FILENO 1
/* define FTP reply code */
#define USERNAME 220
#define PASSWORD 331
#define LOGIN 230
#define PATHNAME 257
#define CLOSEDATA 226
#define ACTIONOK 250
/* DefinE global variables */
char *host; /* hostname or dotted-decimal string */
char *port;
char *rbuf, *rbuf1; /* pointer that is malloc'ed */
char *wbuf, *wbuf1; /* pointer that is malloc'ed */
struct sockaddr_in servaddr;
//int cliopen(char *host, char *port);
//void strtosrv(char *str, char *host, char *port);
void cmd_tcp(int sockfd);
//void ftp_list(int sockfd);
//int ftp_get(int sck, char *pDownloadFileName_s);
//int ftp_put (int sck, char *pUploadFileName_s);
int main(int argc, char *argv[])
{
struct hostent *hptr;
int fd;
if (0 != argc-2)
{
printf("%s\n","missing <hostname>");
exit(0);
}
port = 21;
if (argc!=2) return 1;
if ((hptr=gethostbyname(argv[1]))==NULL)
{
printf("gethostbyname error");
return 2;
}
host = inet_ntoa(*((struct in_addr *)hptr->h_addr));
printf("Host ip: %s\n",inet_ntoa(*((struct in_addr *)hptr->h_addr)));
//host = argv[1];
struct sockaddr_in servaddr;
char mybuffer[256];
if((fd=socket(AF_INET,SOCK_STREAM,0))<0)
{
printf("error in socket\n");
exit(1);
}
else
{
printf("socket created .\n");
printf("socked id: %d \n",fd);
}
bzero(&servaddr,sizeof(servaddr));
servaddr.sin_family=AF_INET;
servaddr.sin_port=htons(port);
servaddr.sin_addr.s_addr=inet_addr(host);
printf("connecting to %s ...",host);
//printf("%s \n",htons(port));
if(connect(fd,&servaddr,sizeof(servaddr))<0)
{
printf("connect fail\n");
exit(1);
}
else
{
printf("connect ok!\n");
}
cmd_tcp(fd);
if (close(fd) < 0)
printf("close error\n");
//exit(0);
}
void cmd_tcp(int sockfd)
{
int maxfdp1, nread, nwrite, fd, replycode;
char host[16];
char port[6];
fd_set rset;
char mybuffer[1024];
FD_ZERO(&rset);
maxfdp1 = sockfd + 1; /* check descriptors [0..sockfd] */
for (;;)
{
FD_SET(STDIN_FILENO, &rset);
FD_SET(sockfd, &rset);
if (select(maxfdp1, &rset, NULL, NULL, NULL) < 0)
printf("select error\n");
if (FD_ISSET(STDIN_FILENO, &rset))
{
if ( (nread = read(STDIN_FILENO, rbuf, MAXBUF)) < 0)
printf("read error from stdin\n");
nwrite = nread+5;
/* send username */
if (replycode == USERNAME)
{
sprintf(wbuf, "USER %s", rbuf);
if (write(sockfd, wbuf, nwrite) != nwrite)
printf("write error\n");
}
}
if (FD_ISSET(sockfd, &rset))
{
if ( (nread = recv(sockfd,mybuffer, sizeof(mybuffer), 0)) < 0)
printf("recv error\n");
else
{
printf("%s",mybuffer);
if (nread == 0)
break;
}
/* set replycode and wait for user's input */
if (strncmp(rbuf, "220", 3)==0 || strncmp(rbuf, "530", 3)==0){
strcat(rbuf, "your name: ");
nread += 12;
replycode = USERNAME;
}
/*************************************************************
// 9. code here: handle other response coming from server
*************************************************************/
/* open data connection
if (strncmp(rbuf, "227", 3) == 0)
{
strtosrv(rbuf, host, port);
fd = cliopen(host, port);
write(sockfd, wbuf1, nwrite);
nwrite = 0;
}
*/
/* start data transfer */
//if (write(STDOUT_FILENO, rbuf, nread) != nread)
// printf("write error to stdout\n");
}
if (close(sockfd) < 0)
printf("close error\n");
}
}
代码: 全选
if (FD_ISSET(STDIN_FILENO, &rset))
{
if ( (nread = read(STDIN_FILENO, rbuf, MAXBUF)) < 0)
printf("read error from stdin\n");
nwrite = nread+5;
/* send username */
if (replycode == USERNAME)
{
sprintf(wbuf, "USER %s", rbuf);
if (write(sockfd, wbuf, nwrite) != nwrite)
printf("write error\n");
}
}