- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我正在尝试为多线程服务器编写 TCP FTP 服务。我找到了这个教程 http://www.mario-konrad.ch/wiki/doku.php?id=programming:multithreading:tutorial-04这对理解TCP协议(protocol)的客户端多线程非常有帮助。事实上,这段代码创建了一个可以同时接受来自不同客户端的多个连接的服务器。
但是,我正在努力寻找如何在其上应用 ftp 服务。精确地说明如何向 FTP 服务器发送文件和从 FTP 服务器获取文件。
有什么帮助吗?
最佳答案
下面的代码是我去年为一篇网络论文所做的作业。希望对您有所帮助。
#include <windows.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <winsock.h>
#include <winsock2.h>
#define BUFFERSIZE 800
#define WSVERS MAKEWORD(2,0)
WSADATA wsadata;
void wy_fileName_collector(char *_buffer, char *_nameBuffer);
int main(int argc, char *argv[])
{
//INITIALIZATION
struct sockaddr_in localaddr,remoteaddr;
struct sockaddr_in remoteaddr_act;
SOCKET s,ns;
SOCKET s_data_act=0;
char send_buffer[BUFFERSIZE],receive_buffer[BUFFERSIZE];
char fileName[40];
int n,bytes,addrlen;
memset(&localaddr,0,sizeof(localaddr));//clean up the structure
memset(&localaddr,0,sizeof(remoteaddr));//clean up the structure
//WSASTARTUP
if (WSAStartup(WSVERS, &wsadata) != 0)
{
WSACleanup();
printf("WSAStartup failed\n");
exit(1);
}
//SOCKET
s = socket(PF_INET, SOCK_STREAM, 0);
if (s <0)
{
printf("socket failed\n");
}
localaddr.sin_family = AF_INET;
if (argc == 2) localaddr.sin_port = htons((u_short)atoi(argv[1]));
else localaddr.sin_port = htons(1234);//default listening port
localaddr.sin_addr.s_addr = INADDR_ANY;//server address should be local
//BIND
if (bind(s,(struct sockaddr *)(&localaddr),sizeof(localaddr)) != 0)
{
printf("Bind failed!\n");
exit(0);
}
//LISTEN
listen(s,5);
while (1)
{
addrlen = sizeof(remoteaddr);
//NEW SOCKET newsocket = accept
ns = accept(s,(struct sockaddr *)(&remoteaddr),&addrlen);
if (ns <0 ) break;
printf("accepted a connection from client IP %s port %d \n",inet_ntoa(remoteaddr.sin_addr),ntohs(localaddr.sin_port));
//Respond with welcome message
sprintf(send_buffer,"\n==220 Welcome to Alan's FTP site== \r\n\n");
bytes = send(ns, send_buffer, strlen(send_buffer), 0);
while (1)
{
n = 0;
while (1)
{
//RECEIVE
bytes = recv(ns, &receive_buffer[n], 1, 0);//receive byte by byte...
//PROCESS REQUEST
if ( bytes <= 0 ) break;
if (receive_buffer[n] == '\n')
{ /*end on a LF*/
receive_buffer[n] = '\0';
break;
}
if (receive_buffer[n] != '\r') n++; /*ignore CRs*/
}
if ( bytes <= 0 ) break;
printf("-->DEBUG: the message from client reads: '%s' \r\n", receive_buffer);
/**
* @brief Exception handling
*/
if(strncmp(receive_buffer,"USER",4) && strncmp(receive_buffer,"PASS",4) && strncmp(receive_buffer,"SYST",4)
&&strncmp(receive_buffer,"PORT",4) && strncmp(receive_buffer,"STOR",4) && strncmp(receive_buffer,"RETR",4)
&&strncmp(receive_buffer,"LIST",4) && strncmp(receive_buffer,"NLST",4) && strncmp(receive_buffer,"QUIT",4))
{
sprintf(send_buffer,"202 Command not implemented, superfluous at this site. \r\n");
bytes = send(ns, send_buffer, strlen(send_buffer), 0);
}
if (strncmp(receive_buffer,"USER",4)==0)
{
printf("Logging in \n");
sprintf(send_buffer,"331 Password required \r\n");
bytes = send(ns, send_buffer, strlen(send_buffer), 0);
if (bytes < 0) break;
}
if (strncmp(receive_buffer,"PASS",4)==0)
{
printf("Typing password (anything will do... \n");
sprintf(send_buffer,"230 Public login sucessful \r\n");
bytes = send(ns, send_buffer, strlen(send_buffer), 0);
if (bytes < 0) break;
}
if (strncmp(receive_buffer,"SYST",4)==0)
{
system("ver > tmp.txt");
FILE *fin=fopen("tmp.txt","r");//open tmp.txt file
sprintf(send_buffer,"150 Transfering... \r\n");
bytes = send(ns, send_buffer, strlen(send_buffer), 0);
char temp_buffer[80];
while (!feof(fin))
{
fgets(temp_buffer,78,fin);
sprintf(send_buffer,"%s",temp_buffer);
send(s_data_act, send_buffer, strlen(send_buffer), 0);
}
fclose(fin);
sprintf(send_buffer,"226 File transfer completed... \r\n");
bytes = send(ns, send_buffer, strlen(send_buffer), 0);
if (bytes < 0) break;
}
//PORT
if(strncmp(receive_buffer,"PORT",4)==0)
{
s_data_act = socket(AF_INET, SOCK_STREAM, 0);
//local variables
unsigned char act_port[2];
int act_ip[4], port_dec;
char ip_decimal[40];
sscanf(receive_buffer, "PORT %d,%d,%d,%d,%d,%d",&act_ip[0],&act_ip[1],&act_ip[2],&act_ip[3],(int*)&act_port[0],(int*)&act_port[1]);
remoteaddr_act.sin_family=AF_INET;//local_data_addr_act
sprintf(ip_decimal, "%d.%d.%d.%d", act_ip[0], act_ip[1], act_ip[2],act_ip[3]);
printf("IP is %s\n",ip_decimal);
remoteaddr_act.sin_addr.s_addr=inet_addr(ip_decimal);
port_dec=act_port[0]*256+act_port[1];
printf("port %d\n",port_dec);
remoteaddr_act.sin_port=htons(port_dec);
if (connect(s_data_act, (struct sockaddr *)&remoteaddr_act, (int) sizeof(struct sockaddr)) != 0)
{
printf("trying connection in %s %d\n",inet_ntoa(remoteaddr_act.sin_addr),ntohs(remoteaddr_act.sin_port));
sprintf(send_buffer, "425 Something is wrong, can't start the active connection... \r\n");
bytes = send(ns, send_buffer, strlen(send_buffer), 0);
closesocket(s_data_act);
}
else
{
sprintf(send_buffer, "200 Ok\r\n");
bytes = send(ns, send_buffer, strlen(send_buffer), 0);
printf("Data connection to client created (active connection) \n");
}
}
/**
* @brief STOR
*/
if((strncmp(receive_buffer,"STOR",4)==0))
{
memset(&fileName, 0, strlen(fileName));
wy_fileName_collector(receive_buffer, fileName);
if(fopen(fileName,"w") == NULL)
{
printf("@alan:line1\n");
sprintf(send_buffer,"450 Requested file action not taken. \r\n");
bytes = send(ns, send_buffer, strlen(send_buffer), 0);
}
else
{
FILE *fout=fopen(fileName,"w");
sprintf(send_buffer,"150 File status okay; about to open data connection. \r\n");
bytes = send(ns, send_buffer, strlen(send_buffer), 0);
while(1)
{
n = 0;
while(1)
{
//RECEIVE
bytes = recv(s_data_act, &receive_buffer[n], 1, 0);//receive byte by byte...
//PROCESS REQUEST
if ( bytes <= 0 ) break;
if (receive_buffer[n] == '\n')
{ /*end on a LF*/
receive_buffer[n] = '\0';
break;
}
if (receive_buffer[n] != '\r') n++; /*ignore CRs*/
}
if ( bytes <= 0 ) break;
fprintf(fout,"%s\n",receive_buffer);
}
sprintf(send_buffer,"226 File transfer completed... \r\n");
bytes = send(ns, send_buffer, strlen(send_buffer), 0);
fclose(fout);
closesocket(s_data_act);
}
}
/**
* @brief RETR
*/
if((strncmp(receive_buffer,"RETR",4)==0))
{
memset(&fileName, 0, strlen(fileName));
wy_fileName_collector(receive_buffer, fileName);
if(fopen(fileName,"r")==NULL)
{
sprintf(send_buffer,"450 Requested file action not taken. \r\n");
bytes = send(ns, send_buffer, strlen(send_buffer), 0);
}
else
{
FILE *fin=fopen(fileName,"r");//open tmp.txt file
sprintf(send_buffer,"150 Transfering... \r\n");
bytes = send(ns, send_buffer, strlen(send_buffer), 0);
char temp_buffer[80];
while (!feof(fin))
{
fgets(temp_buffer,78,fin);
sprintf(send_buffer,"%s",temp_buffer);
send(s_data_act, send_buffer, strlen(send_buffer), 0);
}
fclose(fin);
sprintf(send_buffer,"226 File transfer completed... \r\n");
bytes = send(ns, send_buffer, strlen(send_buffer), 0);
}
closesocket(s_data_act);
}
//LIST or NLST
if ( (strncmp(receive_buffer,"LIST",4)==0) || (strncmp(receive_buffer,"NLST",4)==0))
{
system("dir > tmp.txt");
FILE *fin=fopen("tmp.txt","r");//open tmp.txt file
sprintf(send_buffer,"150 Transfering... \r\n");
bytes = send(ns, send_buffer, strlen(send_buffer), 0);
char temp_buffer[80];
while (!feof(fin))
{
fgets(temp_buffer,78,fin);
sprintf(send_buffer,"%s",temp_buffer);
send(s_data_act, send_buffer, strlen(send_buffer), 0);
}
fclose(fin);
sprintf(send_buffer,"226 File transfer completed... \r\n");
bytes = send(ns, send_buffer, strlen(send_buffer), 0);
closesocket(s_data_act);
}
//QUIT
if (strncmp(receive_buffer,"QUIT",4)==0)
{
printf("Quit \n");
sprintf(send_buffer,"221 Connection closed by the FTP client\r\n");
bytes = send(ns, send_buffer, strlen(send_buffer), 0);
if (bytes < 0) break;
closesocket(ns);
}
}
//CLOSE SOCKET
closesocket(ns);
printf("disconnected from %s\n",inet_ntoa(remoteaddr.sin_addr));
}
closesocket(s);//it actually never gets to this point....use CTRL_C
}
/**
* @brief Return file name from the string passed in.
*/
void wy_fileName_collector(char *_buffer, char *_nameBuffer)
{
char bf[BUFFERSIZE];
char sep[2] = " "; //separation is space
char *word;
int wcount=0;
strcpy(bf, _buffer);
for (word = strtok(bf, sep);word;word = strtok(NULL, sep))
{
wcount++;
if(wcount == 2) // jump the first space"_".
{
strcpy(_nameBuffer, word);
}
}
}
关于用 C 代码编写基于 TCP 的 FTP 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23119615/
我是 ZMQ 的新手。我发现 ZMQ 套接字实现比 winsock 简单得多。但我怀疑 “使用 ZMQ TCP 套接字创建的客户端可以与传统的 TCP 服务器通信吗?” 换句话说我的 ZMQ 客户端可
我想使用 TCP 协议(protocol) 将数据发送到 Logstash。为了发送数据,我正在使用 Node-RED。一个简单的配置如下所示: 在 Logstash 文件夹中,我创建了一个名为 no
当我尝试更改窗口缩放选项时,作为 root,我可以通过在 /proc/sys/net/中执行 net.ipv4.tcp_mem=16777000 来更改值。如果我必须更改这 100 个系统,那将需要大
明天做一些练习题,这道做不出来 TCP 服务器连接 TCP 客户端进行通信所需的最小套接字端口数是多少? 肯定只有两个吧?一个用于服务器,一个用于客户端,但这似乎是显而易见的。我的伙伴们认为 TCP
考虑一个存在一个服务器和多个客户端的场景。每个客户端创建 TCP 连接以与服务器交互。 TCP alive的三种用法: 服务器端保活:服务器发送 TCP 保活以确保客户端处于事件状态。如果客户端死了,
TCP TAHOE 和 TCP RENO 有什么区别。 我想知道的是关于 3-dup-ack 和超时的行为? SST 发生了什么变化? 谢谢! 最佳答案 TCP Tahoe 和 Reno 是处理 TC
大家早上好。我一直在阅读(其中大部分在堆栈溢出中)关于如何进行安全密码身份验证(散列 n 次,使用盐等)但我怀疑我将如何在我的 TCP 客户端中实际实现它-服务器架构。 我已经实现并测试了我需要的方法
在遍历 RFC793 时,我开始知道应该以这种方式选择初始序列号段重叠被阻止。 有人能解释一下如果发生重叠,重复段将如何影响 TCP? 最佳答案 不同的操作系统有不同的行为。参见 http://ins
你能举例说明一下tcp/ip中nagle算法的概念吗? 最佳答案 我认为Wikipedia在开头的段落中做得很好。 Nagle's document, Congestion Control in IP
似乎最大 TCP 接收窗口大小为 1GB(使用缩放时)。因此,仍然可以用一个连接填充 100Gb 管道的最大 RTT 是 40ms(因为 2 * 40E-3 * 100E9/8 = 1GB)。这会将这
考虑在两个 TCP 端点之间建立的 TCP 连接,其中一个调用: 关闭():此处,不允许进一步读取或写入。 关机(fd,SHUT_WR):这会将全双工连接转换为单工连接,其中调用 SHUT_WR 的端
我是在 Lua 中编写解析器的新手,我有两个简短的问题。我有一个包含 TCP 选项的数据包,如 MSS、TCP SACK、时间戳、NOP、窗口比例、未知。我基本上是在尝试剖析 TCP 选项字段中的未知
TCP 是否不负责通过在传输过程中发生丢失等情况时采取任何可能必要的措施来确保通过网络完整地发送流? 它做的不对吗? 为什么更高的应用层协议(protocol)及其应用程序仍然执行校验和? 最佳答案
考虑使用 10 Mbps 链路的单个 TCP (Reno) 连接。假设此链路不缓冲数据并且接收方的接收缓冲区比拥塞窗口大得多。设每个 TCP 段的大小为 1500 字节,发送方和接收方之间连接的双向传
考虑这样一个场景,有client-a和server-b。 server-b 禁用了 TCP keepalive。 server-b 没有任何应用程序逻辑来检查 TCP 连接是否打开。 client-a
我正在尝试用 Rust 编写回显服务器。 use std::net::{TcpStream, TcpListener}; use std::io::prelude::*; fn main() {
听说对于TCP连接,服务器会监听一个端口,并使用另一个端口发送数据。 例如,Web 服务器监听端口 80。每当客户端连接到它时,该服务器将使用另一个端口(比如 9999)向客户端发送数据(Web 内容
我试图了解带有标记 PSH 和标记 URG 的 TCP 段之间的区别。我阅读了 RFC,但仍然无法理解,其中一个在将数据发送到进程之前缓冲数据而另一个没有吗? 最佳答案 它们是两种截然不同的机制。 #
有第三方服务公开 TCP 服务器,我的 Node 服务器(TCP 客户端)应使用 tls Node 模块与其建立 TCP 连接。作为 TCP 客户端, Node 服务器同时也是 HTTP 服务器,它应
我正在发送一些 TCP SYN 数据包以获得 TCP RST 的返回。为了识别每个探测器,我在 TCP 序列字段中包含一个计数器。我注意到以下几点: 当SYN probe中的sequence numb
我是一名优秀的程序员,十分优秀!