473,326 Members | 2,136 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,326 software developers and data experts.

Socket.BeginReceive()

Hi,

This code doesn't seem to work
it always says "there is more data" and also
this method is only called once
private void AsyncReadCallBack(IAsyncResult asyncResult)

{

//Get the state object

StateObject state = (StateObject) asyncResult.AsyncState;
//Get the socket

Socket socket = state.workSocket;

try

{

//Read data from the client socket

int bytesRead = socket.EndReceive(asyncResult);
if (bytesRead > 0)

{

state.sb.Append(System.Text.Encoding.ASCII.GetStri ng(state.buffer,0,bytesRea
d));

socket.BeginReceive(state.buffer,0,StateObject.Buf ferSize,0,new
AsyncCallback(this.AsyncReadCallBack),state);

}

}

catch (SocketException e)

{

if (e.ErrorCode != 64)

{

Console.WriteLine(e.ToString());

}

}

}

}

Thnx for any help.

TP.
Jul 21 '05 #1
3 10455
TP-Software <tp********@users.sourceforgE.net> wrote:
This code doesn't seem to work
it always says "there is more data" and also
this method is only called once


<snip>

Could you post a short but complete program that demonstrates the
problem?

(If you could also tweak OE so it doesn't post a blank line between
each actual line of code, that would help too.)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Jul 21 '05 #2
Hi,

Even using the following code (based on this page:
http://msdn.microsoft.com/library/de...ceiveTopic.asp )

private void AsyncReadCallBack(IAsyncResult asyncResult)
{
StateObject state = (StateObject) asyncResult.AsyncState;
Socket socket = state.workSocket;

int bytesRead = socket.EndReceive(asyncResult);

if (bytesRead > 0)
{
state.sb.Append(Encoding.ASCII.GetString(state.buf fer,0,bytesRead));
socket.BeginReceive(state.buffer,0,StateObject.Buf ferSize,0,
new
AsyncCallback(this.AsyncReadCallBack),state);;
}
else
{
if (state.sb.Length > 1)
{
//All the data has been read, and there is actually data
string str = state.sb.ToString();
Console.WriteLine("Out: {0}",str);
}

socket.Close();
}
The exception I get is thrown at this line:
int bytesRead = socket.EndReceive(asyncResult);

The exception says nothing more but: "More data is available"

Does this help to explain my problem?

Thnx.
Tim.
Jul 21 '05 #3
TP-Software <tp********@users.sourceforgE.net> wrote:

<snip>

Hmm... I don't get the same problem myself. My test code is below. It
basically writes stuff for 10 seconds before the client's main thread
finishes, which closes the connection and the test server throws an
understandable exception. No reading exceptions though.

TestClient.cs:
using System;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Text;
using System.Threading;

public class TestClient
{
static void Main()
{
Socket skt = new Socket(AddressFamily.InterNetwork,
SocketType.Stream,
ProtocolType.Tcp);

IPHostEntry ipa = Dns.Resolve("localhost");
IPEndPoint ip = new IPEndPoint (ipa.AddressList[0], 1234);

skt.Connect (ip);

State state = new State();
state.data = new byte[60];
state.skt = skt;

skt.BeginReceive (state.data, 0, state.data.Length, 0,
new AsyncCallback (AsyncReadCallBack), state);

Thread.Sleep(10000);
}

private static void AsyncReadCallBack(IAsyncResult asyncResult)
{
State state = (State) asyncResult.AsyncState;
Socket socket = state.skt;

int bytesRead = socket.EndReceive(asyncResult);

if (bytesRead >= 0)
{
Console.WriteLine (Encoding.ASCII.GetString
(state.data, 0, bytesRead));
socket.BeginReceive(state.data, 0, state.data.Length, 0,
new AsyncCallback(AsyncReadCallBack), state);
}
else
{
Console.WriteLine ("Finished.");
socket.Close();
}

}

class State
{
internal byte[] data;
internal Socket skt;
}
}
TestServer.cs:
using System;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Text;

public class TestServer
{
static void Main()
{
byte[] data = Encoding.ASCII.GetBytes
("abcdefghijklmnopqrstuvwxyzabcdefghijklm"+
"nopqrstuvwxyzabcdefghijklmnopqrstuvwxyz");

IPAddress ipAddress = Dns.Resolve("localhost").AddressList[0];
TcpListener listener = new TcpListener(ipAddress, 1234);

listener.Start();

TcpClient client = listener.AcceptTcpClient();
Console.WriteLine("Connected!");

// Get a stream object for reading and writing
NetworkStream stream = client.GetStream();

while (true)
{
stream.Write (data, 0, data.Length);
}
}
}
--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #4

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

Similar topics

2
by: dream machine | last post by:
Hi all , with BegeinReceive I can build async method of Socket Class that Receive the data from the Socket Client . My question is , if I have this code that create 3 Receive Async Call : ...
4
by: Brian Rice | last post by:
I have a socket application that is sending and receiving packets asynchronously. It works great except, when I receive packets that are larger than my receive buffer which then generate several...
1
by: John Sheppard | last post by:
Thanks to everyone that responded to my previous Socket Programming question. Now I have run into some behavior that I don't quite understand. Programming environment. VS.NET 2003, C#, Windows...
6
by: Steve Richter | last post by:
I dont get the point of socket.BeginReceive and socket.EndReceive. As I understand it, BeginReceive will start a 2nd thread, call the ReceiveCallback delegate in the 2nd thread, then block until...
7
by: Colin | last post by:
I'm writing a little console socket server but I'm having some difficulty. Can I ask your advice - where is the best place to get some help on that topic? It would be nice if some people who knew...
1
by: Marty | last post by:
Hi, I have a socket that always seek for incoming data. Between Point A and Point B, the socket (mySocket is closed and assigned to nothing in another part of my program (happen when a...
9
by: Macca | last post by:
Hi, I have a synchronous socket server which my app uses to read data from clients. To test this I have a simulated client that sends 100 byte packets. I have set up the socket server so...
0
by: Macca | last post by:
Hi, I am writing an asychronous socket server to handle 20+ simulataneous connections. I have used the example in MSDN as a base. The code is shown at end of question. Each connection has a...
2
by: O.B. | last post by:
In the following code snippet, the thread successfully makes it to the line where it waits for data to be received. Then the client closes the connection. The thread wakes up and returns from the...
6
by: Pat B | last post by:
Hi, I'm writing my own implementation of the Gnutella P2P protocol using C#. I have implemented it using BeginReceive and EndReceive calls so as not to block when waiting for data from the...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.