CLIENT / SERVER : Simple File Sharing and File Transfer
using System.Net;
using System.Net.Sockets;
serversocket = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
serversocket.Blocking = true ;
IPHostEntry IPHost = Dns.Resolve(server);
string []aliases = IPHost.Aliases;
IPAddress[] addr = IPHost.AddressList;
IP_ADDRESS.Text = addr[0].ToString();
IPEndPoint ipepServer = new IPEndPoint(addr[0], 8090);
serversocket.Bind(ipepServer);
serversocket.Listen(-1);
while ( true )
{
clientsock = serversocket.Accept();
if ( clientsock.Connected )
{
total_clients_connected++;
AppendText("Client connected...") ;
Thread tc = new Thread(new ThreadStart(listenclient));
tc.Start();
}
}
void listenclient()
{
Socket sock = clientsock ;
string cmd = server ;
byte[] sender = System.Text.Encoding.ASCII.GetBytes("SERVER " + cmd) ;
sock.Send(sender, sender.Length,0) ;
try
{
while ( sock != null )
{
cmd = "" ;
byte[] recs = new byte[32767];
int rcount = sock.Receive(recs,recs.Length,0) ;
string clientmessage = System.Text.Encoding.ASCII.GetString(recs) ;
clientmessage = clientmessage.Substring(0,rcount);
string smk = clientmessage ;
cmdList = null ;
cmdList = clientmessage.Split(' ');
string execmd = cmdList[0];
AppendText("COMMAND==>" + execmd) ;
sender = null ;
sender = new Byte[32767];
Console.WriteLine("CLIENT COMMAND = " + execmd + "\r\n");
string parm1 = "";
if ( execmd == "USER" )
{
login_client_machine = cmdList[2];
string pass = cmdList[3];
string ip = cmdList[1];
AppendText("Client Connected IP:" + ip + " User :" + login_client_machine );
continue ;
}
if ( execmd == "GET" )
{
// GET
for ( int i=1 ; i < cmdList.Length-1;i++)
parm1 = parm1 + " " + cmdList[i];
parm1 = parm1.Trim();
FileInfo fi = new FileInfo(parm1);
if ( fi.Exists )
cmd = "GETOK " ;
else
cmd = "GETOK_FAILED " ;
sender = System.Text.Encoding.ASCII.GetBytes(cmd) ;
sock.Send(sender, sender.Length,0) ;
continue;
}
if ( execmd == "LISTING" )
{
PopulateList(clientmessage);
continue ;
}
if ( execmd == "SERVER_LISTING" )
{
sender = new Byte[32767];
cmd="LISTING \r\n";
parm1 = cmdList[1];
cmd = cmd + SearchDatabase(parm1);
sender = System.Text.Encoding.ASCII.GetBytes(cmd) ;
sock.Send(sender, sender.Length,0) ;
continue ;
}
if ( execmd == "NOOP" )
{
// do nothing
continue ;
}
if ( execmd == "CLIENT_DISCONNECTING" )
{
cmd = "CLIENT_CLOSE";
sender = System.Text.Encoding.ASCII.GetBytes(cmd) ;
sock.Send(sender, sender.Length,0) ;
sock.Close();
AppendText("Client Disconnected");
continue ;
}
if ( execmd == "FILEOK" )
{
cmd = "NOOP ";
sender = System.Text.Encoding.ASCII.GetBytes(cmd) ;
sock.Send(sender, sender.Length,0) ;
continue ;
}
if ( execmd == "GETOK" )
{
cmd = "BEGINSEND " + shared_file_path + " " + shared_file_size ;
sender = new Byte[1024];
sender = Encoding.ASCII.GetBytes(cmd);
sock.Send(sender, sender.Length , 0 );
ServerDownloadingFromClient(sock);
continue ;
}
if ( execmd == "BEGINSEND" )
{
ClientDownloadingFromServer(cmdList , sock);
continue ;
}
}
}
catch(Exception Se)
{
string s = Se.Message;
Console.WriteLine(s);
}
}
Once the server creates the stream, it opens the selected file, begins reading the file and outputs the data to the stream.
while(rdby < total && nfs.CanWrite)
{
//Read from the File (len contains the number of bytes read)
len =fin.Read(buffed,0,buffed.Length) ;
//Write the Bytes on the Socket
nfs.Write(buffed, 0,len);
//Increase the bytes Read counter
rdby=rdby+len ;
int pc = (int)( ((double)rdby/(double)total ) * 100.00) ;
string perc = pc.ToString() + "%" ;
listView4.Items[cnt].SubItems[3].Text = perc;
listView4.Items[cnt].SubItems[2].Text = rdby.ToString();
}
NetworkStream nfs = new NetworkStream(sock) ;
try
{
//loop till the Full bytes have been read
while(rby < size)
{
byte[] buffer = new byte[1024] ;
//Read from the Network Stream
int i = nfs.Read(buffer,0,buffer.Length) ;
fout.Write(buffer,0,(int)i) ;
rby=rby+i ;
int pc = (int)( ((double)rby/(double)size ) * 100.00) ;
string perc = pc.ToString() + "%";
listView3.Items[cnt].SubItems[3].Text = perc;
listView3.Items[cnt].SubItems[2].Text = rby.ToString();
}
fout.Close() ;
string cmd = "FILEOK" ;
Byte[] sender = new Byte[1024];
sender = new Byte[1024];
sender = Encoding.ASCII.GetBytes(cmd);
sock.Send(sender , sender.Length , 0 );
}
try
{
serversocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
serversocket.Blocking = true ;
IPHostEntry IPHost = Dns.Resolve(textBox1.Text);
string []aliases = IPHost.Aliases;
IPAddress[] addr = IPHost.AddressList;
IPEndPoint ipepServer = new IPEndPoint(addr[0], 8090);
serversocket.Connect(ipepServer);
clientsock = serversocket;
}
See full details: http://www.csharphelp.com/archives2/archive335.html
Comments