Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Nov 25th, 2005, 11:50 AM   #1
Brent
Highly Adaptive Penguin
 
Brent's Avatar
 
Join Date: May 2005
Location: United States
Posts: 247
Rep Power: 4 Brent is on a distinguished road
Problem with FTP server/client

i made an ftp server and client. The client connects to the server, but when i enter a file name the client will freeze. i dont know if the server isnt sending the file or the client isnt recieving the file. sorry for the indentation, my computer's text editor has gone nuts, and i dont know how to fix it. i am using a open watcom compiler on windows xp.


here's the code for the server:
#include<winsock.h>
#include<iostream>
#include<fstream>
#include<cstring> 
#pragma comment(lib,"wsock32.lib")


using namespace std;	

	
SOCKET client;
SOCKET server;	


char file[50];
char send_file[5000];
char *str_file;
char *file_dir;
int a;
ifstream infile;
	
int main()
{
	WORD sockversion;
	WSADATA wsaData;
	int net;
	
	sockversion=MAKEWORD(1,1);
	WSAStartup(sockversion,&wsaData);
	
	cout<<"ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ\n";
	cout<<"Quick-e FTP server\n";
	cout<<"ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ\n";
	
	server=socket(PF_INET,SOCK_STREAM,IPPROTO_TCP);
	if(server==INVALID_SOCKET)
	{
		cout<<"invalid socket\n";
		WSACleanup();
		return 0;
	}
	
	sockaddr_in sin;
	sin.sin_family=AF_INET;
	sin.sin_port=htons(21);
	sin.sin_addr.s_addr=INADDR_ANY;
	
	net=bind(server,(LPSOCKADDR)&sin,sizeof(sin));
	if(net==SOCKET_ERROR)
	{
		cout<<"failed bind\n";
		WSACleanup();
		return 0;
	}
	
	if((net=listen(server,4))!=SOCKET_ERROR)
	{
			
		client=accept(server,NULL,NULL);
		if(client==INVALID_SOCKET)
		{
			cout<<"error accepting connection\n";
		}
		else
		{
			cout<<"client connected\n";
		}
		
		for(;;)
		{
			a=recv(client,file,sizeof(file),0);    //recieve file request from client
			if(a==SOCKET_ERROR)
			{
				int error=WSAGetLastError();
				if(error==WSAENOTCONN)
				{
					cout<<"client not connected\n";
					WSACleanup();
				}
				else if(error==WSAESHUTDOWN)
				{
					cout<<"client disconnected\n";
					WSACleanup();
				}
				cout<<"failed to recieve file request\n";
			}
			else
			{
				cout<<file<<endl;
			}
				
			infile.open(file,ios::binary);           //open filename sent by client
			if(infile)
			{
				while(!infile.eof())               //while the end of file is not reached 
				{									//retrieve data from the file and send it to the client
					infile.get(send_file,sizeof(send_file));
					infile.ignore(80,'\n');
					send(server,send_file,sizeof(send_file),0);    
				}
			}
			else
			{
				cout<<"invalid file name\n";
				send(server,"invalid file name",sizeof("invalid file name"),0);
			}
			infile.close();
		}
		WSACleanup();
		closesocket(client);
	}
	WSACleanup();
	closesocket(client);
	return 0;
}


here's the client:
#include<winsock.h>
#include<iostream>
#include<fstream>
#include<cstring> 
#pragma comment(lib,"wsock32.lib")
#define MAXBUFLEN 500
	
using namespace std;	

	
SOCKET client;
SOCKET server;	


char file[50];
char recv_file[5000];
int a;
int count=0;
	
int main()
{
	WORD sockversion;
	WSADATA wsaData;
	int net;
	
	char rdata[500];
	char data[500];
	
	sockversion=MAKEWORD(1,1);
	WSAStartup(sockversion,&wsaData);
	
	client=socket(PF_INET,SOCK_STREAM,IPPROTO_TCP);
	if(server==INVALID_SOCKET)
	{
		cout<<"invalid socket\n";
		WSACleanup();
		return 0;
	}
	
	sockaddr_in sin;
	sin.sin_family=AF_INET;
	sin.sin_port=htons(21);
	sin.sin_addr.s_addr=inet_addr("127.0.0.1");
	
	if(connect(client,(LPSOCKADDR)&sin,sizeof(sin))!=SOCKET_ERROR)
	{
		cout<<"connected to FTP server\n";
		while(strcmp(file,"exit ftp")!=0)
		{
			int a;
			ifstream infile;
			ofstream outfile;
			
			cout<<"get file: ";         //enter file to get
			cin.get(file,49);
			cin.ignore(80,'\n');
			
			a=send(client,file,sizeof(file),0);       //send file request to server
			if(a==SOCKET_ERROR)
			{
				cout<<"failed to send file request\n";
			}
			
			outfile.open(file,ios::out);               //open the requested file
			if(outfile)
			{
				while(SOCKET_ERROR)                    //while SOCKET_ERROR recieve file date  
				{     									//from server and write it to the file
					count++;
					
					a=recv(client,recv_file,sizeof(recv_file),0);    
					if(a==SOCKET_ERROR)
					{
						int error=WSAGetLastError();
						if(error==WSAENOTCONN)
						{
							cout<<"socket not connected\n";
							WSACleanup();
							return 1;
						}
						else if(error==WSAESHUTDOWN)
						{
							cout<<"connection closed\n";
							WSACleanup();
							return 1;
						}
						cout<<"failed to recieve file\n";
					}
					else if(strcmp(recv_file,"invalid file name")==0)
					{
						cout<<"invalid file name\n";
						break;
					}
					else
					{
						outfile<<recv_file<<endl;
						cout<<"chunck "<<count<<" recieved\n";
					}
				}
				cout<<"file recieved\n";
			}
			else
			{
				cout<<"failed to save file\n";
			}
			outfile.close();
		}
	}
	
	WSACleanup();
	closesocket(server);
	closesocket(client);
	return 0;
}
Brent is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 4:12 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC