Quick understanding of socket

Source: Internet
Author: User

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

 

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.