| |

VerySource

 Forgot password?
 Register
Search
View: 1086|Reply: 3

Ask about asynchronous socket server side issues

[Copy link]

1

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-9-25 09:30:01
| Show all posts |Read mode
What I do is asynchronous socket server side. The approximate code is as follows:

private void form_load()
{
  Get server IP and listening port
  Start the listening thread
}

private void listening()
{
  while(true)
  {
     lisdone.rest();
     mysocket.beginaccept (call the asynchronous listener callback function);
     lisdone.waitone();
  }
}

private void acceptcallback()//Asynchronous callback function
{
  Start the receiving thread for the current link client (as long as there is a client link, start a thread for this client)
}

private void receivedata()
{
  while(true)
{
   try
  {
   receivedone.rest();
   mysocket.beginreceive (call the receive callback function)
   receivedoen.waitone();
   }
  catch
  {
   mysocket.shutdown(shutdown.receive);
   mysocket.close();
   return;
   }
}
}

private void receivecallback()
{
  receivedone.set();
  Receive data
}

The above is roughly the process of SOCKET server-side linking from client to receiving client data. Now I have a problem. My current method is as long as there are customer links. Just create a thread for receiving this client for this client in the listening callback function. This program can also be received normally. But when the customer quit halfway, the CPU usage of my program soared to 100. What is going on? ? When the link client is prominent, how should I release the client's link object? How to release the thread created after this client link comes up? ? Thank you, I look forward to replying. . .
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-9-25 21:15:01
| Show all posts
It’s better not to wait silly, you can set the waiting timeout or use other ways to deal with
Reply

Use magic Report

1

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

 Author| Post time: 2020-9-26 11:30:01
| Show all posts
Are you talking about waiting for monitoring or waiting for data collection? My problem now is that when there is a client link, I will create a thread for the user in the listener callback function for communication. The server only receives and does not send. So, when the client exits, the server CPU surges by 100. What is causing this? How should I release the sockets and threads created for this client when each client links? ? Thank you, look forward to it? ? ?
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-9-27 20:00:01
| Show all posts
The reason is an infinite loop. When it is detected that the data is empty or the connection is disconnected, the process XX is dropped. I also encountered it yesterday
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list