Click here to Skip to main content
15,896,497 members
Home / Discussions / C#
   

C#

 
GeneralRe: Displaying edit mask as 95.25% and value of 0.9525 Pin
DaveyM696-Nov-08 3:05
professionalDaveyM696-Nov-08 3:05 
QuestionAssume 3 or 5 textbox which will be filled by user at runtime and these values should assigned to radio button text which will be created dynamically. [modified] Pin
B875-Nov-08 22:18
B875-Nov-08 22:18 
AnswerRe: Consider 3 or 5 textbox which will be filled by user at runtime and these values should assigned to radio button text will be created dynamically. PinPopular
J4amieC5-Nov-08 22:22
J4amieC5-Nov-08 22:22 
GeneralRe: Consider 3 or 5 textbox which will be filled by user at runtime and these values should assigned to radio button text will be created dynamically. Pin
B875-Nov-08 22:44
B875-Nov-08 22:44 
GeneralRe: Consider 3 or 5 textbox which will be filled by user at runtime and these values should assigned to radio button text will be created dynamically. Pin
Shyam Bharath5-Nov-08 22:52
Shyam Bharath5-Nov-08 22:52 
GeneralRe: Consider 3 or 5 textbox which will be filled by user at runtime and these values should assigned to radio button text will be created dynamically. Pin
J4amieC5-Nov-08 23:18
J4amieC5-Nov-08 23:18 
AnswerRe: [modified - To remove stupidly long subject line] Pin
DaveyM695-Nov-08 23:27
professionalDaveyM695-Nov-08 23:27 
QuestionAsync callback usage in TCP server application Pin
Metal765-Nov-08 21:58
Metal765-Nov-08 21:58 
I'm developing an application in which multiple clients exchange data on a TCP link with a server using a custom protocol. I've chosen the asynchronous approach to implement the server.

Below I've attached an excerpt of my code, showing the points discussed in my questions. I have several doubts about correct asynchronous callback usage, and I could not find anything in the docs:

1) Suppose you have several clients connected. Do all the callbacks (OnClientConnect, OnClientRead...) "live" in the same thread? That is, while I'm inside OnClientRead to serve one of the clients, can my code be interrupted by an OnClientRead from another client?

I performed a simple investigation, and it seems that OnClientRead for different clients are called from the same thread. So, it seems that OnClientRead execution never gets interrupted. Am I right? Is this always true? If so, I think I can safely remove all the lock statements for clientList access.

2) Is it correct to always launch a new AsyncCallback every time? e.g. new AsyncCallback(OnClientRead) inside OnClientRead. Does this waste resources? Should I keep a different AsyncCallback for each client and reuse it?

Regards,
Andrea

class ClientState
{
    // Holds client state: I/O buffer, socket, IP endpoint etc...
}

class NetworkServer
{
    // Holds list of client states
    private List<clientstate> clientList;
    private object lockObject = new object();

    // Start server node
    public void Start(int backlog)
    {
        clientList = new List<clientstate>();
    
       // Listen to incoming connections asynchronously
        serverSocket.Listen(backlog);
        serverSocket.BeginAccept(new AsyncCallback(OnClientConnect), null);   
    }

    // Callback on client connection
    private void OnClientConnect(IAsyncResult state)
    {
        // Accepts incoming connection
        Socket clientSocket = serverSocket.EndAccept(state);
    
        // Create client state and fill it with state values    
        ClientState clientState = new ClientState();
        ...

        // Add to client list
        lock(lockObject)
        {
            clientList.Add(clientState);
        }
        
        // Start receiving from client
        clientSocket.BeginReceive(clientState.Buffer, 
                                  0,
                                  clientState.Buffer.Length, 
                                  SocketFlags.None,
                                  new AsyncCallback(OnClientRead),
                                  clientState);
    
        // Listen for a new incoming connection
        serverSocket.BeginAccept(new AsyncCallback(OnClientConnect), null);
    }

    // Callback on data ready from a client
    private void OnClientRead(IAsyncResult state)
    {
        // Terminate async read
        SocketError socketError;
        int numBytes = clientSocket.EndReceive(state, out socketError);
    
        if ((numBytes == 0) || (socketError != SocketError.Success))
        {
            // Client closed or receive error: close client socket
            clientSocket.Shutdown(SocketShutdown.Both);
            clientSocket.Close();

            // Remove client from list
            lock(lockObject)
            {
                clientList.Remove(clientState);
            }
        } 
        else 
        {
            // Process data 
            ...

            // Launch a new async read from the client
            clientSocket.BeginReceive(clientState.Buffer,
                                  0,
                                  clientState.Buffer.Length,
                                  SocketFlags.None,
                                  new AsyncCallback(OnClientRead),
                                  clientState); 
        }
    }
}</clientstate></clientstate>

AnswerRe: Async callback usage in TCP server application Pin
Nicholas Butler6-Nov-08 0:18
sitebuilderNicholas Butler6-Nov-08 0:18 
GeneralRe: Async callback usage in TCP server application Pin
Metal766-Nov-08 0:56
Metal766-Nov-08 0:56 
GeneralRe: Async callback usage in TCP server application Pin
Nicholas Butler6-Nov-08 1:41
sitebuilderNicholas Butler6-Nov-08 1:41 
GeneralRe: Async callback usage in TCP server application Pin
Metal766-Nov-08 2:14
Metal766-Nov-08 2:14 
Questioncrystal report with dynamic columns? Pin
kieu duc khuong5-Nov-08 21:21
kieu duc khuong5-Nov-08 21:21 
AnswerRe: crystal report with dynamic columns? Pin
Programm3r6-Nov-08 1:22
Programm3r6-Nov-08 1:22 
AnswerRe: crystal report with dynamic columns? Pin
nelsonpaixao6-Nov-08 13:27
nelsonpaixao6-Nov-08 13:27 
GeneralRe: crystal report with dynamic columns? Pin
kieu duc khuong6-Nov-08 14:23
kieu duc khuong6-Nov-08 14:23 
GeneralRe: crystal report with dynamic columns? Pin
nelsonpaixao7-Nov-08 14:06
nelsonpaixao7-Nov-08 14:06 
QuestionContextSwitchDeadlock Pin
dec825-Nov-08 21:03
dec825-Nov-08 21:03 
AnswerRe: ContextSwitchDeadlock Pin
Nicholas Butler5-Nov-08 23:41
sitebuilderNicholas Butler5-Nov-08 23:41 
AnswerRe: ContextSwitchDeadlock Pin
DaveyM695-Nov-08 23:43
professionalDaveyM695-Nov-08 23:43 
QuestionFunction keys Pin
sram155-Nov-08 20:40
sram155-Nov-08 20:40 
AnswerRe: Function keys Pin
leppie5-Nov-08 22:38
leppie5-Nov-08 22:38 
AnswerCP IGNORE: Cross post three forums Pin
leckey6-Nov-08 10:06
leckey6-Nov-08 10:06 
QuestionProblem running a .bat file from a C# forms application Pin
Member 41835605-Nov-08 20:08
Member 41835605-Nov-08 20:08 
AnswerRe: Problem running a .bat file from a C# forms application Pin
Franck Paquier5-Nov-08 20:54
Franck Paquier5-Nov-08 20:54 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.