473,394 Members | 1,887 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Socket server problems

I'm trying to do async socket IO in a socket server in C# and I'm having
trouble on the receive side of things.

I've tried variations of the code below and I just can't seem to get it to
work. I think my _receiveEvent.Set() calls may be misplaced among other
things. EndReceive() gets called when the first message is sent.

What I want it to do is to continue to receive until the entire message has
arrived. I'm not really sure how to accomplish that. Once a full message has
arrived, I want to process the message (which generally involves a reply)
and then continue receiving.

Thanks for any help anyone can provide.

Pete Davis
private void CommunicationThread()
{
Monitor.Enter(_socketStates);
SocketState sockState = (SocketState) _socketStates[Thread.CurrentThread];
sockState.Connected = true;
Monitor.Exit(_socketStates);

try
{
_receiveEvent.Reset();
sockState.RemoteSocket.BeginReceive(sockState.Rece iveBuffer, 0,
SocketState.BufferSize, 0, new AsyncCallback(this.ReadCallback), sockState);
_receiveEvent.WaitOne();
}
catch(SocketException se)
{
sockState.Connected = false;
LogException(se);
}
catch(System.Exception ex)
{
sockState.Connected = false;
LogException(ex);
}
return;
}

private void ReadCallback(IAsyncResult asyncResult)
{
SocketState socketState = (SocketState) asyncResult.AsyncState;
int bytesRead = 0;
try
{
bytesRead = socketState.RemoteSocket.EndReceive(asyncResult);
Debug.WriteLine("SocketServer.ReadCallback: EndReceive completed");
}
catch (SocketException se)
{
// Likely the socket timed out.
LogException(se);
_receiveEvent.Set();
return;
}

Debug.WriteLine(String.Format("SocketServer.ReadCa llback: End receive with
{0} bytes", bytesRead));
// Append new data until all data received
if (bytesRead > 0)
{
socketState.DataReceived = bytesRead;
socketState.BufferUpdate();
sockState.RemoteSocket.BeginReceive(socketState.Re ceiveBuffer, 0,
SocketState.BufferSize, 0, new AsyncCallback(this.ReadCallback),
socketState);
_receiveEvent.Set();
return;
}
// All data received. Handle message
else
{
HandleMessage(socketState);
}
_receiveEvent.Set();
}

Nov 15 '05 #1
0 1201

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Nathan Davis | last post by:
Hi, I am having problems reading data from a socket. The problem only occurs while trying to read from one particular server, and does not occur on Windows (as far as I know, this may occur...
8
by: simon place | last post by:
Spent some very frustrating hours recoding to find a way of closing a server socket, i'd not thought it would be any problem, however, after complete failure and as a last resort, i looked at the...
0
by: Hameed Khan | last post by:
hi all, i am getting some problems with my first socket script. can any one of you point me why this is happening. the server script suppose to accept one connection at a time and send countdown...
2
by: Rene Sørensen | last post by:
We are 4 students working on a assignment, that our teacher gave use, normally we do this is C++, but the 4 of us, use C# more often that C++ so… We made a small games called reversi, now our job...
7
by: | last post by:
Hi all, I have a simple .aspx page running on net 2.0 that is trying to do a http post to a remote server. Here is the code Private Function ProcessRequests(ByVal strbody As String) As String...
13
by: coloradowebdev | last post by:
i am working on basically a proxy server that handles requests via remoting from clients and executes transactions against a third-party server via TCP. the remoting site works like a champ. my...
10
by: Uma - Chellasoft | last post by:
Hai, I am new to VB.Net programming, directly doing socket programming. In C, I will be able to map the message arrived in a socket directly to a structure. Is this possible in VB.Net. Can...
6
by: Sean | last post by:
Hi Everyone, My apologies for a somewhat dump question but I am really stuck. I have been working on this code for two days straight I am dont know what is wrong with it. when I run the code, All...
0
by: =?Utf-8?B?QWxwZXIgQUtDQVlPWg==?= | last post by:
Hello, First of all I wish you a good day. My help request is about .NET asynchrounus socket communication. I have developed Server-Client Windows Forms .NET applications in VC++ .NET v2003. I...
0
by: clemima | last post by:
Hi, i have a script that listens on a port for simple text messages and then executes sql to a remote mssql database . The script is started via crontab . There are 2 problems : 1 . The line to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.