Application example of C#socket in asp.net

Source: Internet
Author: User
The code is as follows Copy Code

Server using System.Collections.Generic;
Using System.Text;
Using System.Net.Sockets;
Using System.Net;

Namespace Sockettest
{
Class Program
{
static void Main (string[] args)
{
Socket Serversk = new socket (addressfamily.internetwork, SocketType.Stream, protocoltype.tcp);

Serversk.bind (New IPEndPoint (Ipaddress.parse ("127.0.0.1"), 8596);
Serversk.listen (1);

SocketAsyncEventArgs acceptsae = new SocketAsyncEventArgs ();
acceptsae.completed + = new eventhandler<socketasynceventargs> (acceptsae_completed);
Serversk.acceptasync (ACCEPTSAE);

Console.ReadLine ();
}

static void Acceptsae_completed (object sender, SocketAsyncEventArgs e)
{
Socket Serversk = sender as socket;
if (E. SocketError = = socketerror.success)
{
Serversk = E.acceptsocket;
SocketAsyncEventArgs sendsae = new SocketAsyncEventArgs ();
byte[] data = System.Text.Encoding.UTF8.GetBytes ("Ok,just put it!" );
Sendsae.setbuffer (data, 0, data.) Length);
sendsae.completed + = new eventhandler<socketasynceventargs> (sendsae_completed);

SocketAsyncEventArgs recievesae = new SocketAsyncEventArgs ();
byte[] buffer = new byte[2048];
Recievesae.setbuffer (buffer, 0, buffer. Length);
recievesae.completed + = new eventhandler<socketasynceventargs> (recievesae_completed);

                Serversk.receiveasync (RECIEVESAE);
                Serversk.sendasync (SENDSAE);
           }
            Else
                 Console.WriteLine ("Take over collection socket connection request failed!") Specific reasons please debug yourself! ");
       }


static void Recievesae_completed (object sender, SocketAsyncEventArgs e)
{
Socket SK = sender as socket;
byte[] data = E.buffer; Attention here, how to take the value of the buffer that relates to the socket's sending takeover.
String msg = System.Text.Encoding.UTF8.GetString (data);
Console.WriteLine ("Message Received:" + msg);

Sk. Disconnectasync ()//What do you think you should do?
}

        static void Sendsae_completed (object sender, SocketAsyncEventArgs e)
        {
             Socket SK = sender as socket;
            if (E. SocketError = = socketerror.success)
            {
                Console.WriteLine (" Send complete! " ),

byte[] data = E.buffer; Attention here, how to take the value of the buffer that relates to the socket's sending takeover.
String msg = System.Text.Encoding.UTF8.GetString (data);
Console.WriteLine ("What You Sent:" + msg);
}
}
}
}


2 Client code

The code is as follows Copy Code

Cilent using System.Collections.Generic;
Using System.Text;
Using System.Net.Sockets;
Using System.Net;

Namespace Sockettesttwo
{
    class program
    {
   & nbsp;    static void Main (string[] args)
        {
             Socket Clientsk = new Sockets (AddressFamily.InterNetwork, SocketType.Stream, protocoltype.tcp);

SocketAsyncEventArgs connectsae = new SocketAsyncEventArgs ();
Connectsae.remoteendpoint = new IPEndPoint (Ipaddress.parse ("127.0.0.1"), 8596);
connectsae.completed + = new eventhandler<socketasynceventargs> (connectsae_completed);
Clientsk.connectasync (CONNECTSAE);
Console.ReadLine ();
}

static void Connectsae_completed (object sender, SocketAsyncEventArgs e)
{
Socket Clientsk = sender as socket;
if (E. SocketError = = socketerror.success && clientsk.connected)
{
SocketAsyncEventArgs sendsae = new SocketAsyncEventArgs ();
byte[] data = System.Text.Encoding.UTF8.GetBytes ("I want put all Funy things together!" );
Sendsae.setbuffer (data, 0, data.) Length);
sendsae.completed + = new eventhandler<socketasynceventargs> (sendsae_completed);

SocketAsyncEventArgs recievesae = new SocketAsyncEventArgs ();
byte[] buffer = new byte[2048];
Recievesae.setbuffer (buffer, 0, buffer. Length);
recievesae.completed + = new eventhandler<socketasynceventargs> (recievesae_completed);

               //Call the asynchronous takeover first, and then invoke the asynchronous send. It allows you to experience asynchronous, apparently unusual, emotional infections.
                Clientsk.receiveasync (RECIEVESAE);
                Clientsk.sendasync (SENDSAE);
           }
       }

static void Recievesae_completed (object sender, SocketAsyncEventArgs e)
{
Socket SK = sender as socket;
byte[] data = E.buffer; Attention here, how to take the value of the buffer that relates to the socket's sending takeover.
String msg = System.Text.Encoding.UTF8.GetString (data);
Console.WriteLine ("Message Received:" +msg);

Sk. Disconnectasync ()//What do you think you should do?
}

static void Sendsae_completed (object sender, SocketAsyncEventArgs e)
{
Socket SK = sender as socket;
if (E. SocketError = = socketerror.success)
{
Console.WriteLine ("Send complete!" );
}
}


}
}

3 Debug Run

To debug the Run Manager project first

 

Debug Run client project

 

4 View Run results

 < /p>

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.