multi thread question
-
I have written a socket server that accepts a connection over a single port. It now reads data into a string and then creates a hipaa compliant file out of the data. That data is then sent to the mainframe and a response file is created. I then reverse the process and write the data back over the port to the sender. Right now the process is single threaded. Where I get the data, create file, wait for response file, send data. I can do all this in about 3-4 seconds. I have a few questions that I hope someone can answer... I need to address multiple request that are sent. How do I thread this process so that multiple transactions (data files) can be dealt with. I am concerned about the sender sending me a data feed that contains multiple data files like this |data1|data2|data3|. How can I know where one piece of data starts and stops. Can I use a delimitor, the data file always ends in a ~ ? Can I send and receive over 1 socket at the same time? Should I leave the connection open all the time, or close it with each transmission? The client will be connected over a frame relay. Any resources? I have found several examples where the connections are threaded, but I will only have 1 connection. I really need to get my brain around the model for this process and can not seem to do it... rod
-
I have written a socket server that accepts a connection over a single port. It now reads data into a string and then creates a hipaa compliant file out of the data. That data is then sent to the mainframe and a response file is created. I then reverse the process and write the data back over the port to the sender. Right now the process is single threaded. Where I get the data, create file, wait for response file, send data. I can do all this in about 3-4 seconds. I have a few questions that I hope someone can answer... I need to address multiple request that are sent. How do I thread this process so that multiple transactions (data files) can be dealt with. I am concerned about the sender sending me a data feed that contains multiple data files like this |data1|data2|data3|. How can I know where one piece of data starts and stops. Can I use a delimitor, the data file always ends in a ~ ? Can I send and receive over 1 socket at the same time? Should I leave the connection open all the time, or close it with each transmission? The client will be connected over a frame relay. Any resources? I have found several examples where the connections are threaded, but I will only have 1 connection. I really need to get my brain around the model for this process and can not seem to do it... rod
I'm not sure I follow what you mean by threading. First, you mention a case where you want clients processed in multiple threads, but then you mention a single client processing multiple chunks of data. Could you elaborate a little on which you mean? As far as handling multiple connections, there are a slew of articles here on CP about that so, unless necessary, I won't bog-down the forums with code. If you want to handle multiple data chunks in a single transaction, think about how several protocols like HTTP deal with it: content-length. You should specify a protocol where the content-length is a fixed size, like a
long
. That gives the number of raw bytes to read. Read that many bytes. If data exists after that, assume it was another content-length. Keep doing the same thing. Of course, you'll want to check the input data to make sure the protocol was use correctly. This is pretty common in a lot of different things. You can send and receive on the same socket in a connection protocol. There's some other stuff that has to be done with connectionless protocols since they send datagrams with no return. As far as leaving the connection open, it really depends on the persistence of communication. If the transactions happen in intervals, you could get away with leaving the connection open. If they transactions are random or you're unsure if another transaction will occur, close the connection.-----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----
-
I have written a socket server that accepts a connection over a single port. It now reads data into a string and then creates a hipaa compliant file out of the data. That data is then sent to the mainframe and a response file is created. I then reverse the process and write the data back over the port to the sender. Right now the process is single threaded. Where I get the data, create file, wait for response file, send data. I can do all this in about 3-4 seconds. I have a few questions that I hope someone can answer... I need to address multiple request that are sent. How do I thread this process so that multiple transactions (data files) can be dealt with. I am concerned about the sender sending me a data feed that contains multiple data files like this |data1|data2|data3|. How can I know where one piece of data starts and stops. Can I use a delimitor, the data file always ends in a ~ ? Can I send and receive over 1 socket at the same time? Should I leave the connection open all the time, or close it with each transmission? The client will be connected over a frame relay. Any resources? I have found several examples where the connections are threaded, but I will only have 1 connection. I really need to get my brain around the model for this process and can not seem to do it... rod
I'll add another vote for the general solution that Heath already sketched out. In addition, here[^] is some sample code to do exactly this which I posted last week in answer to someone else's question. To answer your other questions: Yes, you can read and write on the same socket at the same time. Leave the connection open, a single connection doesn't consume enough resources to care about. You should, however, be prepared to handle connection failures and go back into listening state to wait for the client to reconnect, incase of transient network failure. No, you don't need to use multiple threads given that you have a single client. (There is an advanced case where it might be useful to use multiple threads with one client, if you want to send your requests to the mainframe asynchronously, i.e. submit a second request before the result of the first one has been recieved. If your transactional performance is already adequate, then don't complicate life by doing that.) One last pointer, checksums are your friend:
System.Convert.ToBase64String( System.Security.Cryptography.SHA1.Create().ComputeHash( System.Text.Encoding.Unicode.GetBytes(yourData)))
-Blake