Silverlight 2.0 Sockets; Sending Data between Client & Servers.
Last post 05-11-2008 10:51 AM by Kinjara. 2 replies.
Sort Posts:
05-09-2008 6:21 PM
Silverlight 2.0 Sockets; Sending Data between Client & Servers.

Greetings,

first off, my experience with sockets is limited.
I am trying to use this example(http://weblogs.asp.net/dwahlin/archive/2008/04/13/pushing-data-to-a-silverlight-client-with-sockets-part-ii.aspx) as base for my own application, but i need a way to send data from the client(s) to the server and back to the clients.
As so far i tried to bluntly copy the TCP-connection (tpc is not recognized in Silverlight 2.0 sockets?).

My second idea was to just use sockets (Socket listener = new Socket()) and while i could receive data on the server(yeeh) and i could send data out with "client.Sock.Send(BinArrayLoad);".
The client didn't recognize and/or OnSocketReceive did not fire. Personally i think its cause SocketAsyncEventArgs isn't raised on an socket-connection?
If anyone knows an solution towards remedieing this problem or another way to send data from Client --> Server, i would be very greatfull.

With Kind Regards,
Kinjara
Enlightened_Freaks[@]hotmail.com

ps. i am not totally sure if this kind of posts is the place for this but i am stuck on the limitations of the Silverlight 2.0 sockets (atleast i think i am)

Kinjara

Joined on 12-08-2007
Posts 11
05-11-2008 7:12 AM
Marked as Answer
Re: Silverlight 2.0 Sockets; Sending Data between Client & Servers.

Soo seeing the example. what to do to send Data between Client & Server? (Yes i like to reply to my own posts. I hate when i google and find the question but not the answer)

Server:
On line 44:
// Buffer for reading data
Byte[] bytes = new Byte[256];
String data = null;

On line 54:
Thread
.Sleep(500); //if you remove this, it will be to fast and it will fail.
NetworkStream stream = client.GetStream();
//lets lissen for our beloved clients
int i = 0;
foreach (StreamWriter writer in _ClientStreams)
{
NetworkStream Usedstream = client.GetStream();
while ((i = Usedstream.Read(bytes, 0, bytes.Length)) != 0)
{
// Translate data bytes to a UTF8 string.
data = System.Text.Encoding.UTF8.GetString(bytes, 0, i);
OnRecieve(data); //OnRecieve, process data there.
}}

Client:
First off, make the following declerations class-wide:
//declarations to SEND data
Socket socket = null;
DnsEndPoint endPoint = null;
SocketAsyncEventArgs args = null;

than add the following fucntion in your client:
private void SendDate(string Data)
{
Byte[] Bytes = new Byte[255];
Bytes =
Encoding.UTF8.GetBytes(Data);
SocketAsyncEventArgs Async = new SocketAsyncEventArgs();
Async.SetBuffer(Bytes, 0, Bytes.Length);
Async.UserToken = socket;
Async.RemoteEndPoint = endPoint;
socket.SendAsync(Async);
Async.SetBuffer(
null, 0, 0);
}

Kinjara

Joined on 12-08-2007
Posts 11
05-11-2008 10:51 AM
Re: Silverlight 2.0 Sockets; Sending Data between Client & Servers.

For those that might have tried the last solution,  will have seen the same thing as i did.
It only works on 1 client, while that is fun and all...its too limited to be usefull, so i made a thread out of the listening part:

For Threads we need 1 function to call, like:

private void ClientListener()   {
Thread.Sleep(500);
// Buffer for reading data
Byte[] bytes = new Byte[256];
String data = null;
while (true) {

//Now we listen for the wind of change...uhm our clients.
NetworkStream stream = client.GetStream();

int i = 0;
foreach (StreamWriter writer in _ClientStreams)  {
NetworkStream Usedstream = client.GetStream();
while ((i = Usedstream.Read(bytes, 0, bytes.Length)) != 0)  {
// Translate data bytes to a ASCII string.
data = System.Text.Encoding.UTF8.GetString(bytes, 0, i);
OnRecieve(data);   }  }  }  }

the variable Client has been made globale and it is filled on connecting but that part is beter explained in the blog ini refere to in my original post.
now except that the code has been made more tidy, we can start the thread like this:

while (true)  {
_Listener.BeginAcceptTcpClient(
new AsyncCallback(OnBeginAccept), null);
_TcpClientConnected.WaitOne();
//Block until client connects
Thread ListenForClients = new Thread(new ThreadStart(ClientListener));
ListenForClients.Start(); }

The While true makes it possible to have, dont pin me down on this, an infinite amount of clients that we can all send data depending on one clients action. Anyways hope this post helps atleast someone that was stuck on the sending/recieving data between client/server.

If you lost me somewhere cause of my pore english and high ramblingrate, feel free to mail me so i can send you the project.

Kinda Regards,

Kinjara
Enlightened_Freaks{ad}hotmail.com

 

Kinjara

Joined on 12-08-2007
Posts 11