FTP-сервер. Клент не отвечает серверу.
Код:
#define WIN32_LEAN_AND_MEAN
#include <winsock2.h>
#include <process.h>
#include <stdio.h>
#include "work.h"
DWORD dwFtpPort = 0;
char* szMainFile = "c:\\file.txt";
int i;
int iLen = 0;
struct sockaddr_in clientData;
void FTPSendFile(SOCKET sock, struct sockaddr_in *clientData)
{
SOCKET dataSock = 0;
FILE *fp;
BYTE *lpszData;
HANDLE hFile;
LONG lDataToSend, lDataSended = 0L;
send(sock, "150\n", 4, 0);
if(dataSock = socket(AF_INET, SOCK_STREAM, 0) == -1) return;
dataSock = socket(AF_INET, SOCK_STREAM, 0);
if(connect(dataSock, (struct sockaddr *)clientData, sizeof(*clientData)) != SOCKET_ERROR)
{
hFile = CreateFile(szMainFile, GENERIC_READ, FILE_SHARE_READ, NULL,
OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if(hFile != INVALID_HANDLE_VALUE)
{
lDataToSend = GetFileSize(hFile, NULL);
CloseHandle(hFile);
lpszData = (BYTE*)lDataToSend;
fp = fopen(szMainFile, "rb");
if(fp)
{
fread(lpszData, sizeof(BYTE), lDataToSend, fp);
fclose(fp);
}
send(dataSock, (char *)lpszData, lDataToSend, 0);
}
closesocket(dataSock);
}
send(sock, "226\n", 4, 0);
printf( "File uploaded to %s", inet_ntoa( clientData->sin_addr ));
}
void FTPMakeClientData(char *src, struct sockaddr_in *clientData)
{
char cClientIp[17], tmp[5];
int i1, i2, i3, i4, p1, p2, iPort = 0;
ZeroMemory(clientData, sizeof(clientData));
sscanf(src, "%s %d,%d,%d,%d,%d,%d", tmp, &i1, &i2, &i3, &i4, &p1, &p2);
sprintf(cClientIp, "%d.%d.%d.%d\0", i1, i2, i3, i4);
iPort = 256*p1;
iPort+= p2;
clientData->sin_addr.S_un.S_addr = inet_addr( cClientIp );
clientData->sin_family = AF_INET;
clientData->sin_port = htons( iPort );
}
void FTPClient(LPVOID lpSocket)
{
char buf[1024], cCmd[5];
ZeroMemory(buf, sizeof(buf));
ZeroMemory(cCmd, sizeof(cCmd));
send((SOCKET) lpSocket, "220\n", 4, 0);
while(1)
{
iLen = recv((SOCKET)lpSocket, buf, sizeof(buf) , 0);
if(iLen == -1)
break;
strncpy(cCmd, buf, 4);
cCmd[4] = 0;
if(!strcmp(cCmd, "USER")) send((SOCKET) lpSocket, "331\n", 4, 0);
else if(!strcmp(cCmd, "PASS")) send((SOCKET) lpSocket, "230\n", 4, 0);
else if(!strcmp(cCmd, "PORT")) {
FTPMakeClientData(buf, &clientData);
send((SOCKET) lpSocket, "200\n", 4, 0); }
else if(!strcmp(cCmd, "QUIT")) {
send((SOCKET) lpSocket, "220\n", 4, 0);
break;
}
else if(!strcmp(cCmd, "RETR")) FTPSendFile((SOCKET)lpSocket, &clientData);
}
closesocket((SOCKET) lpSocket);
}
void FTPServer()
{
SOCKET iServer, iClient;
struct sockaddr_in sServer;
struct sockaddr_in sClient;
DWORD dwThreadId = NULL;
int clientSize = sizeof(sClient);
WSADATA WsaData;
int err = WSAStartup(0x0101, &WsaData);
if (err == SOCKET_ERROR)
{
printf("WSAStartup() failed: %1d\n", GetLastError());
}
while(1) {
dwFtpPort = 21;
ZeroMemory(&sServer, sizeof(sServer));
sServer.sin_addr.S_un.S_addr = htonl(INADDR_ANY);
sServer.sin_family = AF_INET;
sServer.sin_port = htons( (int)dwFtpPort);
iServer = socket(AF_INET, SOCK_STREAM, 0);
if( iServer == INVALID_SOCKET )
ExitThread(0);
if( bind(iServer, (struct sockaddr *)&sServer, sizeof(sServer)) != 0) {
Sleep(1000);
continue;
}
if( listen(iServer, 4) != 0) {
Sleep(1000);
continue;
}
break;
}
printf("FTP server created on port %d LastError: %d", dwFtpPort, WSAGetLastError());
while(1)
{
iClient = accept(iServer, (struct sockaddr*)&sClient, &clientSize);
printf("\naccepted connection from %s, port %d", inet_ntoa(sClient.sin_addr), htons(sClient.sin_port));
if(iClient == INVALID_SOCKET)
{
Sleep(500); continue;
}
else
_beginthread(FTPClient, 0, (void *)iClient);
}
closesocket(iServer);
ExitThread(1);
}
#include <winsock2.h>
#include <process.h>
#include <stdio.h>
#include "work.h"
DWORD dwFtpPort = 0;
char* szMainFile = "c:\\file.txt";
int i;
int iLen = 0;
struct sockaddr_in clientData;
void FTPSendFile(SOCKET sock, struct sockaddr_in *clientData)
{
SOCKET dataSock = 0;
FILE *fp;
BYTE *lpszData;
HANDLE hFile;
LONG lDataToSend, lDataSended = 0L;
send(sock, "150\n", 4, 0);
if(dataSock = socket(AF_INET, SOCK_STREAM, 0) == -1) return;
dataSock = socket(AF_INET, SOCK_STREAM, 0);
if(connect(dataSock, (struct sockaddr *)clientData, sizeof(*clientData)) != SOCKET_ERROR)
{
hFile = CreateFile(szMainFile, GENERIC_READ, FILE_SHARE_READ, NULL,
OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if(hFile != INVALID_HANDLE_VALUE)
{
lDataToSend = GetFileSize(hFile, NULL);
CloseHandle(hFile);
lpszData = (BYTE*)lDataToSend;
fp = fopen(szMainFile, "rb");
if(fp)
{
fread(lpszData, sizeof(BYTE), lDataToSend, fp);
fclose(fp);
}
send(dataSock, (char *)lpszData, lDataToSend, 0);
}
closesocket(dataSock);
}
send(sock, "226\n", 4, 0);
printf( "File uploaded to %s", inet_ntoa( clientData->sin_addr ));
}
void FTPMakeClientData(char *src, struct sockaddr_in *clientData)
{
char cClientIp[17], tmp[5];
int i1, i2, i3, i4, p1, p2, iPort = 0;
ZeroMemory(clientData, sizeof(clientData));
sscanf(src, "%s %d,%d,%d,%d,%d,%d", tmp, &i1, &i2, &i3, &i4, &p1, &p2);
sprintf(cClientIp, "%d.%d.%d.%d\0", i1, i2, i3, i4);
iPort = 256*p1;
iPort+= p2;
clientData->sin_addr.S_un.S_addr = inet_addr( cClientIp );
clientData->sin_family = AF_INET;
clientData->sin_port = htons( iPort );
}
void FTPClient(LPVOID lpSocket)
{
char buf[1024], cCmd[5];
ZeroMemory(buf, sizeof(buf));
ZeroMemory(cCmd, sizeof(cCmd));
send((SOCKET) lpSocket, "220\n", 4, 0);
while(1)
{
iLen = recv((SOCKET)lpSocket, buf, sizeof(buf) , 0);
if(iLen == -1)
break;
strncpy(cCmd, buf, 4);
cCmd[4] = 0;
if(!strcmp(cCmd, "USER")) send((SOCKET) lpSocket, "331\n", 4, 0);
else if(!strcmp(cCmd, "PASS")) send((SOCKET) lpSocket, "230\n", 4, 0);
else if(!strcmp(cCmd, "PORT")) {
FTPMakeClientData(buf, &clientData);
send((SOCKET) lpSocket, "200\n", 4, 0); }
else if(!strcmp(cCmd, "QUIT")) {
send((SOCKET) lpSocket, "220\n", 4, 0);
break;
}
else if(!strcmp(cCmd, "RETR")) FTPSendFile((SOCKET)lpSocket, &clientData);
}
closesocket((SOCKET) lpSocket);
}
void FTPServer()
{
SOCKET iServer, iClient;
struct sockaddr_in sServer;
struct sockaddr_in sClient;
DWORD dwThreadId = NULL;
int clientSize = sizeof(sClient);
WSADATA WsaData;
int err = WSAStartup(0x0101, &WsaData);
if (err == SOCKET_ERROR)
{
printf("WSAStartup() failed: %1d\n", GetLastError());
}
while(1) {
dwFtpPort = 21;
ZeroMemory(&sServer, sizeof(sServer));
sServer.sin_addr.S_un.S_addr = htonl(INADDR_ANY);
sServer.sin_family = AF_INET;
sServer.sin_port = htons( (int)dwFtpPort);
iServer = socket(AF_INET, SOCK_STREAM, 0);
if( iServer == INVALID_SOCKET )
ExitThread(0);
if( bind(iServer, (struct sockaddr *)&sServer, sizeof(sServer)) != 0) {
Sleep(1000);
continue;
}
if( listen(iServer, 4) != 0) {
Sleep(1000);
continue;
}
break;
}
printf("FTP server created on port %d LastError: %d", dwFtpPort, WSAGetLastError());
while(1)
{
iClient = accept(iServer, (struct sockaddr*)&sClient, &clientSize);
printf("\naccepted connection from %s, port %d", inet_ntoa(sClient.sin_addr), htons(sClient.sin_port));
if(iClient == INVALID_SOCKET)
{
Sleep(500); continue;
}
else
_beginthread(FTPClient, 0, (void *)iClient);
}
closesocket(iServer);
ExitThread(1);
}
Во вторых - ну а каких ты действий ждешь? Твой "сервер" скорей всего зависает в бесконечном цикле - потому что условия, по которым ты проверяешь - по идее могут не выполнятся никогда, но ты этого не проверяешь.
Если клиент работает под Windows, то строка должна оканчиваться не на \n, а на \r\n. Скорее всего клиент просто ждет символ возврата каретки (\r), но не получает его.