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