Click here to Skip to main content
15,923,015 members
Home / Discussions / .NET (Core and Framework)
   

.NET (Core and Framework)

 
QuestionAssemblies Pin
MrColeyted9-May-08 22:22
MrColeyted9-May-08 22:22 
AnswerDouble Post - Please Ignore Pin
Giorgi Dalakishvili10-May-08 5:13
mentorGiorgi Dalakishvili10-May-08 5:13 
Questionhow to find a version of DLL Pin
Sujit Gupta9-May-08 7:01
Sujit Gupta9-May-08 7:01 
AnswerRe: how to find a version of DLL Pin
CodingYoshi9-May-08 13:59
CodingYoshi9-May-08 13:59 
QuestionRemoting issue Pin
Siddharth Rastogi8-May-08 22:58
Siddharth Rastogi8-May-08 22:58 
AnswerRe: Remoting issue Pin
CodingYoshi9-May-08 12:18
CodingYoshi9-May-08 12:18 
QuestionHow to find argument type (in] [out] using reflection? Pin
fizk8-May-08 22:11
fizk8-May-08 22:11 
QuestionSocket is closed before all buffered data is transmitted Pin
realbobb8-May-08 20:54
realbobb8-May-08 20:54 
First of all, this post is also on msdn - but Im not getting any help Frown | :( I hope some of you can help me...

Hi!
I have a small webserver on a pda, which upon request can return some data. The problem is, that after the data to transmit has been buffered, and the socket is called with the shutdown and close command, the socket connection is terminated before all data has been transmitted.

If I add a delay before the shutdown is called on the socket, all data is send correctly. What am I doing wrong?

If I add a delay before the close methode, but after the shutdown methode, the transmission is also stopped before all data has been send.

It seems very wired, since the LingerOption of the socket is enabled...

Really hope someone can tell me what is going on...

Best regards
Martin Jørgensen

Here is the code (I have removed a lot of unnecessay code...):

using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Net;
using System.Net.Sockets;
namespace DeviceApplication2
{
class RegulationStatusService
{
private const int PORT = 13000;
private static RegulationStatusService s_Instance = null;
private Thread m_Thread;
private TcpListener m_TCPServer;
private string m_HTTPVersion;
private bool m_IsRunning = false;
public static RegulationStatusService Instance
{
get
{
if (s_Instance == null)
{
s_Instance = new RegulationStatusService();
}
return s_Instance;
}
}

public void Start()
{
if (!m_IsRunning)
{
try
{
IPAddress ipAddr = IPAddress.Any;
m_TCPServer = new TcpListener(ipAddr, PORT);
m_TCPServer.Start();
m_Thread = new Thread(new ThreadStart(Runner));
m_Thread.IsBackground = true;
m_Thread.Start();
m_IsRunning = true;
}
catch { }
}
}

public bool Running
{
get
{
return m_IsRunning;
}
}

private void Runner()
{
while (Running)
{
Socket socket = m_TCPServer.AcceptSocket();
if (!socket.Connected)
continue;
try
{
LingerOption lo = new LingerOption(true, 0);
socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Linger, lo);
IList<byte> buffer = new List<byte>(100);
using (NetworkStream stream = new NetworkStream(socket))
{
int b;
while ((b = stream.ReadByte()) != -1 && (b != '\r'))
buffer.Add(Convert.ToByte(b));
}
byte[] ba = new byte[buffer.Count];
buffer.CopyTo(ba, 0);
string httpRequest = Encoding.ASCII.GetString(ba, 0, ba.Length).ToUpper();
bool validRequest = false;
string instruction = string.Empty;
try
{
// Retrieve the HTTP text and version (e.g. "HTTP/1.1")
m_HTTPVersion = httpRequest.Substring(httpRequest.IndexOf("HTTP"), 8);
instruction = httpRequest.Substring(5, (httpRequest.IndexOf(" HTTP") - 5));
validRequest = (httpRequest.Substring(0, 3) == "GET");
}
catch { }
if (validRequest)
{
if (instruction.IndexOf("GETALLROOMS") != -1)
{
byte[] contentData = Encoding.UTF8.GetBytes("Hello World!");
int contentLength = contentData.Length;
string now = DateTime.Now.ToLongDateString();
StringBuilder txbuffer = new StringBuilder();
txbuffer.Append(m_HTTPVersion).Append(" 200 OK\r\n");
txbuffer.Append("Server:This Computer\r\n");
txbuffer.Append("Content-Type:text/xml\r\n"); // Mime type is always text/xml
txbuffer.Append("Last-Modified:").Append(now).Append("\r\n");
txbuffer.Append("Accept-Ranges:bytes\r\n");
txbuffer.Append("Connection:close\r\n");
txbuffer.Append("Content-Length:").Append(contentLength).Append("\r\n");
txbuffer.Append("\r\n");
String s = txbuffer.ToString();
byte[] headerData = Encoding.UTF8.GetBytes(s);
try
{
if (socket.Connected)
{
int bytesSend;
bytesSend = 0;
while (bytesSend < headerData.Length)
bytesSend += socket.Send(headerData, bytesSend, headerData.Length - bytesSend, SocketFlags.None);
bytesSend = 0;
while (bytesSend < contentData.Length)
bytesSend += socket.Send(contentData, bytesSend, contentData.Length - bytesSend, SocketFlags.None);
}
else
{
}
}
catch { }
}
}
}
catch { }
finally
{
try
{
//Thread.Sleep(5000);
socket.Shutdown(SocketShutdown.Both);
//Thread.Sleep(5000);
socket.Close();
}
catch { }
}
socket = null;
}
}
}
}
AnswerRe: Socket is closed before all buffered data is transmitted Pin
CodingYoshi9-May-08 13:57
CodingYoshi9-May-08 13:57 
GeneralRe: Socket is closed before all buffered data is transmitted Pin
realbobb12-May-08 21:06
realbobb12-May-08 21:06 
Question"?xml namespace="" prefix="asp" ?" in .NET 2005 Pin
salon8-May-08 4:39
salon8-May-08 4:39 
QuestionGet the proper path name, proper case I mean. Pin
AndrewVos8-May-08 3:38
AndrewVos8-May-08 3:38 
AnswerRe: Get the proper path name, proper case I mean. Pin
Dave Kreskowiak8-May-08 4:59
mveDave Kreskowiak8-May-08 4:59 
GeneralRe: Get the proper path name, proper case I mean. Pin
AndrewVos8-May-08 5:14
AndrewVos8-May-08 5:14 
AnswerRe: Get the proper path name, proper case I mean. Pin
Dave Kreskowiak8-May-08 5:32
mveDave Kreskowiak8-May-08 5:32 
GeneralRe: Get the proper path name, proper case I mean. Pin
AndrewVos8-May-08 5:39
AndrewVos8-May-08 5:39 
GeneralRe: Get the proper path name, proper case I mean. Pin
AndrewVos8-May-08 6:12
AndrewVos8-May-08 6:12 
Questiongetting input from user Pin
raydona7-May-08 8:11
raydona7-May-08 8:11 
AnswerRe: getting input from user Pin
Dave Kreskowiak7-May-08 8:42
mveDave Kreskowiak7-May-08 8:42 
Questionhow to use SetWindowLong API function calling in Vb.net Pin
brams77-May-08 4:04
brams77-May-08 4:04 
AnswerRe: how to use SetWindowLong API function calling in Vb.net Pin
Dave Kreskowiak7-May-08 7:12
mveDave Kreskowiak7-May-08 7:12 
GeneralRe: how to use SetWindowLong API function calling in Vb.net Pin
brams77-May-08 10:33
brams77-May-08 10:33 
GeneralRe: how to use SetWindowLong API function calling in Vb.net Pin
Dave Kreskowiak7-May-08 10:57
mveDave Kreskowiak7-May-08 10:57 
GeneralRe: how to use SetWindowLong API function calling in Vb.net Pin
brams79-May-08 4:40
brams79-May-08 4:40 
GeneralRe: how to use SetWindowLong API function calling in Vb.net Pin
Dave Kreskowiak9-May-08 5:44
mveDave Kreskowiak9-May-08 5:44 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.