Echo Сервер. Программа для отправки файлов, страница 3

ssa.sin_addr.s_addr = INADDR_ANY;

if(bind(s, sp, sz) == -1){

myclock=time(NULL);

fprintf(fl,"%s Порт уже занят\n",ctime(&myclock));

fclose(fl);

exit(1);

}

if(listen(s, 0) == -1){

myclock=time(NULL);

fprintf(fl,"%s Невозможно перейти в режим ожидания\n",ctime(&myclock));

fclose(fl);

exit(1);

}

gotoxy(1,1);

myclock=time(NULL);

fprintf(fl,"%s Сервер запущен\n",ctime(&myclock));

fprintf(fl,"Maxconection=%d\n",maxconnection);

fprintf(fl,"AllLog=%d\n",alllog);

while(1){

if((c = accept(s, cp, &sz)) == -1){

myclock=time(NULL);

fprintf(fl,"%s Невозможно принять соединение\n",ctime(&myclock));

fclose(fl);

exit(1);

}

else{

DWORD ID;

Thread=pthread_create(NULL,0,&Send,&c,0,&ID) ;

}

return 0;

}

//--------------------------------------------------------------------------b)  Tclienlist.h

#ifndef tclientlistH

class TClientList{

protected:

class TSpisok{

public:

int Socket;

TSpisok *Next,*Prev;

};

TSpisok *bg;

int maxcount;

public:

TClientList(int n);

~TClientList();

int Append(int Socket);

int Delete(int Socket);

int Count();

int Get(int Count);

};

#define tclientlistH

//--------------------------------------------------------------------------#endif

c)  TClientList.cpp

#pragma hdrstop

#include "tclientlist.h"

#include <stddef.h>

#include <stdio.h>

//--------------------------------------------------------------------------TClientList::TClientList(int n)

{

bg=NULL;

maxcount=n;

}

TClientList::~TClientList()

{

TSpisok *tp;

tp=bg;

while(bg!=NULL){

bg=tp->Next;

delete tp;

tp=bg;

}

}

int TClientList::Append(int Socket){

if (Count()>=maxcount) return -1;

TSpisok *tp=new TSpisok;

tp->Socket=Socket;

tp->Next=NULL;

tp->Prev=NULL;

if(bg==NULL){

bg=tp;

}

else{

TSpisok *pp;

pp=bg;

while((pp->Next!=NULL)&&(pp->Socket!=Socket)) pp=pp->Next;

if(pp->Socket==Socket){

delete tp;

return -2;

}

else{pp->Next=tp;

tp->Prev=pp;

}

}

return 0;

}

int TClientList::Delete(int Socket){

if(bg==NULL){

return -1;

}

else{

TSpisok *pp,*tp;

pp=bg;

while((pp->Next!=NULL)&&(pp->Socket!=Socket)) pp=pp->Next;

if(pp->Socket==Socket){

if (pp==bg){

bg=pp->Next;

if (bg!=NULL)bg->Prev=NULL;

}

else{

if (pp!=NULL) if(pp->Prev!=NULL) pp->Prev->Next=pp->Next;

if (pp!=NULL) if(pp->Next!=NULL) pp->Next->Prev=pp->Prev;

}

delete pp;

}

else return -2;

}

return 0;

}

int TClientList::Count(){

TSpisok *tp;

tp=bg;

int c=0;

while(tp!=NULL) {

c++;

tp=tp->Next;

}

return c;

}

int TClientList::Get(int Count){

TSpisok *tp;

tp=bg;

for(int i=0;i<Count;i++){

tp=tp->Next;

if (tp==NULL) break;

}

if(tp!=NULL)return tp->Socket;else return -1;

}

//--------------------------------------------------------------------------#pragma package(smart_init)

4)  Chat Client

#include <vcl.h>

#include <iostream.h>

#include <cstdlib>

#include <stdio.h>

#include <sys/types.h>

#include <winsock2.h>

#include <time.h>

#include <signal.h>

#include <stdlib.h>

#include <signal.h>

#include <conio.h>

#pragma hdrstop

#if 1

#define pthread_mutex_t _RTL_CRITICAL_SECTION

#define pthread_mutex_lock EnterCriticalSection

#define pthread_mutex_unlock LeaveCriticalSection

#define pthread_t HANDLE

#define pthread_exit ExitThread

#define pthread_mutex_init InitializeCriticalSection

#define socklen_t int

#define sigset signal

#define pthread_create CreateThread