Home > Net >  C # socket threads to receive new data, won't accept it, and the receive function infinite loop
C # socket threads to receive new data, won't accept it, and the receive function infinite loop

Time:09-15

C # socket threads opened the receive, then infinite loop receiving data, while (true) {... } will continue to receive data, is arguably breaking point will be stopped at any time, but I this is breakpoint will not come in in a minute, the server send a heartbeat packets every 10 seconds, can receive data at first, in a few minutes or more than ten minutes every time (time), can't into the infinite loop, also not receiving data, this is how to return a responsibility? But TCP tools can be received,

The code is as follows:
TCP=new Thread (new ThreadStart (ProcessData));
TCP. The Start ();


Private void ProcessData ()
{
Byte [] tmp_package;
While (true)
{
Try
{
Byte [] TMP=new byte [20000].
Int recv=socket. The Receive (TMP);
Byte [] radar=new byte [recv];
Buffer. BlockCopy (TMP, 0, radar, 0, recv);
RadarPackageList. Add (radar);
_writeListIndex + +;

While (_writeListIndex & gt; 0)
{
The lock (RadarPackageList)
{
Tmp_package=RadarPackageList [0];
RadarPackageList. RemoveAt (0);
_writeListIndex -;
}
. String rcvString=System. Text. Encoding UTF8. Get string (tmp_package);
String s2=rcvString. Substring (startIndex rcvString. Length - startIndex);
if (! S2. The Contains (" direction_finding ") | | startIndex & lt;=0)
{
Console. WriteLine (" heartbeat data -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- "+ DateTime. Now. The ToString ());
continue;
}

}
}
}
}
The catch (Exception ex)
{

}

}


CodePudding user response:

Code is wrong, ACCEPT it in a WHILE to go,

CodePudding user response:

Consult the code is wrong? Just began to run, while the Console. WriteLine (" heartbeat data -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- "+ DateTime. Now. The ToString ()); This sentence can't print, while in the infinite loop could not to the breaking point

CodePudding user response:

Yes ah, you eat abnormalities, abnormal thread does not naturally out of the race?

CodePudding user response:

reference 4 floor wanghui0380 response:
yes ah, have you had abnormal, the abnormal thread does not naturally out of the race?



I print the words in the catch, the program without abnormal jump, the heartbeat prompt printing can't print, besides the try... Catch in the while (true), and even to catch, and should not be out of the while (true)

CodePudding user response:

That you wrote, the Console. WriteLine (" while in the operation of the -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- "+ DateTime. Now. The ToString ()); In the while next

Said, look at you without printing, while really still while

Words don't print the words, also do not represent the while not allowed, only represent the if (! S2. The Contains (" direction_finding ") | | startIndex & lt; Wood to
=0) conditions
As for not up to the cause of it, is also very simple, this code is run by coincidence, rely on the logic is that happened to receive direction_finding each time, since it is run by coincidence, so also can be happened to N minutes later there was a mismatch, every time received no direction_finding
he
May be received 3 times in a row is a
Direction_findin
Gdirection_
_findingdire

CodePudding user response:

Not necessarily is a problem of this code, you are a web or desktop application? Web page, this passage is in the Session or just started in the web page code?

CodePudding user response:

refer to 7th floor icoolno1 response:
does not necessarily is a problem of this code, you are a web site or desktop application? Web page, this passage is in the Session or just started in the web page code?



Is a console application

CodePudding user response:

In the int recv=socket. The Receive (TMP); Statement after add printing code, the content of the TMP and see if it is sticky package, unpacking,

CodePudding user response:

references 9 f datafansbj response:
in int recv=socket. The Receive (TMP); Statement after add printing code, the content of the TMP and see if it is sticky package, unpacking,


-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
Stick package, unpacking will have an impact of the socket receiving data?
If the sticky package, unpacking, have a solution?

CodePudding user response:

Socket. The Receive () timeout problems

CodePudding user response:

11 references of seized Qian love reply:
Socket. The Receive () timeout problems


The overtime will no longer receive data? Server every 10 seconds to send a heartbeat packets, 10 seconds too long

CodePudding user response:

refer to 6th floor wanghui0380 response:
that you write so, Console. WriteLine (" while in the operation of the -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- "+ DateTime. Now. The ToString ()); In the while next

Said, look at you without printing, while really still while

Words don't print the words, also do not represent the while not allowed, only represent the if (! S2. The Contains (" direction_finding ") | | startIndex & lt; Wood to
=0) conditions
As for not up to the cause of it, is also very simple, this code is run by coincidence, rely on the logic is that happened to receive direction_finding each time, since it is run by coincidence, so also can be happened to N minutes later there was a mismatch, every time received no direction_finding
he
May be received 3 times in a row is a
Direction_findin
Gdirection_
_findingdire


-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
According to your method tried, will Console. WriteLine (" while in the operation of the -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - ") in this sentence the while (true), run twice, the first run for more than an hour has been normal, turned off to run an application, running for more than 20 minutes, what all don't print, while (true) not interrupt point

CodePudding user response:

Is that the thread has you hung up the

Then Application. ThreadException +=event

Articulated ThreadException abnormal events, see exactly why hanging threads,

Ps: if the project has just started, personally, I suggest you directly into the dotnetty dotnettyclient didn't also a few lines of code

 group=new MultithreadEventLoopGroup (3); 


The bootstrap=new bootstrap ();
The bootstrap
Group (Group)
. Channel ()//using TCP protocol
Option (ChannelOption TcpNodelay, true)
Option (ChannelOption ConnectTimeout, TimeSpan. FromSeconds (3))//connection timeout definition
Handler (new ActionChannelInitializer (channel=& gt;
{
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  •  Tags:  
  • C#
  • Related