474,099 Members | 4,201 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Socket server problem

I've written an async socket server app and I'm having an issue with the
EndReceive() call.

My begin receive call is:

sockState.Remot eSocket.BeginRe ceive(sockState .ReceiveBuffer, 0,
SocketState.Buf ferSize, 0, new AsyncCallback(t his.ReadCallbac k), sockState);

The ReadCallback() method begins:

private void ReadCallback(IA syncResult asyncResult)
{
Debug.WriteLine ("SocketServer. ReadCallback: Entry");
SocketState socketState = (SocketState) asyncResult.Asy ncState;
int bytesRead = socketState.Rem oteSocket.EndRe ceive(asyncResu lt);
Debug.WriteLine (String.Format( "SocketServer.R eadCallback: End receive with
{0} bytes", bytesRead));

The first Debug.WriteLine () gets called when I connect, but EndReceive seems
to block after the send.

My test application is very simple. All it does is:

Socket sock = new Socket(AddressF amily.InterNetw ork, SocketType.Stre am,
ProtocolType.Tc p);
IPEndPoint ep = new IPEndPoint(IPAd dress.Loopback, 7451);
sock.Connect(ep );
HelloMsg hm = (HelloMsg)
SocketMessageFa ctory.CreateSoc ketMessage(Sock etMsgType.HELLO );
sock.Send(hm.To Buffer());

So, as I said, the server registers the connection and gets to the
ReadCallback method, but blocks on EndReceive. Any ideas on what I'm
missing?

Thanks.

Pete

--
http://www.petedavis.net
Nov 15 '05 #1
4 1849

"Pete Davis" <pd******@hotma il.com> wrote in message
news:1c******** *************** *******@news.me ganetnews.com.. .
I've written an async socket server app and I'm having an issue with the
EndReceive() call.

My begin receive call is:

sockState.Remot eSocket.BeginRe ceive(sockState .ReceiveBuffer, 0,
SocketState.Buf ferSize, 0, new AsyncCallback(t his.ReadCallbac k), sockState);
The ReadCallback() method begins:

private void ReadCallback(IA syncResult asyncResult)
{
Debug.WriteLine ("SocketServer. ReadCallback: Entry");
SocketState socketState = (SocketState) asyncResult.Asy ncState;
int bytesRead = socketState.Rem oteSocket.EndRe ceive(asyncResu lt);
Debug.WriteLine (String.Format( "SocketServer.R eadCallback: End receive with {0} bytes", bytesRead));

The first Debug.WriteLine () gets called when I connect, but EndReceive seems to block after the send.

My test application is very simple. All it does is:

Socket sock = new Socket(AddressF amily.InterNetw ork, SocketType.Stre am,
ProtocolType.Tc p);
IPEndPoint ep = new IPEndPoint(IPAd dress.Loopback, 7451);
sock.Connect(ep );
HelloMsg hm = (HelloMsg)
SocketMessageFa ctory.CreateSoc ketMessage(Sock etMsgType.HELLO );
sock.Send(hm.To Buffer());

So, as I said, the server registers the connection and gets to the
ReadCallback method, but blocks on EndReceive. Any ideas on what I'm
missing?

Thanks.

Pete

--
http://www.petedavis.net


Are you using ManualResetEven ts ?
Nov 15 '05 #2
I have a manual reset event for the Accept (which is also async). Do I need
it for the receive as well?

Pete

--
http://www.petedavis.net

"James" <James@Kimberle yREMOVE_MEdawn. co.uk> wrote in message
news:OV******** ******@TK2MSFTN GP10.phx.gbl...

"Pete Davis" <pd******@hotma il.com> wrote in message
news:1c******** *************** *******@news.me ganetnews.com.. .
I've written an async socket server app and I'm having an issue with the
EndReceive() call.

My begin receive call is:

sockState.Remot eSocket.BeginRe ceive(sockState .ReceiveBuffer, 0,
SocketState.Buf ferSize, 0, new AsyncCallback(t his.ReadCallbac k),

sockState);

The ReadCallback() method begins:

private void ReadCallback(IA syncResult asyncResult)
{
Debug.WriteLine ("SocketServer. ReadCallback: Entry");
SocketState socketState = (SocketState) asyncResult.Asy ncState;
int bytesRead = socketState.Rem oteSocket.EndRe ceive(asyncResu lt);
Debug.WriteLine (String.Format( "SocketServer.R eadCallback: End receive

with
{0} bytes", bytesRead));

The first Debug.WriteLine () gets called when I connect, but EndReceive

seems
to block after the send.

My test application is very simple. All it does is:

Socket sock = new Socket(AddressF amily.InterNetw ork, SocketType.Stre am,
ProtocolType.Tc p);
IPEndPoint ep = new IPEndPoint(IPAd dress.Loopback, 7451);
sock.Connect(ep );
HelloMsg hm = (HelloMsg)
SocketMessageFa ctory.CreateSoc ketMessage(Sock etMsgType.HELLO );
sock.Send(hm.To Buffer());

So, as I said, the server registers the connection and gets to the
ReadCallback method, but blocks on EndReceive. Any ideas on what I'm
missing?

Thanks.

Pete

--
http://www.petedavis.net


Are you using ManualResetEven ts ?

Nov 15 '05 #3
Ok, so that was the problem. I added a manual reset event for the receive
operation and it now works. Thanks a ton.

Now I have a new problem. One I suspected I might have and I'm confused on
this. According to the documentation:
ms-help://MS.VSCC.2003/MS.MSDNQTR.2003 OCT.1033/cpref/html/frlrfSystemNetS ock
etsSocketClassE ndReceiveTopic. htm

on EndReceive(), at least from looking at the sample code (which granted,
doesn't necessarily mean anything), it appears that by continuing to call
BeginReceive() and EndReceive(), I should eventually get an EndRevceive()
that returns 0 bytes at the end of my data.

That's not happening. In this sample case, I'm sending only a 16 byte
message. EndReceive() gets called once for the 16 byte message and then it
blocks the second time.

I need to know when a message has been completely sent. I could interpret
the data as it comes in and start keeping a tally of how much data has come
across (the first 2 ints in the message are a message type and a message
length). So based on the length, I should know how many bytes are waiting to
be received.

I'd rather not have to do this partial decoding of the data, but if I
have to, obviously I will.

Pete

--
http://www.petedavis.net

"James" <James@Kimberle yREMOVE_MEdawn. co.uk> wrote in message
news:OV******** ******@TK2MSFTN GP10.phx.gbl...

"Pete Davis" <pd******@hotma il.com> wrote in message
news:1c******** *************** *******@news.me ganetnews.com.. .
I've written an async socket server app and I'm having an issue with the
EndReceive() call.

My begin receive call is:

sockState.Remot eSocket.BeginRe ceive(sockState .ReceiveBuffer, 0,
SocketState.Buf ferSize, 0, new AsyncCallback(t his.ReadCallbac k),

sockState);

The ReadCallback() method begins:

private void ReadCallback(IA syncResult asyncResult)
{
Debug.WriteLine ("SocketServer. ReadCallback: Entry");
SocketState socketState = (SocketState) asyncResult.Asy ncState;
int bytesRead = socketState.Rem oteSocket.EndRe ceive(asyncResu lt);
Debug.WriteLine (String.Format( "SocketServer.R eadCallback: End receive

with
{0} bytes", bytesRead));

The first Debug.WriteLine () gets called when I connect, but EndReceive

seems
to block after the send.

My test application is very simple. All it does is:

Socket sock = new Socket(AddressF amily.InterNetw ork, SocketType.Stre am,
ProtocolType.Tc p);
IPEndPoint ep = new IPEndPoint(IPAd dress.Loopback, 7451);
sock.Connect(ep );
HelloMsg hm = (HelloMsg)
SocketMessageFa ctory.CreateSoc ketMessage(Sock etMsgType.HELLO );
sock.Send(hm.To Buffer());

So, as I said, the server registers the connection and gets to the
ReadCallback method, but blocks on EndReceive. Any ideas on what I'm
missing?

Thanks.

Pete

--
http://www.petedavis.net


Are you using ManualResetEven ts ?

Nov 15 '05 #4

"Pete Davis" <pd******@hotma il.com> wrote in message
news:11******** *************** *******@news.me ganetnews.com.. .
Ok, so that was the problem. I added a manual reset event for the receive
operation and it now works. Thanks a ton.

Now I have a new problem. One I suspected I might have and I'm confused on
this. According to the documentation:
ms-help://MS.VSCC.2003/MS.MSDNQTR.2003 OCT.1033/cpref/html/frlrfSystemNetS ock etsSocketClassE ndReceiveTopic. htm

on EndReceive(), at least from looking at the sample code (which granted,
doesn't necessarily mean anything), it appears that by continuing to call
BeginReceive() and EndReceive(), I should eventually get an EndRevceive()
that returns 0 bytes at the end of my data.

That's not happening. In this sample case, I'm sending only a 16 byte
message. EndReceive() gets called once for the 16 byte message and then it
blocks the second time.

I need to know when a message has been completely sent. I could interpret the data as it comes in and start keeping a tally of how much data has come across (the first 2 ints in the message are a message type and a message
length). So based on the length, I should know how many bytes are waiting to be received.

I'd rather not have to do this partial decoding of the data, but if I
have to, obviously I will.

Pete

--
http://www.petedavis.net

"James" <James@Kimberle yREMOVE_MEdawn. co.uk> wrote in message
news:OV******** ******@TK2MSFTN GP10.phx.gbl...

"Pete Davis" <pd******@hotma il.com> wrote in message
news:1c******** *************** *******@news.me ganetnews.com.. .
I've written an async socket server app and I'm having an issue with the EndReceive() call.

My begin receive call is:

sockState.Remot eSocket.BeginRe ceive(sockState .ReceiveBuffer, 0,
SocketState.Buf ferSize, 0, new AsyncCallback(t his.ReadCallbac k),

sockState);

The ReadCallback() method begins:

private void ReadCallback(IA syncResult asyncResult)
{
Debug.WriteLine ("SocketServer. ReadCallback: Entry");
SocketState socketState = (SocketState) asyncResult.Asy ncState;
int bytesRead = socketState.Rem oteSocket.EndRe ceive(asyncResu lt);
Debug.WriteLine (String.Format( "SocketServer.R eadCallback: End receive

with
{0} bytes", bytesRead));

The first Debug.WriteLine () gets called when I connect, but EndReceive

seems
to block after the send.

My test application is very simple. All it does is:

Socket sock = new Socket(AddressF amily.InterNetw ork, SocketType.Stre am, ProtocolType.Tc p);
IPEndPoint ep = new IPEndPoint(IPAd dress.Loopback, 7451);
sock.Connect(ep );
HelloMsg hm = (HelloMsg)
SocketMessageFa ctory.CreateSoc ketMessage(Sock etMsgType.HELLO );
sock.Send(hm.To Buffer());

So, as I said, the server registers the connection and gets to the
ReadCallback method, but blocks on EndReceive. Any ideas on what I'm
missing?

Thanks.

Pete

--
http://www.petedavis.net


Are you using ManualResetEven ts ?



Are you Setting the Receive Manual Reset Event after each Recieve? - You
shouldn't - only Set the MSR when bytes == 0

does this help?

JJ
Nov 15 '05 #5

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

Similar topics

11
8551
by: anuradha.k.r | last post by:
hi, i am writing a socket program in python,both client side and server side.I've written the client side which is working perfectly fine(checked it against server program written in C).but as for my server program written in python it simply hangs.it does not show any error also.I've tried sample programs available .I don understand what the reason is as i am quite new to it. here is teh server side program: ///////////////////////
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):
4
8770
by: DreJoh | last post by:
I've read many articles on the subject and the majority of them give the same solution that's in article 821625 on the MSDN website. I'm using the following code and when a the client disconnects the child socket remains in the CLOSE_WAIT state. Anyone have any idea what's missing? ----------------------------- Socket Code ----------------------------- namespace Sockets { #region Class - SocketClient
4
7115
by: zbcong | last post by:
Hello: I write a multithread c# socket server,it is a winform application,there is a richtextbox control and button,when the button is click,the server begin to listen the socket port,waiting for a incoming connection,the relative code snipprt as following:: private IPAddress myIP=IPAddress.Parse("127.0.0.1"); private IPEndPoint myServer; private Socket socket; private Socket accSocket; private System.Windows.Forms.Button button2;...
2
26544
by: zhebincong | last post by:
Hello: I write a multithread c# socket server,it is a winform application,there is a richtextbox control and button,when the button is click,the server begin to listen the socket port,waiting for a incoming connection,the relative code snipprt as following:: private IPAddress myIP=IPAddress.Parse("127.0.0.1");
4
18175
by: Chris Tanger | last post by:
Context: C# System.Net.Sockets Socket created with constructor prarmeters Internetwork, Stream and TCP everything else is left at the default parameters and options except linger may be changed as I find appropriate. I am using the socket asynchronously by calling the BeingSend and BeginReceive calls. I would like to be able to call shutdown and close asynchronously if possible.
4
2371
by: Sa¹o Zagoranski | last post by:
Hi! I'm writing a simple 3D First person shooter game. It is a multiplayer game, where all the players connect to one server.
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...
2
15359
by: djc | last post by:
I read a network programming book (based on framework 1.1) which indicated that you should 'never' use the RecieveTimeout or the SendTimeout 'socket options' on TCP sockets or you may loose data. I now see the socket.RecieveTimeout 'property' in the visual studio 2005 help documentation (framework 2.0) and it has example of it being used with TCP socket. This propery is also listed as 'new in .net 2.0'. 1) is the socket.RecieveTimeout...
6
3696
by: ahlongxp | last post by:
socket.makefile() may lose data when "connection reset by peer". and socket.recv() will never lose the data. change the "1" to "0" in the client code to see the difference. confirmed on both windows and linux. so I guess there is a problem with makefile(). # Echo server program
0
10665
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
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
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
6841
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
5567
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
3
4134
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.