Socket.BeginReceive()

  • Thread starter Thread starter TP-Software
  • Start date Start date
T

TP-Software

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.GetString(state.buffer,0,bytesRea
d));

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

}

}

catch (SocketException e)

{

if (e.ErrorCode != 64)

{

Console.WriteLine(e.ToString());

}

}

}

}



Thnx for any help.

TP.
 
TP-Software said:
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.)
 
Hi,

Even using the following code (based on this page:
http://msdn.microsoft.com/library/d...ystemNetSocketsSocketClassEndReceiveTopic.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.buffer,0,bytesRead));
socket.BeginReceive(state.buffer,0,StateObject.BufferSize,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.
 
<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);
}
}
}
 
Back
Top