I am able to successfully connect the client and server together using the Sockets/tcpclient/tcplistener and able to send messages between each other successfully.
the problem is this:
when the client sends "[-]RF[-]" to the server, the server checks the incoming data, if it is "[-]RF[-]" then it sends back to the client the file name, for each file in the current working directory of the server.
after each filename, it sends that name back to the client, the client should recieve it... and it seems to do this.
however, it seems as if the client is recieving this in 1 big long string. This is not what I have told the server to do:
SERVER:
if (command.Equals("[-]RF[-]")){ string flagListSend = "";flagListSend = "[-]Start[-]"; byte[] msg = System.Text.Encoding.ASCII.GetBytes(flagListSend); this.SendMessageToClient(msg); foreach(string s in this.lstAudioFiles.Items) { msg = System.Text.Encoding.ASCII.GetBytes(s); this.SendMessageToClient(msg); } flagListSend = "[-]Finish[-]"; msg = System.Text.Encoding.ASCII.GetBytes(flagListSend); this.SendMessageToClient(msg); } |
the method "SendMessageToClient()" is just a method that uses the client Socket to send the message using the Socket.Send();
When the client recieves any kind of message it calls this method and processes the data the way I would like to. The client has this:
private void HandleIncomingData(byte[] incomingData){ string dataFromServer = System.Text.Encoding.ASCII.GetString(incomingData, 0, incomingData.Length);string properString = this.GetProperString(dataFromServer, "\0"); txtInfo.Text = txtInfo.Text + "Server: " + properString + " "; if(properString.Equals("[-]Start[-]")) { string innerMsg = properString; while (innerMsg.Equals("[-]Finish[-]") == false) { byte[] innerIncomingData = new byte[64]; int theData = ns.Read(innerIncomingData, 0, innerIncomingData.Length); innerMsg = this.GetProperString(System.Text.Encoding.ASCII.GetString(incomingData, 0, incomingData.Length), "\0"); if (innerMsg.Equals("[-]Finish[-]") == false) { txtInfo.Text += "\n" + innerMsg; cmbItems.Items.Add(innerMsg); } } } } |
it doesnt seem to work. no items are being added to the combobox. I am basically trying to detect that if the incoming text is of a certain value then go into a loop and keep recieving items and add them to the combo box until a certain incoming data has been reached. once that data has been recieved ([-]Finish[-]) then it should exit the loop
It doesnt seem to be doing that. It's pretty tough to explain. (Asynchronous is not an option here unfortunatly)
The data I get shown in the txtInfo is the entire string concatinated.
any ideas If you need clearer explanation let me know. I've been working on this for 2 days and i am really stuck.

server client data send problem
hotmailman
i was thinking about doing the sleep thing for the thread on the server side and did so but for 4 milliseconds (not long enough)
But I thought that this would really be ineffeciant and perhaps unprofessional and a cheap way of trying to do what I want
I will try the other way you mentioned. hope it works and I get it right!
i appreciate your time and help and has made it alot clearer :)
Chris Erickson
amit1agrawal
flagListSend = "[-]Start[-]";
byte[] msg = System.Text.Encoding.ASCII.GetBytes(flagListSend);
msg will look like this:
byte[] msg = new byte[] { '[', '-', ']', 'S', 't', 'a', 'r', 't', '[', '-', ']' };
There is no '\0' at the end, which is what your client code is expecting. Easiest solution is to append a trailing null.
What you're doing is quite low-level. You're also losing international characters as you're converting from Unicode (System.String) to ASCII. I would consider using a Web Service or even .NET Remoting. Then serialization would be taken care of for you by the framework. With a Web Service, you could write:
[WebService]
public class Jukebox : WebService {
[WebMethod]
public string[] GetSongs(int AlbumId) {
// Do something
return songs;
}
}
// WARNING: I just hacked the above code together in the textbox here. No guarentees this will even compile. Just trying to give you an example.
Then you could just bind the returned string[] to your databound controls.
Ahmed abd el hakim
I would strongly recommend to run your client and server on separate machines and investigate the behaviour. It might be different than running both on the same machine.
More than happy to assist. Happy coding.
GimGif
yup...dnt work
Greg Stolecki
Juan C. Mendez
Interesting. Wish I didnt need to really create a web service. I just want to notify the client "special data is coming"... the client then does certain things with that data until "end of special data" is sent from the server....
Martin Hansen
Still one question remains, and i still do not follow:
when client sends server (or vica versa) a message like "hi"... it recieves it fully without a problem
that should be the same if I tell it to go thru each file in the current directory and send the file name... should be the same as sending a msg on its own (like a chat program)
I just dont understand why the client seems to get it as 1 big string. or maybe its just something im doing wrong (and i know that but dont know what)
this is the pseudo I have but dont think it will work:
client:
if startspecial == true
{
add item to combobox
}
if incoming msg = "start"
set startSpecial = true
if incoming msg = "finish"
set startSpecial = false;
Marius Roets
thanks alot for your help :)
Glenn Burnside
idea of a webservice is of course better - I can add more features to it easily. I wonder if it will work with .NET CF (retrieving webservices). Worth a shot for sure!
Eze..
If all you're doing is creating standard web services using System.Web.Services in .NET and consuming them from .NET CF, they definitely work. If you need additional functionality above and beyond what .NET CF offers, check out http://www.OpenNETCF.org, a free extension to .NET CF created by the .NET CF MVPs among others.
Yannis26
To test this theory, you may want to try adding some:
System.Threading.Thread.CurrentThread.Sleep(1000);
to sleep your thread for 1 second after sending every line.