The previous two sections discussed about the short-range communications using infrared and Bluetooth (and serial port). Besides these two communication options, your device may also be connected to the Internet using GPRS, EDGE, or WIFI. In such cases, your applications will now be able to connect to servers and exchange information.
There are primarily two ways to connect to servers for data exchange:
- Using sockets (TCP/IP)
- Using Web services (covered in the last section of this article).
A socket is an abstract description of a means of sending and receiving data between computers/applications on a network. It describes a connection between two communicating points (which may be on different computers or within the same computer).
In practice, socket programming is commonly associated with TCP/IP and UDP/IP communications. TCP is a connection-oriented network protocol that guarantees reliable and in-order delivery of packets (through acknowledgements). Coupled with IP, TCP is adopted as a popular networking protocol by applications such as web browsers and email clients. In contrast, UDP is a connection-less network protocol that sends packets from one point to another, with one exceptionit does not provide reliable and guaranteed delivery.
Sockets programming using TCP/IP is straight-forwardone party listens for incoming data and the other party sends the data (both parties can listen for data and send data at the same time). When using sockets, you send the data in raw directly without needing to encapsulate the data using a messaging medium (such as XML), and because of this socket is an efficient way to exchange data between a device and a server (or another client). The downside to using sockets is that it is relatively more complex and requires the programmer to write the code for the server and the client.
To use sockets in your .NET CF application, import the following namespace:
For the purposes of this article, you will learn how to write a server and client application using a TCP socket.
You can listen for incoming data using the TcpListener class's AcceptTcpClient() method (if you recall the code for infrared communication the concept is similar). However, like the infrared example, the AcceptTcpClient() method is a blocking call, which means that the method will block until a client is connected. While this is alright for infrared communication (you only communicate with one device at a time), this is not really practical for sockets communication. For sockets communication, it is likely that you have multiple clients connected to your server at any one time and hence you need to do some extra work to ensure that your server supports multi-users.
To do so, create a class called Client and code it as shown in Listing 3.
Here, the constructor takes in a TcpClient object and starts to read from it asynchronously using the receiveMessage() method. Once the incoming data is read, it continues to wait for more data.
To ensure that the server supports multi-user, you use a TcpListener class to listen for incoming client connections and then use an infinite loop to accept new connections. Once a client is connected, you create a new instance of the Client object and continue waiting for the next client. This is shown in the code below:
const string SERVER_IP = "192.168.1.1";
const int PORT_NO = 500;
IPAddress localAddress = IPAddress.Parse(SERVER_IP);
//---listen at the specified address---
TcpListener listener = new TcpListener(localAddress, PORT_NO);
Client user = new Client(listener.AcceptTcpClient());
To connect to the server using a TCP socket, you will use the Socket class. Listing 4
shows how you can use the Socket class to connect to a TCP server and then sends some data using a byte array.