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