chào mọi người…
Mình đang học môn lập trình mạng và đang làm bt về xây dựng ứng dụng ftp client connect tới server là filezilla server
mình đang code xong phần đăng nhập,sau khi gửi lệnh PASV báo cho server kiểu passive mode,server gửi về id và port của nó để client tạo kết nối data connect truyền dữ liệu
Nhưng khi mình tạo socket connect tới port đó thì lại không kết nối được …không biết do lỗi gì mong mọi người giúp đỡ
#include<stdio.h>
#include<conio.h>
#include<iostream>
#include<Winsock2.h>
#include<string>
using namespace std;
int main(){
WORD wversion = MAKEWORD(2, 2);
WSADATA wsaData;
if (WSAStartup(wversion,&wsaData)==SOCKET_ERROR){
cout << "\n loi khoi tao winsock";
_getch();
return 0;
}
SOCKET clientSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
//dinh ip
SOCKADDR_IN addrServer;
addrServer.sin_family = AF_INET;
addrServer.sin_port = htons(21);
addrServer.sin_addr.s_addr = inet_addr("127.0.0.1");
//connect
if (connect(clientSocket,(SOCKADDR*)&addrServer,sizeof(addrServer)) == SOCKET_ERROR){
cout << "\n loi connect toi server";
closesocket(clientSocket);
WSACleanup();
_getch();
return 0;
}
//nhan ma thong bao 220 tu server
char receive[1024];
char sendd[1024];
int dataLen;
dataLen = recv(clientSocket, receive, 1024, 0);
if (dataLen==SOCKET_ERROR){
cout << "\n loi connect toi server";
closesocket(clientSocket);
WSACleanup();
_getch();
return 0;
}
receive[dataLen] = '\0';
puts(receive);
//tien hanh gui user va pass
while (true){
//gui USERNAME
cout << "\n Nhap Server:";
fflush(stdin);
gets(sendd);
char user[128] = "USER ";
strcat(user, sendd);
strcat(user, "\n");
if (send(clientSocket, user, strlen(user), 0) == SOCKET_ERROR){
cout << "\n loi connect toi server";
closesocket(clientSocket);
WSACleanup();
_getch();
return 0;
}
//nhan ma gui ve tu server,user dung se cho ma 331
dataLen = recv(clientSocket, receive, 1024, 0);
if (dataLen == SOCKET_ERROR){
cout << "\n loi connect toi server";
closesocket(clientSocket);
WSACleanup();
_getch();
return 0;
}
receive[dataLen] = '\0';
puts(receive);
char temp[8];
strncpy(temp,receive,3);
temp[3] = '\0';
if (strcmp("331",temp) != 0){
continue;
}
//gui Pass
cout << "\n Nhap Password:";
fflush(stdin);
gets(sendd);
char pass[128] = "PASS ";
strcat(pass, sendd);
strcat(pass, "\n");
if (send(clientSocket, pass, strlen(pass), 0) == SOCKET_ERROR){
cout << "\n loi connect toi server";
closesocket(clientSocket);
WSACleanup();
_getch();
return 0;
}
//neu dang nhap thanh cong se tra ve ma 230
dataLen = recv(clientSocket, receive, 1024, 0);
if (dataLen == SOCKET_ERROR){
cout << "\n loi connect toi server";
closesocket(clientSocket);
WSACleanup();
_getch();
return 0;
}
receive[dataLen] = '\0';
puts(receive);
strncpy(temp, receive, 3);
temp[3] = '\0';
if (strcmp("230", temp) != 0){
continue;
}
break;
}
//gui PASV
strcpy(sendd, "PASV\n");
if (send(clientSocket,sendd,strlen(sendd),0) == SOCKET_ERROR){
cout << "\n loi connect toi server";
closesocket(clientSocket);
WSACleanup();
_getch();
return 0;
}
//nhan port gui ver
dataLen = recv(clientSocket, receive, 1024, 0);
if (dataLen == SOCKET_ERROR){
cout << "\n loi connect toi server";
closesocket(clientSocket);
WSACleanup();
_getch();
return 0;
}
receive[dataLen] = '\0';
puts(receive);
//kiem tra co phai ma loi 227
char temp[8];
strncpy(temp, receive, 3);
temp[3] = '\0';
unsigned int port;
if (strcmp("227", temp) ==0){
//tien hanh boc tach de lay port
string receive1(receive);
int pos1 = receive1.find('(');
int pos2 = receive1.find(')');
string c1 = receive1.substr(pos1 + 1, pos2 - pos1 - 1);
for (int i = 1; i <= 4; i++){
pos1 = c1.find(',');
c1 = c1.substr(pos1 + 1, c1.length() - pos1 - 1);
}
pos1 = c1.find(',');
int h1 = atoi(c1.substr(0,pos1).c_str());
int h2 = atoi(c1.substr(pos1+1,c1.length()-pos1-1).c_str());
port = h1 * 256 + h2;
}
//sau khi lay dc port tao socket data connection
SOCKET clientSocket2 = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
//dinh ip
SOCKADDR_IN addrServer2;
addrServer2.sin_family = AF_INET;
addrServer2.sin_port = port;
cout << port;
addrServer2.sin_addr.s_addr = inet_addr("127.0.0.1");
if (connect(clientSocket2,(SOCKADDR*)&addrServer2,sizeof(addrServer2)) == SOCKET_ERROR){
cout << "\n khong tao duoc socket data connection";
closesocket(clientSocket);
WSACleanup();
_getch();
return 0;
}
cout << "\n hehe";
closesocket(clientSocket);
WSACleanup();
_getch();
return 0;
}