PDA

Click to See Complete Forum and Search --> : Is this a threading issue? (vs 2003)


sooner
Nov 10th, 2006, 11:02 AM
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. :thumb:

Here's the code...

using ....
.....

public delegate void GenDelegate();
public delegate void OutPutText(string txt);

public class Form1 : System.Windows.Forms.Form
{
byte[] data = new byte[1024];
Socket client;

.....
.....
.....


// INITIATE THE SERVER WITH A TIMER AFTER FORM LOADS

// STARTED THE xTRANSER() FUNCTION WITH AND WITHOUT A DELEGATE
// IT MAKES NO CHANGE EITHER WAY

private void timerLoad_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
this.timerLoad.Enabled=false;
this.xTransfer();
// Instantiation the Delegate
//GenDelegate start = new GenDelegate(xTransfer);
// Invocation
//start();
}

// Using a delegate to output the text to the textbox hoping to avoid cross-threading issues
private void showResult(string txt)
{
this.rText1.Text = this.rText1.Text + Environment.NewLine + txt;
}

// Connect to client
void AcceptConn(IAsyncResult iar)
{
Socket oldserver = (Socket)iar.AsyncState;
client = oldserver.EndAccept(iar);
this.rText1.Text = "Connected to: " + client.RemoteEndPoint.ToString();
string stringData = "Welcome to my server";
byte[] message1 = Encoding.ASCII.GetBytes(stringData);
// client.BeginSend(message1, 0, message1.Length, SocketFlags.None, new AsyncCallback(SendData), client);
}


// This function does the communicating

// THE BUG IS IN HERE

private void xTransfer()
{

// Output using a Delegate for cross thread calls
OutPutText output= new OutPutText(showResult);

try
{
long fileSize = 0;
string ClientMessage;
IPEndPoint ipep = new IPEndPoint(IPAddress.Any, 9050);

// Initiate Server
Socket newsock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
newsock.Bind(ipep);
newsock.Listen(1);
output("Waiting...");

// Inititate Client
newsock.BeginAccept(new AsyncCallback(AcceptConn), newsock);

// Wait for client connection
do
{
Application.DoEvents();
} while (client == null);

IPEndPoint clientep = (IPEndPoint)client.RemoteEndPoint;
output("Connected with " + clientep.Address + " on port "+ clientep.Port);

// Send Welcome
data = Encoding.ASCII.GetBytes("Welcome");
client.Send(data, data.Length, SocketFlags.None);
output("Welcome Sent.");

// !!!!!!!! HERE -- IF I DON'T SHOW THIS MESSAGEBOX, I DON'T GET TEXT IN THE TEXTBOX
// !!!!!!!! WHEN I SHOW THIS TEXTBOX, I SEE THE TEXT.
// ALSO, THE FILE TRANSFER WORKS WETHER I SHOW THE MSBBOX OR NOT
//MessageBox.Show("hello...");

Debug.WriteLine("hello....");

// Get Ack
data = new byte[1024];
client.Receive(data);
ClientMessage = Encoding.ASCII.GetString(data);
output(ClientMessage);

// Request File Size
data = Encoding.ASCII.GetBytes("Size");
client.Send(data, data.Length, SocketFlags.None);
output("Size Requested.");

// Recieve File Size
data = new byte[1024];
client.Receive(data); // 0 ends the transmission
ClientMessage = Encoding.ASCII.GetString(data); //, 0, recv).ToString());
fileSize = Convert.ToInt64(ClientMessage);
output("File Size: " + ClientMessage);

// Request Data Now
data = new byte[1024];
data = Encoding.ASCII.GetBytes("send file");
client.Send(data, data.Length, SocketFlags.None);
output("Requested Data...");

// Recieve the Data
data = new byte[fileSize];
client.Receive(data); // 0 ends the transmission
output("Received " + data.Length.ToString() + " bytes for the file.");

// Write Data to File
FileStream fs = new FileStream("C:\\newFile.jpg", FileMode.Create, FileAccess.ReadWrite); // , FileShare.None, 4096, true);
BinaryWriter bw = new BinaryWriter(fs);
bw.Write(data, 0, data.Length);
bw.Close();
fs.Close();
output("Data Saved.......");

// Close
output("Disconnected from " + clientep.Address);
client.Close();
newsock.Close();

}
catch(SocketException ex)
{
MessageBox.Show(ex.Message);
}
catch(Exception ex)
{
MessageBox.Show(ex.Message + "\n\r");
} // End Try/Catch


} // End xTransfer

} // End Class
} // End Namespace


Thanks for the time gurus. :) :wave:

sooner
Nov 14th, 2006, 09:00 AM
bump ^^

Anyone see why this is happening?