|
这个小程序,怎么才会结束?read时在什么时候结束的?
/* ftp.c - ftp , main */
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
extern int errno;
int TCPftp(const char *host, const char *service);
int errexit(const char *format, ...);
int connectTCP(const char *host, const char *service);
#define LINELEN 128
#define MSG "anonymous"
/*------------------------------------------------------------------------
* main - TCP client for DAYTIME service
*------------------------------------------------------------------------
*/
int
main(int argc, char *argv[])
{
char *host = "localhost"; /* host to use if none supplied */
char *service = "daytime"; /* default service port */
switch (argc) {
case 1:
host = "localhost";
break;
case 3:
service = argv[2];
/* FALL THROUGH */
case 2:
host = argv[1];
break;
default:
fprintf(stderr, "usage: TCPdaytime [host [port]]\n");
exit(1);
}
TCPftp(host, service);
exit(0);
}
/*------------------------------------------------------------------------
* TCPftp - invoke ftp on specified host and print results
*------------------------------------------------------------------------
*/
int
TCPftp(const char *host, const char *service)
{
char buf[LINELEN+1]; /* buffer for one line of text */
int s, n; /* socket, read count */
s = connectTCP(host, service);
while( (n = read(s, buf, LINELEN)) > 0) {
buf[n] = '\0'; /* ensure null-terminated */
(void) fputs( buf, stdout );
}
/* write(s,MSG,strlen(MSG));
while( (n = read(s, buf, LINELEN)) > 0) {
buf[n] = '\0';
(void) fputs( buf, stdout );
}*/
return 0;
}
/*------------------------------------------------------------------------
* connectTCP - connect to a specified TCP service on a specified host
*------------------------------------------------------------------------
*/
int
connectTCP(const char *host, const char *service )
/*
* Arguments:
* host - name of host to which connection is desired
* service - service associated with the desired port
*/
{
return connectsock( host, service, "tcp");
} |
|