いつも楽しんでサイトを拝見させていただいております。 今UNIX上でクライアント、サーバのネットワークプログラミングをC言語で 挑戦しているところなんですが、FTPコマンド (QUIT,USER,PASS,PWD,STAT,MKD,RMD,CDW,DEL,LIST,HELPcmd,HELP,PORT,NLST) の組み込み方がイマイチよく分りません。 以下にserverftp.cとclientftp.cのコードを貼り付けておきます。 これらのコードは正常に動いております。 大変お忙しいとは思いますが、 ご教授の程、どうぞよろしくお願い致します。 serverftp.c *********************** #include <sys/types.h> #include <netinet/in.h> #include <sys/socket.h> #include <netdb.h> #define SERVER_FTP_PORT 9237 // list function prototypes int receiveMsg(int s, char *buf, short bufsize, short *msgsize); int sendMessage(int s, char *buf, short size); main( int argc, char *argv[] ) { int listenSocket; int s; int status; short msgSize; printf("Starting server ftp\n"); status = svcInitServer(&listenSocket); if(status != 0) { printf("Exiting server ftp due to error\n"); exit(-1); } for(;;) { printf("waiting to accept client connection\n"); s = accept(listenSocket, NULL, NULL); if(s < 0) { perror("cannot accept connection: "); exit(-1); } int msg; short size; char recvBuffer[1024]; printf("Server connected to client, issuing recvmsg\n"); status = receiveMsg (s, recvBuffer, 1024, &msgSize); if (status < 0) { close(s); return(status); } char replyMsg[1024]; strcpy(replyMsg, "200 command ok\n"); size = strlen (replyMsg) + 1; status = sendMessage(s, replyMsg, size); if(status < 0) { close(s); return(s); } } close(listenSocket); printf("Exiting from server ftp main\n"); } int svcInitServer( int *s ) { int sock; struct sockaddr_in svcAddr; int qlen; if((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) { perror("cannot create socket "); return(-1); } memset((char *) &svcAddr, 0, sizeof(svcAddr)); svcAddr.sin_family = AF_INET; svcAddr.sin_addr.s_addr = htonl(INADDR_ANY); svcAddr.sin_port = htons(SERVER_FTP_PORT); if(bind(sock,(struct sockaddr *)&svcAddr, sizeof(svcAddr)) < 0) { perror("cannot bind "); close(sock); return(-1); } qlen = 1; listen(sock, qlen); *s = sock; return(0); } int sendMessage( int s, char *msg, short msgSize ) { int i; for(i = 0; i < msgSize; i++) { printf("%c", msg[i]); } printf(" \n"); if((send(s, msg, msgSize, 0)) < 0) { perror("unable to send "); return(-1); } return(0); } int receiveMsg( int s, char *buffer, short bufferSize, short *msgSize ) { int i; *msgSize = recv(s, buffer, bufferSize, 0); if(*msgSize < 0) { perror("unable to receive"); return(-1); } for(i = 0; i < *msgSize; i++) { printf("%c", buffer[i]); } printf("\n"); return(0); } clientftp.c ********************** #include <sys/types.h> #include <netinet/in.h> #include <sys/socket.h> #include <netdb.h> #define SERVER_FTP_PORT 9237 int sendMessage(int s, char *msg, short msgSize); int receiveMsg(int s, char *buffer, short bufferSize, short *msgSize); int main( int argc, char *argv[] ) { int status; int s; short msgSize; char FTPcmd[1024]; char replyMsg[1024]; printf("Trying to connect to server FTP\n"); status = clntConnect("//IPアドレス", &s); if(status < 0) { return(status); } printf("Connected to ftp server\n"); strcpy(FTPcmd, "quit"); status = sendMessage(s, FTPcmd, strlen(FTPcmd) + 1); if(status < 0) { close(s); return(s); } printf("Issuing receivemsg to get reply msg from server\n"); status = receiveMsg(s, replyMsg, 1024, &msgSize); if(status < 0) { close(s); return(s); } printf("%c", replyMsg); close(s); return(0); } int sendMessage(int s, char *msg, short msgSize) { int i; for(i = 0; i < msgSize; i++) { printf("%c", msg[i]); } printf(" \n"); if((send(s, msg, msgSize, 0)) < 0) { perror("unable to send "); return(-1); } return(0); } int receiveMsg(int s, char *buffer, short bufferSize, short *msgSize) { int i; *msgSize = recv(s, buffer, bufferSize, 0); if(*msgSize < 0) { perror("unable to receive "); return(-1); } for(i = 0; i < *msgSize; i++) { printf("%c", buffer[i]); } printf("\n"); return(0); } int clntConnect( char *serverName, int *s ) { int sock; struct sockaddr_in us; struct sockaddr_in them; struct hostent *he; if((he = gethostbyname(serverName)) == NULL) { printf("%s is unknown server\n", serverName); return(-1); } if((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) { perror("cannot create socket "); return(-1); } memset((char *) &us, 0, sizeof(us)); us.sin_family = AF_INET; us.sin_addr.s_addr = htonl(INADDR_ANY); us.sin_port = 0; if(bind(sock,(struct sockaddr *)&us, sizeof(us)) < 0) { perror("cannot bind"); close(sock); return(-1); } memset((char *)&them, 0, sizeof(them)); them.sin_family = AF_INET; memcpy((char *) &them.sin_addr, he->h_addr, he->h_length); them.sin_port = htons(SERVER_FTP_PORT); if(connect(sock,(struct sockaddr *)&them, sizeof(them)) < 0) { perror("cannot connect "); close(sock); return(-1); } printf("Connected to server, socket number: %d\n", sock); *s = sock; return(0); } void clntExtractReplyCode( char *buffer, int *replyCode ) { sscanf(buffer, "%d", replyCode); return; } |