2 yamatong yamatong 于 2016.09.06 19:55 提问

tcp连接中,connect连接失败,求大神

想写个简单的ftp服务端,主动模式下连接客户端时connect 错误(connect refused),errno是111
#include
#include
#include
#include
#include
#include
#include
#include
#define PORT 8147
#define BACKLOG 2
void process_server(int ,struct sockaddr_in);
void process_port(int ,char[],struct sockaddr_in);

int main(int argc,char argv[])
{
int ss,sc;
struct sockaddr_in server_addr;
struct sockaddr_in client_addr;
int ret;
pid_t pid;
ss=socket(AF_INET,SOCK_STREAM,0);
if(ss<0)
{
printf("error socket\n");
return -1;
}
bzero(&server_addr,sizeof(server_addr));
server_addr.sin_family=AF_INET;
server_addr.sin_addr.s_addr=htonl(INADDR_ANY);
server_addr.sin_port=htons(PORT);
ret=bind(ss,(struct sockaddr
)&server_addr,sizeof(server_addr));
if(ret {
printf("bind error\n");
return -1;
}
ret=listen(ss,BACKLOG);
if(ret {
printf("listen error\n");
return -1;
}
for(;;)
{
printf("enter the server cycle\n");
socklen_t addrlen=sizeof(struct sockaddr);
sc=accept(ss,(struct sockaddr*)&client_addr,&addrlen);
if(sc>0)
printf("accept success\n");
if(sc<0)
return 0;
pid=fork();
if(pid==0)
{
close(ss);
process_server(sc,(struct sockaddr_in)client_addr);
return 0;
}
else {close(sc);}
}
return 0;
}

void process_server(int sc,struct sockaddr_in client_addr)
{
int num;
char buf[1024];
char msg[256];
while(1)
{
num=read(sc,buf,1024);
buf[num]=0;
printf("receive %s\n",buf);
if(num>=0)
{
if(strncasecmp(buf,"port",4)==0)
{process_port(sc,buf,(struct sockaddr_in)client_addr);
}
//else if(strncasecmp(buf,"pasv",4)==0)
//process_pasv(sc,buf,strlen(buf));
else
{
printf("error pattern\n");
strncpy(msg,"unknown pattern",strlen("unknown pattern")+1);
write(sc,msg,strlen(msg)+1);
}
}
}
close(sc);
}
void process_port(int sc,char buf[],struct sockaddr_in client_addr)
{
int s;//socket of data connect
char buf2[1024];
char filename[256];
short int port=atoi(buf+4);//ftp客户端侦听的端口
printf("port %d\n",port);
strcpy(buf2,"success");
write(sc,buf2,strlen(buf2));
int fp;
struct sockaddr_in ftpclient_addr;
memset(&ftpclient_addr,0,sizeof(struct sockaddr_in));
ftpclient_addr.sin_family=AF_INET;
ftpclient_addr.sin_addr.s_addr=htonl(INADDR_ANY);
ftpclient_addr.sin_port=htons(port);
s=socket(AF_INET,SOCK_STREAM,0);
if(s perror("socket");
if(connect(s,(struct sockaddr*)&ftpclient_addr,sizeof(struct sockaddr))!=0)
{
printf("errno is %d\n",errno);
printf("%d\n",WSAGetLastError());
perror("connect");
return;
}
else
{
while(1)
{
printf("connect success\n");
read(s,buf2,1024);
if(strncasecmp(buf2,"get",3)==0)
{
strcpy(filename,"/home/");
strcpy(filename+6,buf2+3);
printf("the filename is %s\n",filename);
strcpy(buf2,"success");
write(s,buf2,strlen(buf2)+1);
fp=open(filename,0666);
int n=1;
while(n>0)
{
n=read(fp,buf2,256);
if(n<0)
{perror("read");break;
}
write(s,buf2,n);
}
}
else if(strncasecmp(buf2,"bye",3)==0)
{
printf("quit\n");
return;
}
else
{
printf("cannot get the request\n");
}
}
}
close(s);
}

ftp服务器能接收到ftp客户端发送过来的端口号(用户输入的端口号),但主动连接时显示connect refused

1个回答

dabocaiqq
dabocaiqq   2017.02.13 23:53
Csdn user default icon
上传中...
上传图片
插入图片