7
Answers

Explainin clinet code

jimy arsenal

jimy arsenal

12y
1.3k
1
hi everyone can someone explain me the code under ...
it's a Client - Server code .. so im not undestading when i comunicate with the server .. can you explain this code what is does and other stuff...
}
}

//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;
}
}
}

Answers (7)