Communication (communication) based on Socket UDP development of a multi-person chat room
Introduced
Distinctive Windows Phone 7.5 (SDK 7.1) communication
Example-Developing a multiplayer chat room based on Socket UDP
Example
1. Service End
Main.cs
* * * Socket UDP chat room service side * Note: UDP message (Datagram) The maximum length of 65535 (including the message header)/using System;
Using System.Collections.Generic;
Using System.ComponentModel;
Using System.Data;
Using System.Drawing;
Using System.Linq;
Using System.Text;
Using System.Windows.Forms;
Using System.Net.Sockets;
Using System.Net;
Using System.Threading;
Using System.IO;
namespace SOCKETSERVERUDP {public partial class Main:form {SynchronizationContext _synccontext;
System.Timers.Timer _timer;
Client Endpoint Collection Private list<ipendpoint> _clientlist = new list<ipendpoint> ();
Public Main () {InitializeComponent ();
UI thread _synccontext = synchronizationcontext.current;
Start background thread receive data thread thread = new Thread (new ThreadStart (Receivedata)); Thread.
IsBackground = true; Thread.
Start (); Every 10 secondsRun the method specified by the timer, mass information _timer = new System.Timers.Timer (); _timer.
Interval = 10000d; _timer.
Elapsed + = new System.Timers.ElapsedEventHandler (_timer_elapsed); _timer.
Start ();
///Receive Data private void Receivedata () {//Instantiate a udpclient, listen for specified port, for receiving information
UdpClient listener = new UdpClient (3367);
Client endpoint IPEndPoint clientendpoint = null; try {while (true) {//wait until the data is received (you can get the data received and the client end Point) byte[] bytes = listener.
Receive (ref clientendpoint);
String strresult = Encoding.UTF8.GetString (bytes);
Outputmessage (strresult);
Add clients that send this information to the client endpoint collection if (!_clientlist.any p => p.equals (Clientendpoint))
_clientlist.add (Clientendpoint); } catch (Exception ex) {Outputmessage (ex).
ToString ());
} private void _timer_elapsed (object sender, System.Timers.ElapsedEventArgs e) { Mass information SendData (string) every 10 seconds. Format (Webabcd says to everyone: Hello!
"Information comes from service side {0}" ", DateTime.Now.ToString (" Hh:mm:ss "));
}//Send data private void SendData (string data) {//Send information to every client that has sent information to the server
foreach (IPEndPoint EP in _clientlist) {//Instantiate a udpclient for sending information
UdpClient udpclient = new UdpClient ();
try {byte[] bytedata = UTF8Encoding.UTF8.GetBytes (data);
Sends the message to the specified client endpoint and returns the number of bytes sent int count = Udpclient.send (Bytedata, Bytedata.length, EP);
catch (Exception ex) {Outputmessage (ex.
ToString ());
}//Close UdpClient//Udpclient.close (); }///output The specified information on the UI private void Outputmessage (string data) {_syncco ntext.
Post ((p) => {txtmsg.text + = p.tostring () + "\ r \ n";}, data); }
}
}