Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Asynchronous Technical question !!

Asynchronous Technical question !!

Scheduled Pinned Locked Moved C#
csssysadmindata-structureshelpquestion
1 Posts 1 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    Hussam Fattahi
    wrote on last edited by
    #1

    I'm writing a client\server application, and I have a problem in server part because I want that part to manage more than one client at time, I used asynchronous operations and here comes the problem, I don't know how these function exactly works( I mean in the runtime environment) , this is the code summarized as possible, in the form constructer

    server = new Socket(AddressFamily.InterNetwork, SocketType.Stream,
                    ProtocolType.Tcp);
                IPEndPoint ipep = new IPEndPoint(IPAddress.Parse("169.254.25.129"), 9050);
                server.Bind(ipep);
                server.Listen(-1);
                server.BeginAccept(new AsyncCallback(AcceptConn), server);
    

    The asynchronous callback function (AcceptConn) accepts one incoming connection and assign a socket to deal with this connection, and at the end of the function I call the BeginAccept function again to begin to accept other incoming connections. I have a class ConnectionInformation to keep all the information a bout one connection, and the ConnSock is a property to access the socket member in the class And the sockCount to keep track of instances of ConnectionInformation in the array connInfo.

    public void AcceptConn(IAsyncResult iar)
            {
                Socket oldServer = (Socket)iar.AsyncState;            
                connInfo[sockCount].ConnSock = oldServer.EndAccept(iar);
                
                //begin an asynch receive operation, to receive any data coming from remote host
                connInfo[sockCount].ConnSock.BeginReceive(DataReceiveBuffer, 0, DataReceiveBuffer.Length,
                    SocketFlags.None, new AsyncCallback(ReceiveData), connInfo[sockCount].ConnSock);
                
                //Fill information into data grid
                FillInDataGridView(connInfo[sockCount]);
                
                //begin an asynch accept operation, to listen to any incoming connection
                server.BeginAccept(new AsyncCallback(AcceptConn), server);            
            } 
    

    And in the asynchronous callback function(ReceoveData) for the BeginReceive operation I do the same

    public void ReceiveData(IAsyncResult iar)
            {
                    Socket remote = (Socket)iar.AsyncState;
                    int recv = remote.EndReceive(iar);
                    
                    // to begin other async receive operation
                    remote.BeginReceive(DataReceiveBuffer, 0, DataReceiveBuffer.Length, SocketFlags.None,
                        new
    
    1 Reply Last reply
    0
    Reply
    • Reply as topic
    Log in to reply
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes


    • Login

    • Don't have an account? Register

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • World
    • Users
    • Groups