Results 1 to 2 of 2

Thread: Is this a threading issue? (vs 2003)

  1. #1

    Thread Starter
    Member
    Join Date
    May 2006
    Posts
    60

    Is this a threading issue? (vs 2003)

    I'm trying to write a server with sockets with a windows app.

    I'm converting a console app to a windows app.

    Everything is fine except one little bug.
    I output the status of the connection, transfer, closing of the connection to a text box.
    The output will show up if I have a MessageBox.Show("...") prompt in the form.
    When I remove it, the output doesn't show up in in the text box.
    The text is wrote to the textbox with the showResult() function which is called by the delegate named 'output'.

    I'm posting a lot of code (just in case you may need it - don't think you will thought) but I highlight where the messagebox is about halfway down with text in all caps and !!!!!!! marks.

    Here's the code...
    VB Code:
    1. using ....
    2. .....
    3.  
    4.     public delegate void GenDelegate();
    5.     public delegate void OutPutText(string txt);
    6.  
    7.     public class Form1 : System.Windows.Forms.Form
    8.     {
    9.         byte[] data = new byte[1024];
    10.         Socket client;
    11.  
    12.         .....
    13.         .....
    14.         .....
    15.  
    16.  
    17. // INITIATE THE SERVER WITH A TIMER AFTER FORM LOADS
    18.  
    19. // STARTED THE xTRANSER() FUNCTION WITH AND WITHOUT A DELEGATE
    20. // IT MAKES NO CHANGE EITHER WAY
    21.  
    22.         private void timerLoad_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
    23.         {
    24.             this.timerLoad.Enabled=false;
    25.             this.xTransfer();
    26.             // Instantiation the Delegate
    27.             //GenDelegate start = new GenDelegate(xTransfer);
    28.             // Invocation
    29.             //start();
    30.         }
    31.  
    32.                 // Using a delegate to output the text to the textbox hoping to avoid cross-threading issues
    33.         private void showResult(string txt)
    34.         {
    35.             this.rText1.Text = this.rText1.Text + Environment.NewLine + txt;
    36.         }
    37.  
    38.                 // Connect to client
    39.         void AcceptConn(IAsyncResult iar)
    40.         {
    41.             Socket oldserver = (Socket)iar.AsyncState;
    42.             client = oldserver.EndAccept(iar);
    43.             this.rText1.Text = "Connected to: " + client.RemoteEndPoint.ToString();
    44.             string stringData = "Welcome to my server";
    45.             byte[] message1 = Encoding.ASCII.GetBytes(stringData);
    46.             // client.BeginSend(message1, 0, message1.Length, SocketFlags.None, new AsyncCallback(SendData), client);
    47.         }  
    48.  
    49.  
    50. // This function does the communicating
    51.  
    52. // THE BUG IS IN HERE
    53.  
    54. private void xTransfer()
    55.         {          
    56.            
    57.             // Output using a Delegate for cross thread calls
    58.             OutPutText output= new OutPutText(showResult);
    59.  
    60.             try
    61.             {
    62.                 long fileSize = 0;                         
    63.                 string ClientMessage;
    64.                 IPEndPoint ipep = new IPEndPoint(IPAddress.Any, 9050);
    65.  
    66.                 // Initiate Server
    67.                 Socket newsock = new Socket(AddressFamily.InterNetwork,     SocketType.Stream, ProtocolType.Tcp);
    68.                 newsock.Bind(ipep);
    69.                 newsock.Listen(1);
    70.                 output("Waiting...");          
    71.  
    72.                 // Inititate Client
    73.                 newsock.BeginAccept(new AsyncCallback(AcceptConn), newsock);       
    74.  
    75.                 // Wait for client connection
    76.                 do
    77.                 {
    78.                     Application.DoEvents();
    79.                 } while (client == null);
    80.                
    81.                 IPEndPoint clientep =   (IPEndPoint)client.RemoteEndPoint;             
    82.                 output("Connected with " + clientep.Address + " on port "+  clientep.Port);
    83.  
    84.                 // Send Welcome
    85.                 data = Encoding.ASCII.GetBytes("Welcome");
    86.                 client.Send(data, data.Length,  SocketFlags.None);             
    87.                 output("Welcome Sent.");       
    88.  
    89. // !!!!!!!!  HERE -- IF I DON'T SHOW THIS MESSAGEBOX, I DON'T GET TEXT IN THE TEXTBOX
    90. // !!!!!!!!  WHEN I SHOW THIS TEXTBOX, I SEE THE TEXT.
    91. // ALSO, THE FILE TRANSFER WORKS WETHER I SHOW THE MSBBOX OR NOT   
    92.                 //MessageBox.Show("hello...");
    93.  
    94.                 Debug.WriteLine("hello....");
    95.                
    96.                 // Get Ack
    97.                 data = new byte[1024];
    98.                 client.Receive(data);
    99.                 ClientMessage = Encoding.ASCII.GetString(data);            
    100.                 output(ClientMessage);
    101.                
    102.                 // Request File Size
    103.                 data = Encoding.ASCII.GetBytes("Size");
    104.                 client.Send(data, data.Length,  SocketFlags.None);
    105.                 output("Size Requested.");
    106.                
    107.                 // Recieve File Size
    108.                 data = new byte[1024];         
    109.                 client.Receive(data); // 0 ends the transmission
    110.                 ClientMessage = Encoding.ASCII.GetString(data);  //, 0, recv).ToString()); 
    111.                 fileSize = Convert.ToInt64(ClientMessage);
    112.                 output("File Size: " + ClientMessage);
    113.  
    114.                 // Request Data Now
    115.                 data = new byte[1024];
    116.                 data = Encoding.ASCII.GetBytes("send file");
    117.                 client.Send(data, data.Length, SocketFlags.None);
    118.                 output("Requested Data...");
    119.  
    120.                 // Recieve the Data
    121.                 data = new byte[fileSize];         
    122.                 client.Receive(data); // 0 ends the transmission
    123.                 output("Received " + data.Length.ToString() + " bytes for the file.");
    124.                
    125.                 // Write Data to File          
    126.                 FileStream fs = new FileStream("C:\\newFile.jpg", FileMode.Create, FileAccess.ReadWrite); // , FileShare.None, 4096, true);
    127.                 BinaryWriter bw = new BinaryWriter(fs);
    128.                 bw.Write(data, 0, data.Length);
    129.                 bw.Close();
    130.                 fs.Close();
    131.                 output("Data Saved.......");
    132.                
    133.                 // Close
    134.                 output("Disconnected from " + clientep.Address);
    135.                 client.Close();
    136.                 newsock.Close();
    137.        
    138.             }
    139.             catch(SocketException ex)
    140.             {
    141.                 MessageBox.Show(ex.Message);
    142.             }
    143.             catch(Exception ex)
    144.             {
    145.                 MessageBox.Show(ex.Message + "\n\r");
    146.             } // End Try/Catch
    147.            
    148.            
    149.         } // End xTransfer
    150.  
    151.     } // End Class
    152. } // End Namespace

    Thanks for the time gurus.

  2. #2

    Thread Starter
    Member
    Join Date
    May 2006
    Posts
    60

    Re: Is this a threading issue? (vs 2003)

    bump ^^

    Anyone see why this is happening?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width