Different Windows Phone (31)

Source: Internet
Author: User
Tags socket thread

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); } 
    } 
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.