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