[Mono-list] Threads & Asynchronous Sockets
Giuseppe Greco
giuseppe.greco@agamura.com
Thu, 16 Oct 2003 08:20:39 +0200 (CEST)
... and what happens If I change the ReceiveBufferSize
option from another thread while Socket.ReceiveFrom()
is receiving a datagram?
Gius_.
> Hi all,
>
> Look at the following class:
>
>
> public MyClass
> {
> private Socket socket;
> private Thread listener;
> ...
>
> public MyClass(...)
> {
> socket = new Socket(...);
> ...
>
> listener = new Thread(new ThreadStart(Listen));
> listener.IsBackground = true;
> listener.Start();
> }
>
> private void Listen()
> {
> int recv = 0;
> byte[] data = null;
> EndPoint remoteEndPoint = new IPEndPoint(IPAddress.Any, 0);
>
> while (true) {
> if (socket.Poll(PollTimeOut, SelectMode.SelectRead)) {
> data = new byte[socket.Available];
> recv = socket.ReceiveFrom(
> data,
> 0,
> data.Length,
> SocketFlags.None,
> ref remoteEndPoint);
>
> if (recv > 0) {
> DoSomething();
> }
> }
> }
> }
>
> public void Send(byte[] data, IPEndPoint remoteEndPoint)
> {
> socket.BeginSendTo(
> data,
> 0,
> data.Length,
> SocketFlags.None,
> remoteEndPoint,
> new AsyncCallback(AsyncSend),
> null);
> }
>
> private void AsyncSend(IAsyncResult asyncResult)
> {
> socket.EndSendTo(asyncResult);
> }
> }
>
> As you can see, there is a thread that listens for
> incoming datagrams, and an asynchronous Send method.
> Now the question is: since the two threads work
> on the same socket, could they interfere each other?
>
> I guess they should work fine since sends and receives
> use different socket buffers...
>
> Any comment is appreciated!
>
> Thanks,
> Gius_.
>
> ----------------------------------------
> Giuseppe Greco
>
> ::agamura::
>
> phone: +41 (0)91 604 67 65
> mobile: +41 (0)76 390 60 32
> email: giuseppe.greco@agamura.com
> web: www.agamura.com
> ----------------------------------------
> _______________________________________________
> Mono-list maillist - Mono-list@lists.ximian.com
> http://lists.ximian.com/mailman/listinfo/mono-list
>
----------------------------------------
Giuseppe Greco
::agamura::
phone: +41 (0)91 604 67 65
mobile: +41 (0)76 390 60 32
email: giuseppe.greco@agamura.com
web: www.agamura.com
----------------------------------------