2
Answers

Processing the received string messages from the server

I have this code in the server side that processes normal
string messages sent by the
client. If the client opens an application, the application
name will be sent to the server
along with its file path and client's hostname. My problem
is the server receives only
two looped messages, the rest is being attached to the
second message. This is the
code in the client which sends the message
Code:
public void sendthedata()
{
if (!_timer.Enabled) // If timer is not running send data
and start refresh interval
{ SendData(); _timer.Enabled = true; }
private List<int> listedProcesses = new List<int>();
else // Stop timer to prevent further refreshing { _timer.Enabled = false; } }
String processPath = "";
private void SendData() { String processID = ""; String processName = ""; String processFileName = "";
for (int i = 0; i < piis.Count; i++)
String processMachinename = ""; listBox1.BeginUpdate(); try { piis = GetAllProcessInfos(); { try {
processID = piis[i].Id.ToString();
if (!listedProcesses.Contains(piis[i].Id)) //placed this on a list to avoid redundancy { listedProcesses.Add(piis[i].Id); processName = piis[i].Name.ToString();
output.Text += "\n\nSENT DATA : \n\t" + processFileName +
"\n\t" + processMachinename
+ "\n\t" + processID + "\n\t" + processName + "\n\t" + processPath + "\n";
processPath = piis[i].Path.ToString(); processFileName = piis[i].FileName.ToString(); processMachinename = piis[i].Machinename.ToString(); } } catch (Exception ex) { wait.Abort();
data = "--++" + processFileName + " " + processMachinename +
" " + processID + " "
+ processPath;
output.Text += "Error..... " + ex.StackTrace; } NetworkStream ns = tcpclnt.GetStream(); String data = ""; if (ns.CanWrite) { byte[] bf = new ASCIIEncoding().GetBytes(data); ns.Write(bf, 0, bf.Length); ns.Flush(); } } }
}
finally { listBox1.EndUpdate();
}
And this is where the server processes the received messages.
It passes through the
"normal message" part where it has (--++) unicode.

Code:
private void recieveData()
{
NetworkStream nStream = tcpClient.GetStream();
ASCIIEncoding ascii = null;
byte[] buffer = new byte[1024];
while (!stopRecieving) { if (nStream.CanRead) {
ascii = new ASCIIEncoding();
nStream.Read(buffer, 0, buffer.Length); recvDt = ascii.GetString(buffer);
f = recvDt.Contains("+@@+");
/*Received message checks if it has +@@+ then the ip is disconnected*/ bool f = false; if (f) { string d = "+@@+";
// new Transmit_File().transfer_file(file, ipselected);
recvDt = recvDt.TrimStart(d.ToCharArray()); clientDis(); stopRecieving = true; } //else if (recvDt.Contains("^^")) //{ //}
this.Invoke(new rcvData(addToOutput));
/* ++-- shutsdown/restrt/logoff/abort*/ else if (recvDt.Contains("++--")) { string d = "++--"; recvDt = recvDt.TrimStart(d.ToCharArray()); clientDis(); }
Thread.Sleep(1000);
/*--++ Normal msg*/ else if (recvDt.Contains("--++")) { string d = "--++"; recvDt = recvDt.TrimStart(d.ToCharArray()); this.Invoke(new rcvData(addToOutput)); } } } }
}
public void addToOutput() { if (recvDt != null && recvDt != "") { output.Text += "\n Received Data : " + recvDt; recvDt = null;
}
This is an example of a received message. The second
"Received Data" gets the rest of
the message. I have five opened application in this example.
The messages has been
assigned a unique character (--++) that will be trimmed when
it is being receive in the
server. As you can see here the third, fourth and so on
messages still has that
characters. What seems to be the problem here?



Listening server started @ 192.168.1.101 Listening on 800 port
Waiting for connection . . .

New IP client found :192.168.1.101

 
Received Data : Server_PC UNICORNV-FA15C8 4200
C:\Users\Admin\Desktop
\All Client Server and LogIn\LogIn\LogIn2\bin\
Debug\LogIn2.vshost.exe

 
Received Data : Untitled - Notepad UNICORNV-FA15C8
1652 C:\Windows
\System32\notepad.exe--++Client_PC (Running) -
Microsoft Visual C# 2010
Express UNICORNV-FA15C8 3988 C:\Program Files\
Microsoft Visual Studio 10.0
\Common7\IDE\VCSExpress.exe--++LogIn (Running)
- Microsoft Visual C# 2010
Express UNICORNV-FA15C8 3756 C:\Program Files\
Microsoft Visual Studio 10.0
\Common7\IDE\VCSExpress.exe--++
NetworkStream.Read Metho
d (Byte[],?Int32,?Int32)?(System.Net.Sockets) - Google
Chrome UNICORNV-FA15C8
2168 C:\Program Files\Google\Chrome\Application\chrome.exe
Answers (2)