2
Follows code from server:
int port = 11000/*PortGranter.GrantPort()*/;
TcpSocketListener listener = new TcpSocketListener();
var tcs = new TaskCompletionSource<ITcpSocketClient>();
await listener.StartListeningAsync(port);
listener.ConnectionReceived += (sender, args) => tcs.SetResult(args.SocketClient);
var socket2 = await tcs.Task;
await listener.StopListeningAsync();
// for five seconds, send and receive the data
var sentToSocket1 = new List<byte>();
var sentToSocket2 = new List<byte>();
var recvdBySocket1 = new List<byte>();
var recvdBySocket2 = new List<byte>();
// send random data and keep track of it
// also keep track of what is received
Func<ITcpSocketClient, List<byte>, List<byte>, CancellationToken, Task> sendAndReceive =
(socket, sent, recvd, token) =>
{
var r = new Random(socket.GetHashCode());
var send = Task.Run(async () =>
{
var buf = new byte[1000];
while (!token.IsCancellationRequested)
{
//r.NextBytes(buf);
//sent.AddRange(buf);
//await socket.WriteStream.WriteAsync(buf, 0, buf.Length, token);
//await socket.WriteStream.FlushAsync(token);
Byte[] data = Encoding.UTF8.GetBytes($"Amanhã não vou para escola, porque eu estou doente.<OAF>");
await socket.WriteStream.WriteAsync(data, 0, data.Length, token);
await socket.WriteStream.FlushAsync(token);
}
});
var recv = Task.Run(async () =>
{
var buf = new byte[1000];
while (!token.IsCancellationRequested)
{
var len = await socket.ReadStream.ReadAsync(buf, 0, buf.Length, token);
recvd.AddRange(buf.Take(len));
}
});
var innerTcs = new TaskCompletionSource<bool>();
token.Register(() => innerTcs.SetResult(true));
return innerTcs.Task;
};
Follows code from client:
int port = 11000/*PortGranter.GrantPort()*/;
TcpSocketClient socket1 = new TcpSocketClient();
await socket1.ConnectAsync("192.168.0.153", port);
var sentToSocket1 = new List<byte>();
var sentToSocket2 = new List<byte>();
var recvdBySocket1 = new List<byte>();
var recvdBySocket2 = new List<byte>();
//enviar dados aleatórios e acompanhar isso também acompanhar o que é recebido
Func<ITcpSocketClient, List<byte>, List<byte>, CancellationToken, Task> sendAndReceive =
(socket, sent, recvd, token) =>
{
var r = new Random(socket.GetHashCode());
var send = Task.Run(async () =>
{
var buf = new byte[1000];
while (!token.IsCancellationRequested)
{
r.NextBytes(buf);
sent.AddRange(buf);
await socket.WriteStream.WriteAsync(buf, 0, buf.Length, token);
await socket.WriteStream.FlushAsync(token);
}
});
var recv = Task.Run(async () =>
{
var buf = new byte[1];
while (!token.IsCancellationRequested)
{
var len = await socket.ReadStream.ReadAsync(buf, 0, buf.Length, token);
recvd.AddRange(buf.Take(len));
}
});
var innerTcs = new TaskCompletionSource<bool>();
token.Register(() => innerTcs.SetResult(true));
return innerTcs.Task;
};
string result = Encoding.UTF8.GetString(recvdBySocket1.ToArray());
In the variable result
returns text several times Amanhã não vou para escola, porque eu estou doente.<OAF>
.
Example of how it returns:
Amanhã não vou para escola, porque eu estou doente.<OAF>Amanhã não vou para escola, porque eu estou doente.<OAF>Amanhã não vou para escola, porque eu estou doente.<OAF>Amanhã não vou para escola, porque eu estou doente.<OAF>Amanhã não vou para escola, porque eu estou doente.<OAF>...
The library I use: https://github.com/rdavisau/sockets-for-pcl
Here’s a complete code similar to mine: https://github.com/rdavisau/sockets-for-pcl/blob/dev/Sockets/Tests/Sockets.Tests/TcpSocketClientTests.cs#L166
I just want to do something like this:
Client sends value to server after server send to client and FIM.
It was the only example I found on the internet of how to do bi-directional communication using PCL (Portable Class Libraries).
Some solution ?
It’s not the buffer limits you’re using?
– Leandro Angelo
@Leandroangelo But the buffer would not send part by part until finish ?
– Matheus Miranda
While you do not receive the token.Iscancellationrequested you read the buffer from 0 to its size
– Leandro Angelo
Matheus I don’t know if Victor got it right. Leandro called attention to the gap with
IsCancellationRequested
. As long as you do not cancel the server will continue to send the message. By the way, this request is horrible...– Bruno Costa
@Brunocosta, if you could show a simpler example, I’d appreciate it.
– Matheus Miranda
implement a
MLLP
becomes much simpler– Rovann Linhalis
@Rovannlinhalis I’ve never heard of him. I’m going to do some research.
– Matheus Miranda