Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6649223
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T00:44:53+00:00 2026-05-26T00:44:53+00:00

I currently have a client-server application that involves a Silverlight client and a .NET

  • 0

I currently have a client-server application that involves a Silverlight client and a .NET server. The .NET portion uses the Tcp classes provided in System.Net.Sockets namespace whereas the Silverlight client uses raw sockets. I’m porting into this from code that currently uses the HttpListener classes because it doesn’t suit my needs. The Http classes, though, have on the SL side the ability to use Begin* and End* style asynchronous methods that allow me to specify a handler once the operation has completed. I’m having trouble getting this to work with the new system. My current strategy is to include the handler as part of the UserToken. However, it seems that this token is not getting updated.

Here is some redacted code. I am able to get the two sides to talk to each other, but it seems the correct UserToken is not being sent.

public class ClientUserToken
{
    public Handler Handler { get; set; }
    public string Test { get; set; }

    public ClientUserToken(Handler handler, string test)
    {
        Handler = handler;
        Test = test;
    }
}

public class SocketClient
{
    private Socket _clientSocket;
    private string _ipAddress;
    private int _port;

    private void OpenSocket()
    {
        var endPoint = new DnsEndPoint(_ipAddress, _port);

        SocketAsyncEventArgs args = new SocketAsyncEventArgs();
        args.UserToken = new ClientUserToken(null, "foo");
        args.RemoteEndPoint = endPoint;
        args.Completed += OnSocketCompleted;

        _clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
        _clientSocket.ConnectAsync(args);
    }

    void OnSocketCompleted(object sender, SocketAsyncEventArgs e)
    {
        switch (e.LastOperation)
        {
            case SocketAsyncOperation.Connect: ProcessConnect(e); break;
            case SocketAsyncOperation.Receive: ProcessReceive(e); break;
            case SocketAsyncOperation.Send: ProcessSend(e); break; // this never gets called
        }
    }

    void ProcessConnect(SocketAsyncEventArgs e)
    {
        if (e.SocketError == SocketError.Success)
        {
            byte[] response = new byte[1024];
            e.SetBuffer(response, 0, response.Length);
            _clientSocket.ReceiveAsync(e);
        }
    }

    void ProcessReceive(SocketAsyncEventArgs e)
    {
        if (e.SocketError == SocketError.Success && e.BytesTransferred > 0)
        {
            var userToken = e.UserToken as ClientUserToken; // this token is always the one set in ProcessConnect

            // process the data

            if (!_clientSocket.ReceiveAsync(e))
            {
                ProcessReceive(e);
            }
        }
    }

    // this is never called, for some reason
    void ProcessSend(SocketAsyncEventArgs e)
    {
        if (e.SocketError == SocketError.Success)
        {
            var userToken = e.UserToken as ClientUserToken;

            if (!_clientSocket.ReceiveAsync(e))
            {
                ProcessReceive(e);
            }
        }
    }

    // this is the public API that users use to actually send data across
    public void SendToServer(byte[] data, int len, Handler handler)
    {
        SocketAsyncEventArgs args = new SocketAsyncEventArgs();

        args.UserToken = new ClientUserToken(handler, "bar");
        args.SetBuffer(data, 0, len);

        if (!_clientSocket.SendAsync(args))
        {
            ProcessReceive(args);
        }
    }
}

As the comments above suggest, in ProcessReceive, userToken.Test is always “foo” and userToken.Handler is always null.

I have so far not been able to break into ProcessSend so I can’t see what SocketAsyncEventArgs it actually sent. Anyone have a clue why this event isn’t firing (Completed after a Send)? Am I screwing up my SendToServer function?

I realize there may be other existing problems with synchronization and such, but I don’t think that’s the issue here. One thing I did try was setting up a ManualResetEvent to ensure that no one sends data to the server before the connection has been completed (ProcessConnect) but that did not solve the issue, either.

Any help will be appreciated!

EDIT: So the reason this is happening is because when I call ReceiveAsync in the ProcessConnect function, it is being used when the server is sending back the response for my data. Hence, UserToken “foo” is present in the handler. The next time the server sends data, the ReceiveAsync uses the args with the UserToken “bar”. So it is kind of out of sync, for the duplex communication bit. I can’t ensure that the SocketAsyncEventArgs that I sent from the client-side is the same one that is used on the response. It seems like the only solution is to have the SL client open two sockets–one for server-initiated data and the other for client-initiated requests. However, this means I’m not taking advantage of the duplex nature.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T00:44:54+00:00Added an answer on May 26, 2026 at 12:44 am

    This model won’t work because I’m creating a new SocketAsyncEventArgs on each send, which means that the data can come back on any of these args. I’ve been moving towards a model with a pool of SocketAsyncEventArgs and each client can only have one request/response at a time.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I currently have an application that sends XML over TCP sockets from windows client
I have a .NET 1.1 application that currently connects to a SQL Server 2000
I currently have an archaic system of client records that I am trying to
I currently have a client website on the Grid server on MediaTemple that will
We have a client/server application with a rich client front end (in .Net) and
We have a client and server application currently testing on the same Windows 7
I have a desktop client application that is talking to a server application through
I have a Java web application that currently uses Log4J for logging. I'd like
I have a client/server application that runs on an intranet, and I have the
I currently have a Firebird server on Windows and Windows desktop client applications for

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.