Socket.BeginReceive()

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • TP-Software

    #1

    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 AsyncReadCallBa ck(IAsyncResult asyncResult)

    {

    //Get the state object

    StateObject state = (StateObject) asyncResult.Asy ncState;


    //Get the socket

    Socket socket = state.workSocke t;

    try

    {

    //Read data from the client socket

    int bytesRead = socket.EndRecei ve(asyncResult) ;


    if (bytesRead > 0)

    {

    state.sb.Append (System.Text.En coding.ASCII.Ge tString(state.b uffer,0,bytesRe a
    d));

    socket.BeginRec eive(state.buff er,0,StateObjec t.BufferSize,0, new
    AsyncCallback(t his.AsyncReadCa llBack),state);

    }

    }

    catch (SocketExceptio n e)

    {

    if (e.ErrorCode != 64)

    {

    Console.WriteLi ne(e.ToString() );

    }

    }

    }

    }



    Thnx for any help.

    TP.


  • Jon Skeet

    #2
    Re: Socket.BeginRec eive()

    TP-Software <tpsoftware@use rs.sourceforgE. net> wrote:[color=blue]
    > This code doesn't seem to work
    > it always says "there is more data" and also
    > this method is only called once[/color]

    <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 - <skeet@pobox.co m>
    Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

    If replying to the group, please do not mail me too

    Comment

    • TP-Software

      #3
      Re: Socket.BeginRec eive()

      Hi,

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

      private void AsyncReadCallBa ck(IAsyncResult asyncResult)
      {
      StateObject state = (StateObject) asyncResult.Asy ncState;
      Socket socket = state.workSocke t;

      int bytesRead = socket.EndRecei ve(asyncResult) ;

      if (bytesRead > 0)
      {
      state.sb.Append (Encoding.ASCII .GetString(stat e.buffer,0,byte sRead));
      socket.BeginRec eive(state.buff er,0,StateObjec t.BufferSize,0,
      new
      AsyncCallback(t his.AsyncReadCa llBack),state); ;
      }
      else
      {
      if (state.sb.Lengt h > 1)
      {
      //All the data has been read, and there is actually data
      string str = state.sb.ToStri ng();
      Console.WriteLi ne("Out: {0}",str);
      }

      socket.Close();
      }


      The exception I get is thrown at this line:
      int bytesRead = socket.EndRecei ve(asyncResult) ;

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

      Does this help to explain my problem?

      Thnx.
      Tim.


      Comment

      • Jon Skeet

        #4
        Re: Socket.BeginRec eive()

        TP-Software <tpsoftware@use rs.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.Sock ets;
        using System.IO;
        using System.Text;
        using System.Threadin g;

        public class TestClient
        {
        static void Main()
        {
        Socket skt = new Socket(AddressF amily.InterNetw ork,
        SocketType.Stre am,
        ProtocolType.Tc p);

        IPHostEntry ipa = Dns.Resolve("lo calhost");
        IPEndPoint ip = new IPEndPoint (ipa.AddressLis t[0], 1234);

        skt.Connect (ip);

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

        skt.BeginReceiv e (state.data, 0, state.data.Leng th, 0,
        new AsyncCallback (AsyncReadCallB ack), state);

        Thread.Sleep(10 000);
        }

        private static void AsyncReadCallBa ck(IAsyncResult asyncResult)
        {
        State state = (State) asyncResult.Asy ncState;
        Socket socket = state.skt;

        int bytesRead = socket.EndRecei ve(asyncResult) ;

        if (bytesRead >= 0)
        {
        Console.WriteLi ne (Encoding.ASCII .GetString
        (state.data, 0, bytesRead));
        socket.BeginRec eive(state.data , 0, state.data.Leng th, 0,
        new AsyncCallback(A syncReadCallBac k), state);
        }
        else
        {
        Console.WriteLi ne ("Finished." );
        socket.Close();
        }

        }

        class State
        {
        internal byte[] data;
        internal Socket skt;
        }
        }


        TestServer.cs:
        using System;
        using System.Net;
        using System.Net.Sock ets;
        using System.IO;
        using System.Text;

        public class TestServer
        {
        static void Main()
        {
        byte[] data = Encoding.ASCII. GetBytes
        ("abcdefghijklm nopqrstuvwxyzab cdefghijklm"+
        "nopqrstuvwxyza bcdefghijklmnop qrstuvwxyz");

        IPAddress ipAddress = Dns.Resolve("lo calhost").Addre ssList[0];
        TcpListener listener = new TcpListener(ipA ddress, 1234);

        listener.Start( );

        TcpClient client = listener.Accept TcpClient();
        Console.WriteLi ne("Connected!" );

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

        while (true)
        {
        stream.Write (data, 0, data.Length);
        }
        }
        }


        --
        Jon Skeet - <skeet@pobox.co m>
        Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

        If replying to the group, please do not mail me too

        Comment

        Working...