[MonoTouch] Socket Connection

akobold akobold at bol.com.br
Sat Dec 17 06:19:04 EST 2011


Hello everyone.
I'm writing an iPad app that connects to a server over wifi.
However, the server may not be available.
The protocol is simple. When I connect to the server, the server dumps a lot
of data to the client (me)
If I connect to the socket with timeout, the asynchronous receive does not
work. If i connect directly it works fine.
I'm assuming that I am doing something wrong, but I cannot figure out what
is.
my code is this:

Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream,
ProtocolType.Tcp);

//this works fine:
sock.Connect(address, port);
sock.BeginReceive(rxBuffer, 0, rxBuffer.Length, SocketFlags.None, new
AsyncCallback(DataReceived), this);

//however, if i replace the two last lines for this:
IAsyncResult r = sock.BeginConnect(address, port, null, null);
r.AsyncWaitHandle.WaitOne(timeout, true);
if(!sock.Connected) {
	sock.Close();
	throw new SocketException(10053); //connection aborted
}
sock.EndConnect(r);
sock.BeginReceive(rxBuffer, 0, rxBuffer.Length, SocketFlags.None, new
AsyncCallback(DataReceived), this);

//the DataReceived function is called only one, for the first packet and
nothing else
btw: the idea here is to make a timeout to the connect method in case the
server is offline.

Any ideas what I may be doing wrong?
thank you for your attention.


--
View this message in context: http://monotouch.2284126.n4.nabble.com/Socket-Connection-tp4207965p4207965.html
Sent from the MonoTouch mailing list archive at Nabble.com.


More information about the MonoTouch mailing list