CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: Code explaining

  1. #1
    Join Date
    Jan 2012
    Posts
    0

    Code explaining

    Hi to all you .. i found a code that has a client and a server.. and i can connect to server than read , or download or upload files... but i dont understand very good the all of the code..
    from the METOD " //Metoda per dergim te te dhenave te serveri " and down can someone explain me the code .. thankyou very much ... and this is the Client part-CODE..

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Net.Sockets;
    using System.IO;
    using System.Threading;

    namespace ProgSistFileClient
    {
    class Program
    {
    static void Main(string[] args)
    {
    if (!Directory.Exists(Program.m_clientPath))
    {
    Directory.CreateDirectory(Program.m_clientPath);
    }

    Program p = new Program();
    p.Connect();
    Thread th = new Thread(p.Listening);
    th.Start();
    p.Puno();
    }

    //show fajllat qe gjenden ne folder
    private const string Show = "show";
    //shkarko + pathi
    private const string Shkarko = "shkarko ";
    //ngarko + pathi
    private const string Ngarko = "ngarko ";

    //formati i komunikimit
    private const char Komand = 'c';
    private const char EmriFile = 'f';

    private const char Data = 'd';
    private const char Message = 'm';

    private TcpClient m_tcpClient;
    private NetworkStream m_networkStream;
    private static string m_clientPath = @"D:\ProgSistClientFolder";

    private string m_shkarkoFileName = "";
    private string m_ngarkoFileName = "";

    //Metoda per tu lidhur me server
    public bool Connect()
    {
    try
    {
    m_tcpClient = new TcpClient();
    m_tcpClient.Connect("127.0.0.1", 7980);
    m_networkStream = m_tcpClient.GetStream();
    }
    catch (Exception ex)
    {
    Console.WriteLine("not WORKING...");
    Environment.Exit(0);
    return false;

    }
    return true;
    }

    private void Puno()
    {
    string komanda = "";

    while (komanda != "quit")
    {
    Console.WriteLine("Shkruaj komanden");
    komanda = Console.ReadLine();

    byte[] bufferKomanda = Encoding.ASCII.GetBytes(komanda);

    //nese u shtyp show
    if (komanda == Show)
    {
    //dergo kerkese tek serveri me kete komand
    Send(Komand, bufferKomanda);
    }
    //nese fjala qe u shtyp eshte ma e madhe se 8 (shkarko )
    else if (komanda.Length > 8)
    {
    //nese 8 charachteret e para jane (shkarko )
    if (komanda.Substring(0, 8) == Shkarko)
    {
    m_shkarkoFileName = komanda.Substring(8);
    //dergo kerkese te serveri per me shkarku nje file
    Send(Komand, bufferKomanda);
    }

    if (komanda.Length > 7)
    {
    if (komanda.Substring(0, 7) == Ngarko)
    {
    m_ngarkoFileName = komanda.Substring(7);
    Send(Komand, bufferKomanda);
    if (NgarkoFile(m_ngarkoFileName) == false)
    Console.WriteLine("===== Fajlli nuk ekziston =====");

    else if (komanda == "exit")
    {
    Console.Clear();
    Environment.Exit(0);
    }
    }
    }
    }
    else if (komanda == "exit")
    {
    Console.Clear();
    Environment.Exit(0);
    }
    else
    {
    Console.WriteLine("===== Komande e gabuar =====");
    }
    if (komanda.Length > 5)
    {
    if (komanda.Substring(0, 5) == "krijo")
    {
    Send(Komand, bufferKomanda);
    }
    else if (komanda.Substring(0, 5) == "fshij")
    {
    Send(Komand, bufferKomanda);
    }
    }
    }
    }

    //Metoda per dergim te te dhenave te serveri
    public bool Send(char tipi, byte[] buffer)
    {
    try
    {
    if (m_tcpClient.Connected)
    {
    byte[] tipiBuffer = BitConverter.GetBytes(tipi);
    byte[] sendBuffer = new byte[buffer.Length + 2];

    tipiBuffer.CopyTo(sendBuffer, 0);
    buffer.CopyTo(sendBuffer, 2);

    m_networkStream.Write(sendBuffer, 0, sendBuffer.Length);
    }
    else
    {
    return false;
    }
    }
    catch (Exception ex)
    {
    return false;
    }

    return true;
    }

    //metoda qe e ndegjon sererin qka po kten
    private void Listening()
    {
    try
    {
    while (m_tcpClient.Connected)
    {
    byte[] buffer = new byte[1024];
    int readBytes = m_networkStream.Read(buffer, 0, buffer.Length - 1);

    //Memory Stream per te ruajtur te dhenat
    MemoryStream memoryStream = new MemoryStream();
    memoryStream.Write(buffer, 0, readBytes);

    char operations = BitConverter.ToChar(buffer, 0);

    switch (operations)
    {
    case Komand:
    string komanda = Encoding.ASCII.GetString(buffer, 2, readBytes - 2);
    Console.WriteLine(Shfaq(buffer, readBytes - 2));
    break;

    case Data:
    ShkarkoFile(buffer, readBytes);
    break;

    case Message:
    string dataCommand = Encoding.ASCII.GetString(buffer, 2, readBytes - 2);

    Console.WriteLine(ShfaqMesazhin(buffer, readBytes - 2));
    break;
    }

    if (memoryStream != null)
    memoryStream.Close();
    }
    }
    catch (Exception ex)
    {

    }
    }

    private string Shfaq(byte[] buffer, int length)
    {
    return Encoding.ASCII.GetString(buffer, 2, length);
    }

    private string ShfaqMesazhin(byte[] buffer, int length)
    {
    return Encoding.ASCII.GetString(buffer, 2, length);
    }

    //e ruajme file-in qe na kteet prej sererit nese ekziston
    private void ShkarkoFile(byte[] receivedDataBuffer, int readBytes)
    {
    FileStream fileStream = new FileStream(m_clientPath + @"\" + m_shkarkoFileName, FileMode.Create, FileAccess.Write);

    fileStream.Write(receivedDataBuffer, 2, readBytes - 2);
    fileStream.Close();
    }

    private bool NgarkoFile(string fileName)
    {
    bool check = false;

    if (File.Exists(m_clientPath + @"\" + fileName))
    {
    FileStream fileStream = new FileStream(m_clientPath + @"\" + fileName, FileMode.Open, FileAccess.Read);

    byte[] buffer = new byte[fileStream.Length];

    fileStream.Read(buffer, 0, buffer.Length);

    Send(Data, buffer);

    fileStream.Close();
    check = true;
    }

    return check;
    }
    }
    }

  2. #2
    Join Date
    Dec 2011
    Posts
    61

    Re: Code explaining

    you need to read some book on .net socket programming

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured