日期:2014-05-16  浏览次数:20747 次

FTP服务器,客户端使用PASV无法连接上
头文件rdwr.h
C/C++ code
#include <unistd.h>
#include <errno.h>
#include <stdio.h>

ssize_t readline(int fd, void *vptr, size_t maxlen)
{
    ssize_t n, rc;
    char c;
    char *ptr;

    ptr = vptr;
    for (n = 0; n < maxlen; n++) {
    again:
        if ((rc = read(fd, &c, 1)) == 1) {
            *ptr++ = c;
            if (c == '\n')
                break;
        } else if (rc == 0) {
            *ptr = 0;
            return -1;
        } else {
            if (errno == EINTR)
                goto again;
            return -1;
        }
    }
    *ptr = 0;
    return n;
}

ssize_t readn(int fd, void *vptr, size_t n)
{
    size_t nleft;
    ssize_t nread;
    char *ptr;

    ptr = vptr;
    nleft = n;
    while (nleft > 0) {
        if ((nread = read(fd, ptr, nleft)) < 0) {
            if (errno == EINTR)
                nread = 0;
            else
                return -1;
        } else if (nread == 0) {
            break;
        }
        nleft -= nread;
        ptr += nread;
    }
    return(n - nleft);
}

ssize_t writen(int fd, void *vptr, size_t n)
{
    size_t nleft;
    ssize_t nwritten;
    const char *ptr;

    ptr = vptr;
    nleft = n;
    while (nleft > 0) {
        if ((nwritten = write(fd, ptr, nleft)) <= 0) {
            if (nwritten < 0 && errno == EINTR)
                nwritten = 0;
            else
                return -1;
        }
        nleft -= nwritten;
        ptr += nwritten;
    }
    return(n - nleft);
}

ssize_t wr_and_rd(int fd, void *buf, size_t bufsize)
{
    size_t nleft;
    ssize_t n;

    if (writen(fd, buf, bufsize) < 0)
        return -1;
    n = read(fd, buf, bufsize);
    return n;
}


头文件sock.h
C/C++ code
#include <sys/socket.h>
#include <sys/types.h>
#include <strings.h>
#include <netdb.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdlib.h>

#include <stdio.h>

#define SA struct sockaddr

int create_client_fd(const char *hostname, int port)
{
    int clientfd;
    struct hostent *hostinfo;
    struct sockaddr_in srv;
    struct sockaddr_in cli;

    if ((clientfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
        return -1;
    if ((hostinfo = gethostbyname(hostname)) == NULL)
        return -2;
    bzero(&srv, sizeof(srv));
    srv.sin_family = AF_INET;
    srv.sin_port = htons(port);
    srv.sin_addr = *(struct in_addr *)*hostinfo->h_addr_list;
    if (connect(clientfd, (SA *)&srv, sizeof(srv)) < 0) {
        close(clientfd);
        return -1;
    }
    return clientfd;
}

int create_listen_fd(int port, int listen_queue)
{
    int listenfd, optval = 1;
    struct sockaddr_in srv;

    if ((listenfd = socket(AF_INET, SOCK_STREAM ,0)) < 0)
        return -1;
    if (setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, 
                    (const void *)&optval, sizeof(int)) < 0)
        return -1;
    bzero(&srv, sizeof(srv));
    srv.sin_family = AF_INET;
    srv.sin_addr.s_addr = htonl(INADDR_ANY);
    srv.sin_port = htons((unsigned short)port);
    if (bind(listenfd, (SA *)&srv, sizeof(srv)) < 0)
        return -1;
    if (listen(listenfd, listen_queue) < 0)
        return -1;
    return listenfd;
}

int create_connection(struct sockaddr *srv, size_t srv_len, int port)
{
    int fd;

    if ((fd = socket(AF_INET, SOCK_STREAM ,0)) < 0)
        return -1;
    if (connect(fd, (SA *)&srv, srv_len) < 0)
        return -1;
    return fd;
}


server.h
C/C++ code
int do_login(in