473,463 Members | 1,495 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 10463
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...
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,...
1
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...
0
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
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...
0
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...
1
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 ...

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.