SunQuest
 
           General Programming Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingGeneral Programming Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here
  #1  
Old June 7th, 2003, 02:14 AM
tex_guy tex_guy is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Posts: 2 tex_guy User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Exclamation C# Reading From Socket Problem/Question

Hi guys/gals. I'm trying to figure out (in C# .NET environment) how to determine when the remote endpoint of a socket is closed. I know that for HTTP, when you send an HTTP/1.0 request, or a HTTP/1.1 request with a "Connection: close" header(like I am), the server is supposed to terminate the connection once it's sent its data. I'm basically trying to port a Proxy server from one I wrote in java to C#. So the general scheme is as follows (pseudocode).

Code:
Socket clientSocket; //passed in from TcpListener

//read request from client socket
byte[] ByteArray = new Byte[this.clientSocket.Available];
int bytes = this.clientSocket.Receive(ByteArray);

//package it nicely in a HTTPRequest class that I made
HTTPRequest hr = new HTTPRequest(ByteArray);

//Get the remote host (server) and connect
IPHostEntry IPHost = Dns.Resolve(hr.getHost());
IPAddress[] address = IPHost.AddressList;
IPEndPoint sEndpoint = new IPEndPoint(address[0], hr.getPort());
Socket serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream,ProtocolType.Tcp);
serverSocket.Connect(sEndpoint);

//send the request
serverSocket.Send(hr.toBytes());

//get the response
/*How do I know when all of the bytes from the server are here?
If I use serverSocket.Available most of the time I don't have any
 bytes available yet.  I want to read (or block even) until the full
 response is available or the remote endpoint is closed.  I can
 arbitrarily sleep the Thread, but that's a bad hack.  I can parse
 bytes on the fly and look for a Content-Length header, but I'd
 rather not because of complexity of the different encodings etc..
  I'd like to know how many bytes are possible to read so that I
 can construct the byte[] to read into accordingly like below, or be
 able to block until the remote side is done sending.*/

while(?remote_not_done_sending?)
{
     Thread.Sleep(some_short_time);
}
byte [] response_buffer = new byte[serverSocket.Available];



Thanks in advance,

Paul

Last edited by tex_guy : June 7th, 2003 at 02:23 AM.

Reply With Quote
  #2  
Old June 8th, 2003, 07:17 PM
tex_guy tex_guy is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Posts: 2 tex_guy User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
I figured out a way to validate a request just by the structure (ie. GET should have nothing but Headers followed by an extra CRLF, etc.), but I still have the problem of the responses. Do I have to use the Async read methods? There has to be a way of knowing of the remote endpoint has been closed.

Paul

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingGeneral Programming Help > C# Reading Socket.


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway