474,099 Members | 1,636 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.S et() 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 CommunicationTh read()
{
Monitor.Enter(_ socketStates);
SocketState sockState = (SocketState) _socketStates[Thread.CurrentT hread];
sockState.Conne cted = true;
Monitor.Exit(_s ocketStates);

try
{
_receiveEvent.R eset();
sockState.Remot eSocket.BeginRe ceive(sockState .ReceiveBuffer, 0,
SocketState.Buf ferSize, 0, new AsyncCallback(t his.ReadCallbac k), sockState);
_receiveEvent.W aitOne();
}
catch(SocketExc eption se)
{
sockState.Conne cted = false;
LogException(se );
}
catch(System.Ex ception ex)
{
sockState.Conne cted = false;
LogException(ex );
}
return;
}

private void ReadCallback(IA syncResult asyncResult)
{
SocketState socketState = (SocketState) asyncResult.Asy ncState;
int bytesRead = 0;
try
{
bytesRead = socketState.Rem oteSocket.EndRe ceive(asyncResu lt);
Debug.WriteLine ("SocketServer. ReadCallback: EndReceive completed");
}
catch (SocketExceptio n se)
{
// Likely the socket timed out.
LogException(se );
_receiveEvent.S et();
return;
}

Debug.WriteLine (String.Format( "SocketServer.R eadCallback: End receive with
{0} bytes", bytesRead));
// Append new data until all data received
if (bytesRead > 0)
{
socketState.Dat aReceived = bytesRead;
socketState.Buf ferUpdate();
sockState.Remot eSocket.BeginRe ceive(socketSta te.ReceiveBuffe r, 0,
SocketState.Buf ferSize, 0, new AsyncCallback(t his.ReadCallbac k),
socketState);
_receiveEvent.S et();
return;
}
// All data received. Handle message
else
{
HandleMessage(s ocketState);
}
_receiveEvent.S et();
}

Nov 15 '05 #1
0 1234

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

Similar topics

7
8176
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 only on Linux). The server is free.teranews.com, and I am trying to connect to the NNTP port (119). To make things even more bizar, I only have trouble when I try to issue the LIST command. After issuing the command, Java will not return any...
8
9304
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 python wrapper module for sockets, and found that the close command doesn't actually call the underlying close! this didn't seem right, so i added it, and my code now works simply and as expected. def close(self):
0
1617
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 to client connected to it from a function through a separate thread. and echo all the connections opened and closed. the client script is working fine. it echoes the countdown it recieve from server.
2
2903
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 is to make a server, none of us know nothing about socket programming in C#, but we founds some guides for this, and now ,got a server running, but we have some problems though. We have 2 scenario, one where we use a telnet connection and one...
7
2950
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 Dim returnstr As String Dim URL As String = "http://www.dydomain.com/test.asp"
13
2685
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 problem is executing the transactions against the remote server and returning the response to the remoting client. i can open the socket fine and, if i am executing one transaction at a time, everything works great. it's when my proxy server...
10
4068
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 anyone please help me with some sample codings and guidance? Can you also suggest some other news group available for socket programming in VB.Net?
6
2865
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 I get is Input: and the program quits. I also tried reading this online but I didn't quite get it. What is the diff between sin_addr and sin_addr.s_addr. My understanding is that the latter is the IP address of my machine where as the former is...
0
2640
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 have several problems re-establishin connection between peers. Below are my problem cases after closing of the first successfull communication; #1) I re-start the Server to accept connection requests. While it is waiting, I run the Client. It is...
0
2365
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 the remote database appears to go to sleep in the sense that the script receives a null return from the database instead of a message containing the number of rows affected by the sql . The sql in such a case does not get executed against the...
0
10468
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
12327
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
11246
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
10460
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
8012
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
6846
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
6996
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
5568
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
5059
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.