Asynchronous receive data are normal, is a fixed-length TLV package, but received after all are directly written bufflist after the GetData out of processing, found that sometimes there will be a switch situation, thought is a thread safety problem, later joined the lock section will be bufflist inside the code lock, there will still be a switch phenomenon, Causes the package to fail, consulted some people, the answer is still thread safety issues, I would like to ask if there is a better way to deal with, such as the need to post code, because the theory I understand, nor is the problem of solving the package (this solution is just a simple way to write) now needs to be able to actually solve the problem of caching packets
C # code
User. Bufflist.insertbytearray (data); Insert packet buffer pool Do {try {byte[] buff = user. Bufflist.getdata (); Remove the existing packet if (buff = null) break; DataHandler (buff, user); Each packet instruction is processed} catch {//Fetch packet Exception}} while (true);
C # code
public class Bufflist {public Object locklist = new Object (), public list< byte> bytelist {get; set; List () {bytelist = new list< byte> ();} public void Clecr () {lock (locklist) {bytelist.clear ();}} Nsertbytearray (byte[] Data) {lock (locklist) {bytelist.addrange (Data);}} public Int16 Readint16wgh () {byte[] temp = New byte[2]; Bytelist.copyto (0, temp, 0, 2); return bitconverter.toint16 (temp, 0); public enum Cmdtype {XXX = 1101,} public byte[] GetData () {lock (locklist) {if (Bytelist.count >) {if (CMDT ype) readint16wgh () = = cmdtype.xxx) {byte[] temp = new byte[289]; Bytelist.copyto (0, temp, 0, 289); if (packet. Checkwghdata (temp)) {bytelist.removerange (0, 289); return temp;} else {//package exception, cannot parse correctly} return null; else {return null;}} }} BDMH
2011-06-29 12:34:37 It is recommended that reception and data processing be separated, received and placed in a list, and individually threaded to the list individually Hudingwen
2011-06-29 12:39:13 has been separated, you see the code is received and entrusted to another thread for processing, that is, I posted the code is on another thread, but not necessarily a thread, the system can not handle the process may be greater than a thread Hudingwen
2011-06-29 13:46:22 which brother has a solution ah djc7811
2011-06-29 13:50:33 Reference to BDMH 1 floor reply:
It is recommended that receive and data processing be separated, received and placed in a list, and individual threads are processed individually on the list
After receiving the data, asynchronous processing, you are received asynchronously, and then add an asynchronous callback processing, I have previously dealt with, there is no problem, now with Delphi Development, no that code Hudingwen
2011-06-29 14:03:15 Reference to djc7811 4 floor reply:
A reply to the 1 floor bdmh is quoted:
It is recommended that receive and data processing be separated, received and placed in a list, and individual threads are processed individually on the list
After receiving the data, asynchronous processing, you are received asynchronously, and then add an asynchronous callback processing, I have previously dealt with, there is no problem, now with Delphi Development, no that code
I am now asynchronously receiving an asynchronous callback, the key is my caching mechanism is still a thread-safe problem, about 100 packets per second, the packet input callback is a delegate, I remember the system will be with some mechanism to open one or more threads to process the data, if so, There will probably be more than one thread for the insert operation, which will cause the packet to be scrambled, but I've added the lock lock and still have a problem djc7811
2011-06-29 14:13:12 1, the sending end sends data interval time is longer;
2, receiving end with a linked list to receive, Edge receiving edge processing data, receive add new data, processing finished delete old data Hudingwen
2011-06-29 14:15:26 Reference to djc7811 6 floor reply:
1, send the end of the data interval for a long time;
2, receiving end with a linked list to receive, Edge receiving edge processing data, receive add new data, processing finished delete old data
Dude, you didn't look at me. Code, is the edge of the receiving side processing data, is received when add, processing finished deltete ... The key is what I said above, thread-safety issues djc7811
2011-06-29 14:17:23 without lock Hudingwen
2011-06-29 14:18:40 Reference to djc7811 8 floor reply:
No lock.
Without lock will also pack disorderly order, I was just beginning did not add, appear packet disorderly order, now added, still pack disorderly order Hudingwen
2011-06-29 14:34:35 which brother has a solution ah Hudingwen
2011-06-29 15:07:17 which brother has a solution ah hdw163
2011-06-30 12:43:15 to help the top, look for a master Hudingwen
2011-06-30 13:23:43 my top FJCFJCFJCFJC
2011-06-30-14:11:12 socket to send a byte stream is based on the number of bytes sent several times, so the receiver must be received several times, otherwise there may be data loss problems. huahua2000
2011-06-30 14:13:12 to set your thread as a background thread, processing efficiency will be faster. Hudingwen
2011-06-30 15:35:22 Reference to FJCFJCFJCFJC 14 floor reply:
Socket to send a byte stream is based on the number of bytes sent several times, so the receiver must be received several times before the line, or there may be data loss problems.
Socket is not lost packet, is inserted cache may be due to thread safety problems, resulting in the packet sequence problem, please see the previous floor Hudingwen
2011-06-30 16:50:30 Reference to huahua2000 15 floor reply:
Set your thread as a background thread, and the processing efficiency will be faster.
Big Brother I need to solve the problem of the way ... sp1234
2011-06-30 17:06:10 reference to Hudingwen 2 floor reply:
have been separated, you see the code is received and entrusted to another thread for processing, that is, I posted the code is on another thread, but not necessarily a thread, the system can not handle when it may be more than a thread
You are so funny, how can this "entrust another thread to process"? It's paradox. I can't think of it unless you're just chasing fashion, or your entire acceptance code is copied by someone else. sp1234
2011-06-30 17:06:58 that's too much of a face. If you are dealing with another thread, you are not responsible. Ulihss
2011-06-30 17:10:23 Do you pack the order because your connection is not intermittent. Hudingwen
2011-07-01 01:35:23 Reference to sp1234 18 floor reply:
A reply to the 2 floor Hudingwen is quoted:
have been separated, you see the code is received and entrusted to another thread for processing, that is, I posted the code is on another thread, but not necessarily a thread, the system can not handle when it may be more than a thread
You are so funny, how can this "entrust another thread to process"? It's paradox. I can't think of it unless you're just chasing fashion, or your entire acceptance code is copied by someone else.
I don't know where to tease. I'm not a big player either. If you're a good player, just give me a way out.
The following is I write the socket of the receiving, commissioned part, as for copy. Microsoft Official has SocketAsyncEventArgs example, one step the same see oneself
And the first piece of code in my post is in the Inputhandler function.
C # code
public delegate void Inputhandler (byte[] data, SocketAsyncEventArgs socketasync); Public Inputhandler Input {get; set;} void BeginReceive (SocketAsyncEventArgs e) {if (E.socketerror = = Socketerror.succe Ss&&e.bytestransferred> 0) {byte[] data = new byte[e.bytestransferred]; Array.copy (E.buffer, E.offset, data, 0, data. Length); if (this. Input!= null) this. Input.begininvoke (data, E, Receviecallback, Input); if (!e.acceptsocket.receiveasync (e)) {beginreceive (e);}} } Hudingwen
2011-07-01 01:40:07 Reference to ULIHSS 20 floor reply:
Are you in a disorderly order because your connection is not intermittent.
No, long connection, LAN, not broken, packet chaos is the result I observed, not necessarily correct, but asked some people, most of them are considered because of bufflist thread safety issues. Because I'm not sure if BeginInvoke (data, E, Receviecallback, Input) will be a single thread after that. If not, whether it is possible to cause the front has not written, after the first written, resulting in the packet Chaos sequence. Hudingwen
2011-07-01 01:47:14 Reference to sp1234 19 floor reply:
That's too much of a face. If you are dealing with another thread, you are not responsible.
I'm not quite sure what you said about taking responsibility, I understand that the socket itself as the main thread is responsible for a good handshake, collect, send these basic good, how to deal with the data received, and how to make the data sent out should be handled by other, to achieve the optimal, perhaps wrong, perhaps everyone's understanding is not the same Oonukeoo
2011-07-01 09:14:51 What you see in the "swap" phenomenon is actually an asynchronous debugging problem, in fact the order of the package is right ...
It is recommended that you carefully check the GetData () function, and then set the package when the situation is considered
Csnd_freeman
2011-07-01 10:58:14 just started learning socket, to pay attention to. tyler3180
2011-07-01 16:35:50
User. Bufflist.insertbytearray (data); Insert Packet Buffer pool
Plus mutual exclusion
Similar producer and consumer issues Hudingwen
2011-07-01 22:28:26 Reference to Oonukeoo 24 floor reply:
The "swap" phenomenon you see is actually a problem with asynchronous debugging, in fact the order of the package is right ...
It is recommended that you carefully check the GetData () function, and then set the package when the situation is considered
It is true that the problem with asynchronous invocations is similar to the problem of multithreaded reading and writing, that is, there may be more than one asynchronous invocation when the insert data is made, and it is possible that the second thread first acquires the lock and writes it first, and if you do not use this asynchronous invocation to call with blocking, Direct beginreceive can solve this problem, but it does not seem to be appropriate either in terms of efficiency or its own architecture. Hudingwen
2011-07-01 22:35:47 Reference to tyler3180 26 floor reply:
User. Bufflist.insertbytearray (data); Insert Packet Buffer pool
Plus mutual exclusion
Similar producer and consumer issues
You are referring to the use of user. Bufflist.insertbytearray (data) is mutually exclusive. You said I could try, but I don't think it's OK, because the input function itself is working on only one thread when the input function is called asynchronously. If not, it will be mutually exclusive. Because it seems to be the same as my mutex in bufflist, I've added a lock when I insert inside bufflist.insertbytearray (data), look at the code
C # code
public void Insertbytearray (byte[] Data {lock (locklist) {Bytelist.addrange (Data)}}
In principle, if a lock is required to execute Bufflist.insertbytearray (data), then there is a mutex mechanism when it executes the Insertbytearray function itself. Hudingwen
2011-07-02 21:42:22 There is no master AH ~ casually pointing and pointing ah Oonukeoo
2011-07-03 23:57:12 Reference to Hudingwen 27 floor reply:
A reply to the 24 floor Oonukeoo is quoted:
The "swap" phenomenon you see is actually a problem with asynchronous debugging, in fact the order of the package is right ...
It is recommended that you carefully check the GetData () function, and then set the package when the situation is considered
It's really a problem with an asynchronous call, is similar to the problem of multithreaded reading and writing, that is, insert data, there may be more than one asynchronous call, it is possible that the second thread first acquired the lock first written in, if you do not use this asynchronous call with blocking calls, direct beginre ...
We have written similar code ... The basic problems are dealt with rather than imaginary threading problems (as we have guessed) and then we used a stupid way to add a log to the problem in the process of processing. If you haven't solved the problem, suggest a alexccmm.
2011-07-04 02:09:49 probable cause, packages too fast, N-packs arrive at the same time
Scheme one: the packet and the packet pause between the next: Sleep time self-test ROM
Scheme II: After serialization of their own packet length, received packets to solve the packet length, if there is more than the next package, you can solve the next package, not the next data, the package solution package,
Solution one is relatively simple
djc7811
2011-07-04 15:19:11 Interactive Mode transceiver ALEXCCMM
2011-07-04 15:27:57 The contract break, see the serialization of the size,
After receiving the package, at the breakpoint, after the packet size,
See if size has the same,
Too big to be packed in a strife
2011-07-06 09:53:13 Look at bei0305
2011-12-04 23:50:03 brother Did you solve the problem? Chenandczh
2011-12-05 08:18:22 1.Socket. Accept
Judge Socket.connect
True is received, ending to 1
False loops to 1