Article View: borland.public.cppbuilder.internet.socket
Article #3017Re: TWinSocketStream and threading
From: Remy Lebeau (TeamB)
Date: Tue, 20 Dec 2005 03:27
Date: Tue, 20 Dec 2005 03:27
50 lines
3466 bytes
3466 bytes
"Maurice Anderson" <mauriceanderson@hotmail.com> wrote in message news:43a7619b$1@newsgroups.borland.com... > I am trying to create a simple chat server that can send > both text and files. The code you have shown so far is much too simplictic for that. You need to beef up your communication quite a bit in order for the client and server to actually tell each other what to do and when. > how can I determine how many bytes are being receieved from the client? There are a number of ways to do that. At its most basic, you can use the ClientSocket->ReceiveLength() method. That will tell you how much data has actually been received into the socket's internal buffer that can be read without blocking. But that is not reliable enough. For starters, it does not tell you how much data you should be expecting to arrive in the first place. For that, you have two additional options: 1) prefix the data with its length. Read the length first, then you know how many more bytes to read. Keep reading until that many, and only that many, bytes have actually been read. Then you can process what you have read. 2) append a unique byte/sequence to the end of the data. Use a byte/sequence that is quaranteed to never appear in the data. Then you continuous read until that byte/sequence is encontered. Then you can process what you have read, and buffer what is remaining and attach it to the next reading. > How can I tell the server whether the data is simple text > being received or binary data such as a jpg file? Prefix the data with a fixed-length header that describes what the data is. At the very least, include a value that specifies whether the data is text or file data. The data can then include multiple values inside of it, each with a fixed or specified length, that provide parameters (username, password, text message, file name, etc) for the various commands that you then implement by using the header. Only then can you decide what to do with the data. This plays into #1 above, where you could include the data length as part of the header. So, you would read the header first, then read the specified number of bytes and process the overall data based on the rest of the header values. Go to http://www.deja.com and do a search through the newsgroup archives. Numerous discussions and examples on this topic have been posted before. Gambit
Technical Details
Message-ID:
<43a77b12$1@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!nx01.iad01.newshosting.com!newshosting.com!news-out.newsfeeds.com!propagator2-sterling.newsfeeds.com!in.nntp.be!caladan.landsraad.net!newsgroups.borland.com!not-for-mailReferences:
<43a738ea$1@newsgroups.borland.com> <43a75019$1@newsgroups.borland.com> <43a7619b$1@newsgroups.borland.com>Show Full Headers
Complete Article Headers
X-Google-Thread: 11508e,75fde898af9e336 X-Google-Attributes: gid11508e,public X-Google-Language: ENGLISH,ASCII-7-bit From: "Remy Lebeau \(TeamB\)" <no.spam@no.spam.com> Newsgroups: borland.public.cppbuilder.internet.socket References: <43a738ea$1@newsgroups.borland.com> <43a75019$1@newsgroups.borland.com> <43a7619b$1@newsgroups.borland.com> Subject: Re: TWinSocketStream and threading Date: Mon, 19 Dec 2005 19:27:49 -0800 Lines: 52 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: 68.69.17.41 Message-ID: <43a77b12$1@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1135049490 68.69.17.41 (19 Dec 2005 19:31:30 -0700) Path: arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!nx01.iad01.newshosting.com!newshosting.com!news-out.newsfeeds.com!propagator2-sterling.newsfeeds.com!in.nntp.be!caladan.landsraad.net!newsgroups.borland.com!not-for-mail