Udostępnij za pośrednictwem


Sockets for Windows Phone 8

[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]

Windows Phone provides a programming interface to enable developers to create applications that can communicate with internet services and other remote applications using sockets. Examples of applications and services that use sockets to communicate include FTP, email, chat systems, and streaming multimedia. Using sockets in your Windows Phone application enables you to create rich client applications that can communicate with services over Transmission Control Protocol (TCP) or User Datagram Protocol (UDP) sockets.

This topic contains the following sections.

Sockets Support on Windows Phone

Windows Phone provides the programming interface needed to create and use TCP and UDP sockets. You can select which type of socket to use based on your application’s needs. The following diagram shows a view of the operations that take place during a communication session between a client application and a service. For an explanation of the terms used, see Terminology.

The following table explains each operation labeled in the diagram.

Operation

TCP

UDP

1

To communicate over TCP, a connection must be established between the client and the server. The endpoint to which the client wants to communicate must be defined as part of the connection request. This is an asynchronous operation in Windows Phone.

Communication over UDP is connectionless, meaning a connection does not have to be created prior to communication.

2

Once the connection has been successfully established, the client can send data to the server by setting up a buffer of data and passing it to the server. TCP is stream-based and the order in which the data is received is guaranteed to be in the order in which it was sent. The TCP protocol takes care of this ordering and reliability for the transmission.

A UDP socket can begin communicating by creating a send request and passing the buffer of data to the server. The successful receipt of the data by the server and the order in which it is received is not guaranteed. If the client requires this certainty, then this must be custom implemented on both the client and the server.

3

The client can request to receive data from the server. This is an asynchronous call and, if successful, the resulting callback will contain the buffer of data that was sent.

A UDP socket can receive data from a service by “listening” on the port associated with this service for any incoming data, and processing it as appropriate.

4

The send and receive pattern in operations 2 and 3 can be repeated for as long as the socket remains connected.

The client can continue to send and receive data.

5

Once the client has finished communicating, it calls shutdown to inform the server that the socket is terminating. This call is used to make sure the remaining data from the server is received before the socket disconnects.

6

Finally, the client disconnects the socket and closes the communication channel.

7

At this point, there is no active socket channel, and data sent to the client will be lost.

At this point, there is no active socket channel, and data sent to the client will be lost.

TCP and UDP on Windows Phone

The following is a comparison of the characteristics of TCP and UDP sockets on Windows Phone.

TCP

UDP

Transmission Type

Stream-based

Datagram

Example Uses

Email, Remote Administration, File Transfer, Web

Streaming Multimedia, Online Games, Internet Telephony

Unicast

Yes

Yes

Any Source Multicast (ASM)

No

Yes

Source-specific Multicast (SSM)

No

Yes

Broadcast

No

No

Connectionless or Connected

Connection-oriented

Connectionless

Reliable Communication

Yes

No

See Also

How to create and use a TCP socket client app for Windows Phone 8

How to create and use a UDP socket client app for Windows Phone 8

How to send and receive data in a multicast group for Windows Phone 8

Terminology

A socket is a mechanism for delivering data packets or messages between applications or processes. In programming terms, a socket is a programming interface against the TCP/IP protocol stack. Sockets are identified on a network through a socket address, which is a combination of Internet protocol (IP) address and port number. The following table lists some common terminology that you should become familiar with as you work with sockets in your Windows Phone applications.

Term

Description

Broadcast

To send data to all devices on a network.

Client

In socket communication, the consumer of a service provided by a server. For example, a chat client is a consumer of a chat service and can use that service to establish a chat session with other clients. An application running on a Windows Phone device is a client application that can consume a service over sockets.

Connectionless

Communication in which a connection does not need to be set up between the sending socket and the receiving socket prior to the communication starting. In this mode, there is no guarantee that the recipient is ready to receive the data and there is also no acknowledgement that the data was ever received, or received with no errors. A UDP socket provides a connectionless communication interface.

Connection-oriented

Communication in which a socket must first set up a connection to a destination socket prior to sending or receiving data. Once a connection is established, a stream of data can be sent and it will be received in the same order. A TCP socket provides a connection-oriented communication interface.

Endpoint

A communication port on either side of the communication. It is typically defined by an IP address, supported transport protocol type, and port number.

IP Address

The industry-standard naming convention for devices on a network. It is a binary number, usually stored in a human readable format such as 172.36.254.14.

IPv4

The older 32-bit addressing system for devices on the Internet. An example of an IPv4 address in human-readable form is 172.36.254.14.

IPv6

The latest 128-bit addressing system for devices on a network. It was developed to accommodate the ever-increasing growth of the number of devices on the Internet. An example of an IPv6 address in human-readable form is fe80::e42b:2e74:6ddb:e30.

Important Note:
IPv6 is not supported in sockets for Windows Phone OS 7.1.

Multicast

Sending data to devices on a network that have registered interest in that data by joining a multicast group.

Port Number

A number that, combined with an IP address and the transport protocol it supports for communication, identifies a port or endpoint on a network. A well-known list of ports has been reserved for use by specific services such as Telnet (23) and HTTP (80). Other numbers are available for use by other services and applications.

Server

A device on a network that provides a service, or multiple services, for consumption by clients. As an example, a chat server provides a chat service that can be used by chat clients to establish chat sessions with other clients. Although applications on a Windows Phone device can send and receive data over a socket, they are not considered servers.

Socket

A programming interface to communicate with other applications or services on a network.

Transmission Control Protocol (TCP)

An Internet standard that guarantees reliable, in order, delivery of messages on a network.

TCP/IP

The suite of communication protocols used on the Internet and other networks. It was named after the first two protocols that were added to this standard, namely, TCP and IP, but it consists of four layers of protocols, with the TCP and UDP protocols being parts of the Transport Layer.

User Datagram Protocol (UDP)

A Transport Layer protocol used to transmit datagrams in a connectionless manner, meaning that no prior connection needs to be established before sending and receiving messages. This characteristic makes UDP a fast transport protocol, but it can have disadvantages over TCP in terms of reliability since receipt of these datagrams by the destination is not guaranteed and no acknowledgement is sent by default.

Unicast

Sending data to a specific destination with a uniquely identifiable address in a network.

Windows.Net.Networking

The Windows Runtime API, Windows.Networking.Sockets, has been adopted for Windows Phone 8. It has been implemented as a Windows Phone Runtime API, making it easy to use in whatever supported programming language you choose. Although we've enhanced the .NET API, System.Net.Sockets, to support more features such as IPv6 and listener sockets, you should consider using the new API for sockets programming because it is more portable than the .NET API. Windows.Networking.Sockets has been built from the ground up to be clean, secure, and easy-to-use APIs that enforce best practices. For more info about supported Windows Runtime API, see Windows Phone Runtime API.

WinSock Support

Windows Phone 8 now supports WinSock APIs to enable low-level sockets programming. For info on the supported APIs, see Supported Win32 APIs for Windows Phone 8

See Also

Reference

System.Net.Sockets

Windows.Networking.Sockets..::.StreamSocket

Other Resources

Network and network interface information for Windows Phone 8

Samples gallery on Windows Phone Dev Center

Windows Phone 8: Networking, Bluetooth, and NFC Proximity for Developers (Build 2012)

Supported Win32 APIs for Windows Phone 8