C # underlying protocol development (1)

Source: Internet
Author: User

What is an agreement? Let's leave the terminology aside. As the name suggests, it is a whisper of mutual cooperation (not afraid of making public ),

For example, if we have discussed it, 1 + 1 = 22, when I write 1 + 1, if you write 2, then

Violation of the agreement...

Similarly, when developing the underlying layer, you must first define the collaboration symbols between the client and the server (or client,

For example,

A: calculate 1 + 1 = 22;

B: Calculate 2 + 2 = 44;

Exit: exit;

 

Use C # for development. The generation is as follows:

 

Class Program
{

Static void main (string [] ARGs)
{
Appdomain. createdomain ("socketserver"). docallback (
Delegate
{
Byte [] result = new byte [1024];
Int myport = 8889;

IPaddress IP = IPaddress. parse ("192.168.2.136 ");
Ipendpoint IPE = new ipendpoint (IP, myport );
Socket serversocket = new socket (addressfamily. InterNetwork, sockettype. stream,
Protocoltype. TCP );
Serversocket. BIND (IPE );
Serversocket. Listen (1 );
Console. writeline ("An error occurred while listening! ");
Thread mythread = new thread (New threadstart (delegate {
While (true)
{
Socket clientsocket = serversocket. Accept ();
Clientsocket. Send (encoding. utf8.getbytes ("Hello !, You have already signed up with me! "));

Thread implements ETHREAD = new thread (delegate (Object clientsocket ){
Socket myclientsocket = (socket) clientsocket;
String casestr = "";
While (true)
{
Int receivenumber = myclientsocket. Receive (result );
// Console. writeline ("receiving client {0} message {1}", myclientsocket. remoteendpoint. tostring (), encoding. utf8.getstring (result, 0, receivenumber ));
Casestr = encoding. utf8.getstring (result, 0, receivenumber );
Switch (casestr)
{
Case "":
Console. writeline ("1 + 1 = 22 ");
Break;
Case "B ":
Console. writeline ("2 + 2 = 44 ");
Break;
Default:
Console. writeline ("No such suggestion ");
Break;
}

}
});

Receivethread. Start (clientsocket );
}

}));
Mythread. Start ();
});

IPaddress ipcl = IPaddress. parse ("192.168.2.136 ");
Socket clsocket = new socket (addressfamily. InterNetwork, sockettype. Stream, protocoltype. TCP );
Clsocket. Connect (New ipendpoint (ipcl, 8889 ));
Console. writeline ("successfully connected to the server \ r \ n ");
Byte [] rs = new byte [1024];
Int receivecount = clsocket. Receive (RS );
Console. writeline ("Receive message {0} \ r \ n", encoding. utf8.getstring (RS, 0, receivecount ));

// Clsocket. Send (encoding. utf8.getbytes ("I Am a client! \ R \ n "));
String STR = "";
While (true)
{
If (STR = "exit") return;
Clsocket. Send (encoding. utf8.getbytes (STR ));
STR = console. Readline ();
}

 
}

 

 

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.