It seems that N has written an article long ago, and people are too forgetful. Let's get started again.
The common understanding of socket is the combination of IP and port, so you can see that the way to define a socket is:
Int Port = 2000; string host = "127.0.0.1"; IPaddress IP = IPaddress. parse (host); ipendpoint IPE = new ipendpoint (IP, Port); socket S = new socket (addressfamily. interNetwork, sockettype. stream, protocoltype. TCP); // create a socket class S. BIND (IPE); // bind port 2000
After knowing the meaning of socket, let's look at the example and create two console projects.
// Serverstatic void main (string [] ARGs) {try {int Port = 2000; string host = "127.0.0.1"; IPaddress IP = IPaddress. parse (host); ipendpoint IPE = new ipendpoint (IP, Port); socket S = new socket (addressfamily. interNetwork, sockettype. stream, protocoltype. TCP); // create a socket class S. BIND (IPE); // bind port 2000 s. listen (0); // starts listening to the console. writeline ("Wait For connect"); socket temp = S. accept (); // create a new socket for the new connection. Console. writeline ("Get a connect"); string recvstr = ""; byte [] recvbytes = new byte [1024]; int bytes; bytes = temp. receive (recvbytes, recvbytes. length, 0); // receive information from the client recvstr + = encoding. ASCII. getstring (recvbytes, 0, bytes); console. writeline ("Server get message: {0}", recvstr); // display the client information string sendstr = "OK! Client send message sucessful! "; Byte [] BS = encoding. ASCII. getbytes (sendstr); temp. send (BS, BS. length, 0); // return the client success message temp. close (); S. close ();} catch (argumentnullexception e) {console. writeline ("argumentnullexception: {0}", e) ;}catch (socketexception e) {console. writeline ("socketexception: {0}", e);} console. writeline ("press enter to exit"); console. readline ();}
// Client static void main (string [] ARGs) {try {int Port = 2000; string host = "127.0.0.1"; IPaddress IP = IPaddress. parse (host); ipendpoint IPE = new ipendpoint (IP, Port); // convert IP and port to ipendpoint instance socket c = new socket (addressfamily. interNetwork, sockettype. stream, protocoltype. TCP); // create a socket console. writeline ("conneting... "); C. connect (IPE); // connect to the Server String sendstr = "Hello! This is a socket test "; byte [] BS = encoding. ASCII. getbytes (sendstr); console. writeline ("Send message"); C. send (BS, BS. length, 0); // send the Test message string recvstr = ""; byte [] recvbytes = new byte [1024]; int bytes; bytes = C. receive (recvbytes, recvbytes. length, 0); // The slave server accepts the returned information recvstr + = encoding. ASCII. getstring (recvbytes, 0, bytes); console. writeline ("client get message: {0}", recvstr); // display the Server Response Information C. close ();} catch (argumentnullexception e) {console. writeline ("argumentnullexception: {0}", e) ;}catch (socketexception e) {console. writeline ("socketexception: {0}", e);} console. writeline ("press enter to exit"); console. readline ();}