Problem with asynchronous socket

Arsium ***** 331 Reputation points
2021-05-05T09:11:33.517+00:00

Hello dear Sirs,

I'm working with async socket (I used to work with normal socket) and it works perfectly except that server writes files infinity

Link to my sample project : https://mega.nz/file/dMwT0SrB#CSpYaSXUwjSRL2P33noG_ytkvf9DwmjX4prrq0kb8VM

If someone can take a look , I will be grateful.

Arsium.

93830-capture-decran-633.png

Developer technologies | C#
{count} votes

1 answer

Sort by: Most helpful
  1. Timon Yang-MSFT 9,606 Reputation points
    2021-05-11T07:41:12.36+00:00

    I tested your code, the problem is that you clicked the same send button multiple times.

    It may be that the code is running on the UI thread, causing the form to get stuck after you click the button once, making you think that you did not successfully click another button.

    If you make sure to click each send button only once, the file is sent successfully.

    One idea is to set enabled to false after the send button is clicked, and then add a send method on the server side and send a message to the client after the data reception is completed, and then set enabled to true after the client receives the message.

    For specific code, please refer to the case provided in the document:

    Asynchronous Server Socket Example

    In addition, consider moving the work code to other threads. For users, continuous UI freezes during sending will ruin their experience.

    Update (5/20):
    I test the code again, but I get the correct result every time.

    This made me realize that I might have modified part of the code but forgot.

    So I re-downloaded the file you provided, and after careful comparison, I found that I had made some changes to Server.cs.

            public Socket S;  
            public Server(int Port)  
            {  
                S = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);  
                this.Port = Port;  
                StartListening();  
            }  
            private void StartListening()  
            {  
                try  
                {  
                    MessageBox.Show($"Listening started port:{Port} protocol type: {ProtocolType.Tcp}");  
                    S.Bind(new IPEndPoint(IPAddress.Any, Port));  
                    S.Listen(20);  
                    S.BeginAccept(AcceptCallback, S);  
                }  
                catch (Exception ex)  
                {  
                    throw new Exception("listening error" + ex);  
                }  
            }  
    

    To

            public Socket S;  
            public Server(int Port)  
            {  
                this.Port = Port;  
                StartListening();  
            }  
            
            private void StartListening()  
            {  
                try  
                {  
                    S = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);  
                    MessageBox.Show($"Listening started port:{Port} protocol type: {ProtocolType.Tcp}");  
                    S.Bind(new IPEndPoint(IPAddress.Any, Port));  
                    S.Listen(20);  
                    S.BeginAccept(AcceptCallback, S);  
                }  
                catch (Exception ex)  
                {  
                    throw new Exception("listening error" + ex);  
                }  
            }  
    

    Put the initialization of the Socket in the StartListening method instead of the constructor.

    In many tests after the modification, this problem did not occur again.


    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.