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

VulpesPosted Jan 5, 2012, 2:50 PM
* The memory stream is then closed.
VulpesPosted Jan 5, 2012, 6:57 PM
jimy arsenalPosted Jan 5, 2012, 5:39 PM
why buffer.length - 1 ? it's because it's using Array ?..thankyou again
jimy arsenalPosted Jan 5, 2012, 12:40 PM
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;
little explaining ..it will be very helpful
Jignesh TrivediPosted Jan 5, 2012, 8:04 AM
for console and window base application all are client code and run on client side.
so as per me this all are client code.
hope this help.
jimy arsenalPosted Jan 5, 2012, 7:15 AM
Jignesh TrivediPosted Jan 5, 2012, 12:07 AM
Are you use web application?
if yes then its a server side code.
in web application only java script code run on client, other code run on server.
hope this help.