Hi
I am a beginner of programming. These days, I try to
develop a ftp program. But I find it is harder than I
have thought.
I want to post my code, then concentrate on resolving
the multithread problem and the communication problem
between the client and the server.
Can anybody help me or just join the discussion? 5 1472
"fjlpf" <an*******@discussions.microsoft.com> wrote in news:054a01c3ac35
$0****************@phx.gbl: Hi
I am a beginner of programming. These days, I try to develop a ftp program. But I find it is harder than I have thought. I want to post my code, then concentrate on resolving the multithread problem and the communication problem between the client and the server.
Can anybody help me or just join the discussion?
Well, start with some palpable question and we will be able to help you...
cheers,
Peter
--
------ooo---OOO---ooo------
Peter Koen - www.kema.at
MCAD CAI/RS CASE/RS IAT
------ooo---OOO---ooo------
Hi "fjlpf",
(Your parents really gave you that name? :) )
"fjlpf" <an*******@discussions.microsoft.com> schrieb im Newsbeitrag
news:05****************************@phx.gbl... I am a beginner of programming. These days, I try to develop a ftp program. But I find it is harder than I have thought. I want to post my code, then concentrate on resolving the multithread problem and the communication problem between the client and the server.
As Peter told you already: Ask a question and maybe we could answer it.
And: Please go to google and search a little. Or simply go to http://www.codeproject.com/csharp/
There you find a lot of stuff about ftp and c#.
Some small examples are:
FTP Component in C# for .NET: http://www.codeproject.com/csharp/ftp.asp
FTP client library for C#: http://www.codeproject.com/csharp/ftplibrary.asp
Application for uploading modified Files to a FTP Server http://www.codeproject.com/csharp/net_ftp_upload.asp
With kind regards,
Konrad
Here is my code of ftp server. And I just extract the
implement of the function of transfering files.
I know it is very awful, so I am here to asked for your
help.
namespace FtpSrvr
{
public class Form1 : System.Windows.Forms.Form
{
private bool srv = true;
private IPAddress ipAddr;
private IPEndPoint ipEP;
public Socket sock_accpt;//port 20
public Socket sock_daccpt;//port 21
private Thread thrd_accpt;//a thread to accept connection
private Thread thrd_Daccpt;
public int total =0;
..........
[STAThread]
static void Main()
{Application.Run(new Form1()); }
................
private void btStartSrvr_Click(object sender,
System.EventArgs e)
{
this.btStartSrvr.Enabled = false;
ipAddr = IPAddress.Parse("127.0.0.1");
IPEndPoint ipEP1 = new IPEndPoint(ipAddr,20);
sock_accpt = new Socket
(AddressFamily.InterNetwork,SocketType.Stream,Prot ocolType.
Tcp);
sock_accpt.Bind((EndPoint)ipEP1);
sock_accpt.Listen(5);
//
IPEndPoint ipEP2 = new IPEndPoint(ipAddr,21);
sock_daccpt = new Socket
(AddressFamily.InterNetwork,SocketType.Stream,Prot ocolType.
Tcp);
sock_daccpt.Bind((EndPoint)ipEP2);
sock_daccpt.Listen(5);
//create new thread to accept connection
thrd_Accpt = new Thread(new ThreadStart(this.Thrd_Accpt));
thrd_Accpt.Start();
}
private void Thrd_Accpt()
{
while(srv)//
{ Socket newClient = sock_accpt.Accept();
ThreadStartCommand c = new ThreadStartCommand
(newClient,this.sock_daccpt);
Thread thrd = new Thread(new ThreadStart(c.Thrd_Command));
thrd.Start();
}
}
internal class ThreadStartCommand
{
private Socket _sock;//store the socket created by accept()
private Form1 _form;
public ThreadStartCommand(Socket sock, Form1 form
{ _sock = sock;
_form = form;
}
//this function aims to retrevie command
public void Thrd_Command()
{
_sock.SetSocketOption
(SocketOptionLevel.Socket,SocketOptionName.SendTim eout,5000
);
byte[] b= System.Text.Encoding.ASCII.GetBytes("welcome to
my ftp server");
try
{
_sock.Send(b,0,b.Length,SocketFlags.None);
}
catch(SocketException ex)
{
System.Diagnostics.Debug.Write("error to send welcome
message" );
return;
}
_sock.SetSocketOption
(SocketOptionLevel.Socket,SocketOptionName.Receive Timeout,2
0000);
byte[] buffer = new byte[1024];
int rcv ;
while(true)
{
try
{
rcv = _sock.Receive(buffer,0,1024,SocketFlags.None);
}
catch(SocketException ex)
{
/*if the client doesn't send command within 20 seconds,the
socket will close.*/
_sock.Close();
return
}
}
string s = System.Text.Encoding.ASCII.GetString
(buffer,0,rcv);
s = s.ToLower();
int i = s.IndexOf(" ");
string comCode = s.Substring(0,i);
string para = s.Substring(i+1,rcv-1-i);
AnalyseCommand(comCode,para);
byte[] buffer = ASCII.GetBytes("120 new port allocated");
_sock.Send(buffer,0,buffer.Length,SocketFlags.None );
}
}
public void AnalyseCommand(string comCode, string para)
{
switch(comCode)
{
case "retr":
{
lock(this._form.sock_daccpt)//
{if(this._form.total<4)
this._form.total++;//restrict total connection of Port 21
else
{byte[] buf = System.Text.Encoding.ASCII.GetBytes("sorry,
overload");
this._sock.Send(buf,0,buf.Length,SocketFlags.None) ;
return;
}
}
Thread thrd = new Thread(new ThreadStart
(this.ThrdSndFile));
thrd.Start();// thread to transfer file to client
}
break;
}
case "":...
case "":...
}
}
public void ThrdSndFile(Socket accpt, string para)
{
Socket dataclient = this._form.sock_daccpt.Accept();
FileStream fs;
try
{
fs = new FileStream
(@para,FileMode.Open,FileAccess.Read,FileShare.Rea d);
}
catch(IOException ex)
{dataclient.Close();
return;
}
dataclient.SetSocketOption
(SocketOptionLevel.Socket,SocketOptionName.SendTim eout,5000
0);
int rd = 1;
while(rd!=0)
{
//Question:if the client socket collapse, what happen to
//socket of server, does it still push data to TCP's buffer
byte[] buffer = new byte[1024];
rd = fs.Read(buffer,0,1024);
try
{_sock.Send(buffer,0,rd,SocketFlags.None);
}
catch(SocketException ex)
{
lock(this._form._sock_daccpt)
{ this._form.total--;
}
_sock.Close();
return;
}
}
}
_sock.Close();
}
}
}
:)
konrad,
my parents' taste isn't awful like me.
with kind regards
Hi!
"fjlpf" <an*******@discussions.microsoft.com> schrieb im Newsbeitrag
news:05****************************@phx.gbl... :) konrad, my parents' taste isn't awful like me.
Then everything is ok for me. I hope, my links helped a little.
With kind regards,
Konrad This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: svenn.are |
last post by:
Hi,
has anybody thought of / already used graphviz to convert the output of
trace.py into a graph? I looked at PyUMLGraph, but 1. PyUMLGraph does
not successfully create a dot file, and 2. I...
|
by: jeff regoord |
last post by:
A user inputs a float value. The scanf() function gets the value.
However, I need to create an error handler with an if else statement
saying invalid input if the input is not a number. Does...
|
by: DOTNET |
last post by:
Hi,
Anybody help me regarding this error:
I am assigning the values to the session variables when the button is
clicked and passing these session variables to the next page and when I am...
|
by: Rahul |
last post by:
Hi Everybody
I have some problem in my script. please help me. This is script file.
I have one *.inq file. I want run this script in XML files. But this
script errors shows . If u want i am...
|
by: Vic.Dong |
last post by:
Hello:
Is there anybody how to create hidden folder in public folder or system?
I have a any problem.
I cannot create hidden folder in public or system folder.
Is there anybody who knows...
|
by: DurumDara |
last post by:
Hi !
I need to speedup my MD5/SHA1 calculator app that working on
filesystem's files.
I use the Python standard modules, but I think that it can be faster if
I use C, or other module for it.
...
|
by: Henry |
last post by:
Does anybody have a real-world sample of buiding a treeview control using
data from database tables? All the sample code I have found either builds
the treeview manually or uses a file directory...
|
by: Clemens Vasters |
last post by:
"Is anybody from Microsoft reading this?"
Well ... actually .... no .... welll ... maybe if you're really lucky. Let
me explain ...
My name is Clemens Vasters and I am a Program Manager on the...
|
by: raylopez99 |
last post by:
What good is C# Reflection, other than to find out what types are in
an assembly? And to dynamically invoke methods in an assembly (.dll
or .exe)?
Also, bonus question, can you use Reflection...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |