Article View: borland.public.cppbuilder.internet.socket
Article #4190Re: server socket
From: loukiachr
Date: Fri, 18 Mar 2005 09:44
Date: Fri, 18 Mar 2005 09:44
199 lines
6660 bytes
6660 bytes
well i have the following code
//client//------------------------------------------------------------------
---------
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
//--------------------------------------------------------------------------
-
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//--------------------------------------------------------------------------
-
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//--------------------------------------------------------------------------
-
bool __fastcall ReadRaw(TCustomWinSocket *Socket, LPVOID Buffer, int
BufSize)
{
int read;
LPBYTE pBuf = (LPBYTE) Buffer;
while( BufSize > 0 )
{
read = Socket->ReceiveBuf(pBuf, BufSize);
if( read == -1 )
{
if( WSAGetLastError() != WSAEWOULDBLOCK )
return false;
}
else
{
pBuf += read;
BufSize -= read;
}
}
return true;
}
//-----------------------------------------------------------
bool __fastcall ReadStream(TCustomWinSocket *Socket, TStream *Stream)
{
BYTE buf[1024];
int BufSize;
int BytesToRead = 0;
if( !ReadRaw(Socket, &BytesToRead, sizeof(int)) )
return false;
while( BytesToRead > 0 )
{
if( BytesToRead >= 1024 )
BufSize = 1024;
else
BufSize = BytesToRead;
if( !ReadRaw(Socket, buf, BufSize) )
return false;
Stream->Write(buf, BufSize);
BytesToRead -= BufSize;
}
return true;
}
//--------------------------------------------
void __fastcall TForm1::ClientSocket1Read(TObject *Sender,
TCustomWinSocket *Socket)
{
TFileStream *file = new TFileStream("a.dat",fmCreate);
try {
ReadStream(Socket, file);
}
__finally {
delete file;
ClientSocket1->Active=false;Application->Terminate();
}
}
//--------------------------------------------------------------------------
-
//--------------------------------------------------------------------------
//server//------------------------------------------------------------------
---------
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
//--------------------------------------------------------------------------
-
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//--------------------------------------------------------------------------
-
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//--------------------------------------------------------------------------
----
bool __fastcall SendRaw(TCustomWinSocket *Socket, LPVOID Buffer, int
BufSize)
{
int sent;
LPBYTE pBuf = (LPBYTE) Buffer;
while( BufSize > 0 )
{
sent = Socket->SendBuf(pBuf, BufSize);
if( sent == -1 )
{
if( WSAGetLastError() != WSAEWOULDBLOCK )
return false;
}
else
{
pBuf += sent;
BufSize -= sent;
}
}
return true;
}
//--------------------------------------------------------------------------
-
bool __fastcall SendStream(TCustomWinSocket *Socket, TStream *Stream)
{
BYTE buf[1024];
int BytesToSend = (Stream->Size - Stream->Position);
if( !SendRaw(Socket, &BytesToSend, sizeof(int)) )
return false;
while( BytesToSend > 0 )
{
int read = Stream->Read(buf, sizeof(buf));
if( read < 1 ) // unexpected end-of-stream
return false;
if( !SendRaw(Socket, buf, read) )
return false;
BytesToSend -= read;
}
return true;
}
//**********************************************************************
void __fastcall TForm1::ServerSocket1GetSocket(TObject *Sender, int Socket,
TServerClientWinSocket *&ClientSocket)
{
TFileStream* file = new TFileStream("a.dat",fmOpenRead);
try
{
SendStream(ServerSocket1->Socket->Connections[0],file) ;
}
__finally {
delete file;Edit1->Text=Edit1->Text.ToInt()+1;
}
}
//--------------------------------------------------------------------------
-
the program sends a file from the server socket to the client socket when
the client socket connects to the servver socket
if 2 client socket connect simutanuesly to the server the second client
doesnt get the file and the program crashes.if the client take the file in a
row it works(i mean 1st client ask file,take file,closses connection,then
come 2nd client etc)
what changes should be made in order that many clients can take the file
from the server at the same time?
� "loukiachr" <loukiachr@hotmail.com> ������ ��� ������
news:423963b8$2@newsgroups.borland.com...
> can one server socket handle many client sockets?
> i want to create a server (using server socket)that transfers file to each
> client that is connected accordingly to the file that is requested..for
> example if client1 asks for a.txt give him,but in the same time client2
> might ask b.txt and give him...i have checked the code for file transfer
> from serversocket to client socket
> i use the code that (i think)remy posted many weeks ago...
>
>
Technical Details
Message-ID:
<423aa2f3@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!proxad.net!213.200.89.82.MISMATCH!tiscali!newsfeed1.ip.tiscali.net!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mailReferences:
<423963b8$2@newsgroups.borland.com>Show Full Headers
Complete Article Headers
X-Google-Thread: 11508e,b5f37cb24ca8c183 X-Google-Attributes: gid11508e,public X-Google-Language: ENGLISH,ASCII Reply-To: "loukiachr" <loukiachr@hotmail.com> From: "loukiachr" <loukiachr@hotmail.com> Newsgroups: borland.public.cppbuilder.internet.socket References: <423963b8$2@newsgroups.borland.com> Subject: Re: server socket Date: Fri, 18 Mar 2005 11:44:26 +0200 Lines: 201 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2800.1409 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1409 NNTP-Posting-Host: 195.251.45.43 Message-ID: <423aa2f3@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1111139059 195.251.45.43 (18 Mar 2005 01:44:19 -0800) Path: arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!proxad.net!213.200.89.82.MISMATCH!tiscali!newsfeed1.ip.tiscali.net!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail