Note that there are some explanatory texts on larger screens.

plurals
  1. POC# socket receiving thread
    text
    copied!<p>I have a socket connection, and I am sending data through this socket. The server I am connected to answers every correct send of my data. I've got the message working so I get an answer for every message I receive. Sometimes the server likes to hold onto a message for a few seconds, or send in different order. My solution was to spawn a thread and have it spin around a receive function. However using the Sockets example on MSDN I am having fits. They use a simple do/while looping structure. When I do this I get jumbled replies, and/or incomplete data. This is for a homework assignment so I have to write the client by hand rather than just use an easier solution. Is there something wrong with this code maybe? I've been staring at it so long I think I'm missing something simple:</p> <pre><code>private static void ReceiveThread(Socket sock, ReceiverClass rc) { // Create a socket and pass in parameter converted from object socket int receivedBytes = 0; do { // receive data from socket receivedBytes = sock.Receive(rc.buffer); byte[] formattedMsg = new byte[receivedBytes]; Array.Copy(rc.buffer, formattedMsg, receivedBytes); rc.sb.Append("&lt;LF&gt;&lt;CR&gt;" + System.Text.Encoding.ASCII.GetString(formattedMsg) + "\r\n"); } while (receivedBytes &gt; 0); } </code></pre> <p>EDIT, adding function that spawns receiving thread. (It is WAY too long but I plan on making it pretty when I get the stupid thing working):</p> <pre><code> public void SendData(Socket sock) { // Set socket timeout sock.ReceiveTimeout = 4000; // Prepare file for IO operations string path = @"c:\Logs\Lab2.Scenario3.WurdingerO.txt"; StreamWriter logWrite = File.AppendText(path); // Get local ip address: IPAddress ip = Dns.GetHostAddresses(Dns.GetHostName()).Where(address =&gt; address.AddressFamily == AddressFamily.InterNetwork).First(); string portNum = ((IPEndPoint)sock.LocalEndPoint).Port.ToString(); // response time for scenario 2 and 3 int responseTime = 0; // Set up Stopwatch to keep track of time Stopwatch stpWatch = new Stopwatch(); stpWatch.Start(); // setup for logging class ReceiverClass rc = new ReceiverClass(); // setup receiving thread Thread receiveThread = new Thread(delegate() { ReceiveThread(sock, rc); }); receiveThread.Start(); // Counter to call client operations for (int i = 0; i &lt; MESSAGE_COUNT; i++) { string msTime = Convert.ToString(stpWatch.ElapsedMilliseconds); if (msTime.Length &gt; 10) { string newMSTime = ""; for (int t = 9; t &gt;= 0; t++) { newMSTime += msTime[t]; } msTime = newMSTime; } Classes.RequestBuilder reqB = new Classes.RequestBuilder(); byte[] sendMsg; switch (scenarioNo) { case 1: sendMsg = reqB.MessageBuildScenarioOne(sock, msTime, ip.ToString(), portNum, serverPort, serverIP, i); break; case 2: // set up response time delay switch (i) { case 1: responseTime = 1000; break; case 3: responseTime = 3000; break; default: responseTime = 0; break; } sendMsg = reqB.MessageBuildScenarioTwo(sock, msTime, ip.ToString(), portNum, serverPort, serverIP, i, responseTime); break; case 3: // set up response time delay switch (i) { case 1: responseTime = 1000; break; case 3: responseTime = 3000; break; default: responseTime = 0; break; } sendMsg = reqB.MessageBuildScenarioThree(sock, msTime, ip.ToString(), portNum, serverPort, serverIP, i, responseTime); break; default: sendMsg = reqB.MessageBuildScenarioOne(sock, msTime, ip.ToString(), portNum, serverPort, serverIP, i); break; } try { sock.Send(sendMsg); } catch (Exception ex) { System.Windows.Forms.MessageBox.Show(ex.Message); } } // Socket shutdown sock.Shutdown(SocketShutdown.Send); receiveThread.Join(); sock.Shutdown(SocketShutdown.Receive); string date = System.DateTime.Now.ToString("MMddyyyy"); string time = System.DateTime.Now.ToString("HHmmss"); logWrite.Write(rc.sb.ToString()); logWrite.Write(date + "|" + time + "|0|0|"); // Close log file logWrite.Close(); System.Windows.Forms.MessageBox.Show("Finished"); } </code></pre> <p>EDIT: I put a sleep timer in after the send operation and that fixed the issues I was running into. Thank you!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload