question

Arsium-4135 avatar image
0 Votes"
Arsium-4135 asked Arsium-4135 commented

Problem with asynchronous socket

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


dotnet-csharp
· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @Arsium-4135, I cannot decompress your project file.

94197-1.png
Please use onedrive to provide it again. Also, please simplify your project as much as possible and keep only the smallest case that can reproduce the problem, so that we can analyze your problem more easily.

0 Votes 0 ·
1.png (17.9 KiB)

And to reproduce the problem go to client side in form1,

Replace the paths I set (2 paths for big file += 500MB)

And then click on button 2 to send those two files

The server will write them infinity

0 Votes 0 ·

1 Answer

TimonYang-MSFT avatar image
0 Votes"
TimonYang-MSFT answered Arsium-4135 commented

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.

· 9
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Okay for my button i do that (to not make ui freezing) :

Task.Run(() => {
SendPacket P = new SendPacket(S);
byte[] b = System.IO.File.ReadAllBytes(Application.StartupPath + "\\azezae.rar");
P.Send(b);
});

but the server always writes infinity files

0 Votes 0 ·

I added simple messagebox : MessageBox.Show("Done !"); to see if the sending packet is launched more than one time and it is not.

0 Votes 0 ·

@Arsium-4135
I used your code without any changes (except the file name and IP) and tested it a few times, but as long as I make sure to click it only once, then there will be no problems.
You can add the following code to the click event to try:

             Task.Run(() =>
             {
                 SendPacket P = new SendPacket(S);
                 byte[] b = System.IO.File.ReadAllBytes(Application.StartupPath + "\\RootFolder.zip");
                 P.Send(b);
             });
             Button button = sender as Button;
             button.Enabled = false;

I can see that you posted an answer and then deleted it (only you and Microsoft employees can see it). What's the situation now?

0 Votes 0 ·

@Arsium-4135
How is the problem going? If there are still problems that have not been resolved, please let me know and we can discuss further.

1 Vote 1 ·
Show more comments