أرجو توضيح ما الخطاء في هذا الكود المكتوب بالأحمر فقط
حيث أني إحترت ولم أعرف السبب الحقيقي للخطأ في هذا الكود
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Threading;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.IO;
namespace TCPIP_Server_Simple_C_Sharpe
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private ArrayList alSockets;
private System.Windows.Forms.Label lblStatus;
private System.Windows.Forms.ListBox lbConnections;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.lblStatus = new System.Windows.Forms.Label();
this.lbConnections = new System.Windows.Forms.ListBox();
this.SuspendLayout();
//
// lblStatus
//
this.lblStatus.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(178)));
this.lblStatus.Location = new System.Drawing.Point(8, 24);
this.lblStatus.Name = "lblStatus";
this.lblStatus.Size = new System.Drawing.Size(104, 24);
this.lblStatus.TabIndex = 0;
this.lblStatus.Text = "My IP adress is:- ";
//
// lbConnections
//
this.lbConnections.Location = new System.Drawing.Point(0, 56);
this.lbConnections.Name = "lbConnections";
this.lbConnections.Size = new System.Drawing.Size(288, 199);
this.lbConnections.TabIndex = 1;
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.Add(this.lbConnections);
this.Controls.Add(this.lblStatus);
this.Name = "Form1";
this.Text = "TCP Server C#";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void Form1_Load(object sender, System.EventArgs e)
{
IPHostEntry IPHost = Dns.GetHostByName(Dns.GetHostName());
lblStatus.Text = "My IP address is " +
IPHost.AddressList[0].ToString();
alSockets = new ArrayList();
Thread thdListener = new Thread(new
ThreadStart(listenerThread));
thdListener.Start();
}
}
}
public void listenerThread()
{
TcpListener tcpListener = new TcpListener(8080);
tcpListener.Start();
while(true)
{
Socket handlerSocket = tcpListener.AcceptSocket();
if (handlerSocket.Connected)
{
lbConnections.Items.Add(handlerSocket.RemoteEndPoint.ToString() + " connected.");
lock (this)
{
alSockets.Add(handlerSocket);
}
ThreadStart thdstHandler = newThreadStart(handlerThread);
Thread thdHandler = new Thread(thdstHandler);
thdHandler.Start();
}
}
}
new TcpListener(IPAddress.Any,8080)
public void handlerThread()
{
Socket handlerSocket = (Socket)alSockets[alSockets.Count-1];
NetworkStream networkStream = new
NetworkStream(handlerSocket);
int thisRead=0;
int blockSize=1024;
Byte[] dataByte = new Byte[blockSize];
lock(this)
{
// Only one process can access
// the same file at any given time
Stream fileStream = File.OpenWrite("c:\\my documents\\upload.txt");
while(true)
{
thisRead=networkStream.Read(dataByte,0,blockSize);
fileStream.Write(dataByte,0,thisRead);
if (thisRead==0) break;
}
fileStream.Close();
}
lbConnections.Items.Add("File Written");
handlerSocket = null;
}
}
}
}
}
}