关于FTP的实现(请大神指导)

Bittersweet_y 2014-03-13 03:54:54
请问,如何用getopt()函数解析命令参数,监听IP地址和端口号,而且如何传参??
我的客户端是用filezilla登陆的,一个FTP工具,现在要写一个服务端,如何使用getopt()来监听呢???

int main()
{
int client_sock =create_sock(...);
bind(..);
listen(..);
while(1)
{
accept(..);
create_thread();
}
close(..);
}

各函数实现:
void create_sock()
{
...
}

void bind()
{
struct sockaddr_in server;
server.sin_port = htons(...);
server.sin_addr.s_addr=htonl(...)

}

void listen()
{
...
}

void accept()
{
...
}

void create_thread()
{
...
}
...全文
127 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
Bittersweet_y 2014-03-24
  • 打赏
  • 举报
回复
这个只能在main里面实现吗?? 能不能封装起来的???还是说本来就这样实现??
mymtom 2014-03-14
  • 打赏
  • 举报
回复
是想问怎么用getopt吗?

/**
 * @file    foo.c
 * @brief   
 */

#include <unistd.h>

#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

static void
usage(void)
{
    fprintf(stderr, "usage: demo -h host -p port\n");
    exit(1);
}

int main(int argc, char *argv[])
{
    int ch; 
    int port = 0;
    char host[HOST_NAME_MAX + 1] = "";

    while ((ch = getopt(argc, argv, "h:p:")) != -1) {
        switch (ch) {
        case 'h':
            strcpy(host, optarg);
            break;
        case 'p':
            port = strtol(optarg, NULL, 10);
            break;
        default:
            usage();
            break;
        }
    }

    printf("host=%s port=%d\n", host, port);

    return 0;
}

23,125

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧