Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

Лабы / 1 / !lab1 / Server / Server1

.cpp
Скачиваний:
14
Добавлен:
17.04.2013
Размер:
3.06 Кб
Скачать
#include "stdafx.h"
#include "Server1.h"
#include "Winsock2.h"
#include "sys/types.h"
#include <iostream.h>
#include <stdlib.h>
#include <stdio.h>

#pragma comment (lib,"ws2_32.lib")

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

CWinApp theApp;

DWORD WINAPI ClientThread(LPVOID lpParam)
{
	SOCKET sock=(SOCKET)lpParam;
		
	CFile File;
	CString sWorkDir;
	CString sFileName;
	BYTE Buffer[4096];
	u_short BufferLength;
	int rc;

	cout<<"Enter work directory:";
	cin>>sWorkDir.GetBuffer(1024);
	
	while (SetCurrentDirectory((LPCTSTR)sWorkDir)==0)
	{
		cout<<"Invalid directory"<<endl;
		return -1;	
	}

	cout<<"Enter destination name:";
	cin>>sFileName.GetBuffer(1024);
		
	if( !File.Open((LPCTSTR)sFileName, CFile::modeWrite |
           CFile::shareExclusive | CFile::modeCreate))
	{
		 cout<< "File could not be opened "  << "\n";
	}
		
	do
	{
		rc = recv(sock,(char*)&BufferLength,sizeof(BufferLength),0);
		BufferLength = ntohs(BufferLength);
		rc = recv(sock,(char*)&Buffer,BufferLength,0);
		if(rc != BufferLength)
		{
			Sleep(1);
			File.Write(Buffer,rc);
			rc = recv(sock,(char*)&Buffer,BufferLength-rc,0);
			File.Write(Buffer,BufferLength-rc);
		}
		else
		File.Write(Buffer,BufferLength);

	} while (BufferLength!=0);
		   
	File.Close();
	cout<<"Downloading completed successful"<<endl;
    return 0;
}

int _tmain()
{
    WSADATA wsaData;
    SOCKET sListen,sClient;
    int iAddrSize;
    HANDLE hThread;
    DWORD dwThreadId;
    struct sockaddr_in local,client;

    if (WSAStartup(MAKEWORD(2,2), &wsaData) != 0)
    {
        printf("Failed to load Winsock!\n");
        return 1;
    }
    
    sListen = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    if (sListen == SOCKET_ERROR)
    {
        printf("socket() failed: %d\n", WSAGetLastError());
        return 1;
    }
    
    local.sin_family = AF_INET;
    local.sin_port = htons(5150);
	local.sin_addr.s_addr = htonl(INADDR_ANY);

    if (bind(sListen, (struct sockaddr *)&local, sizeof(local)) == SOCKET_ERROR)
    {
        printf("bind() failed: %d\n", WSAGetLastError());
        return 1;
    }
    
	listen(sListen, 8);
    
    while (1)
    {
        iAddrSize = sizeof(client);
        sClient = accept(sListen, (struct sockaddr *)&client,
                        &iAddrSize);        
        if (sClient == INVALID_SOCKET)
        {        
            printf("accept() failed: %d\n", WSAGetLastError());
            break;
        }
        printf("Accepted client: %s:%d\n", 
            inet_ntoa(client.sin_addr), ntohs(client.sin_port));

        hThread = CreateThread(NULL, 0, ClientThread, 
                    (LPVOID)sClient, 0, &dwThreadId);
         if (hThread == NULL)
        {
            printf("CreateThread() failed: %d\n", GetLastError());
            break;
        }
        CloseHandle(hThread);
    }
    
	closesocket(sListen);
    WSACleanup();
    return 0;
}


Соседние файлы в папке Server