Important Notes on TCP Application Programming Server

Source: Internet
Author: User
1 tcp Application Programming server Important Notes 2 1. The tcplistener class is used to listen and accept incoming connection requests. 3 tcpclient class is used to provide connection information between the local host and remote host.

4 2. tcpclient Class 5 is mainly used to write client programs, and you need to directly use the constructor to create a tcpclient object. 6 constructors have four reload forms: 7 tcpclient () 8 tcpclient (addressfamily family) 9 tcpclient (ipendpoint IEP) 10 tcpclient (string hostname, int port) 11 (1) tcpclient (): 12/------- this constructor creates a default tcpclient object and automatically allocates the Local Machine (Client IP address and port number. ---------/13 tcpclient = new tcpclient (); 14 tcpclient. connect ("www.abcd.com", 51888); 15 16 (2) tcpclient (addressfamily family): 17/------ addressfamily enumeration specifies which network protocol to use. -------/18 tcpclient = new tcpclient (addressfamily. interNetwork); 19 tcpclient. connect ("www.abcd.com", 51888); 20 21 (3) tcpclient (ipendpoint IEP): 22/------ this constructor parameter IEP specifies the local (client) IP address and port number. This method can be used when the client has more than one IP address and the programmer wants to directly specify the IP address and port number used. If this method is used --------/23 IPaddress [] address = DNS. gethostaddresses (DNS. gethostname (); 24 ipendpoint IEP = new ipendpoint (Address [0], 51887); 25 tcpclient = new tcpclient (IEP); 26 tcpclient. connect ("www.abcd.com", 51888); 27 28 (4) tcpclient (string hostname, int port) 29/------- this is the most convenient constructor. In the parameter, hostname indicates the DNS name of the remote host to be connected, and port indicates the port number of the remote host to be connected. The constructor automatically allocates the most appropriate local host IP address and port number, parses the DNS, and establishes a connection with the remote host -------/30 tcpclient = new tcpclient ("www.abcd.com ", 51888); 31

32 3. the getstring () method in the tcpclient object can obtain the networkstream object 33 For example: this. client = client; 34 networkstream = client. getstream (); 35. networkstream object 36 is used for data transmission between the server and the customer, for example: Public binaryreader BR; 37 public binarywriter bw; 38 BR = new binaryreader (networkstream ); /* The above are member variables and constructor methods in the user class. When 39 BW = new binarywriter (networkstream); the Code in */40 string receivestring = NULL; 41 // The user object here is the tcplistener object. After it is created, call the START () method and the accepttcpclient () method to obtain the tcpclient object connected to the server, the specific operation details 42 receivestring = user.br in the knowledge point of tcplistener. readstring (); // read the string from the network stream. This method automatically determines the string length prefix and reads the string 43 Based on the length prefix.


44. you can call the string static method format to set the output format. 45 example: additemtolistbox (string. format ("starting at {0 }:{ 1} listening to customer connections", localaddress, Port); 46


47. the split () method of a string object can be used to break down the specified delimiter in a string and split it into several strings. For example, string [] splitstring = receivestring. split (',') // separate the receivestring string with commas (,) and store all the strings in a string array. For example, string command = message. split (',') [0]. tolower (); // The string message is separated by commas (,) into several substrings. The first substring is ????????????????? 50


51 6. the substring (int I) method of the object of the string class can extract the sub-string 52 whose subscript is I to the end, for example, string talkstring = receivestring. substring (splitstring [0]. length + splitstring [1]. length + 2); 53


54 7. create and use a tcplistener object: 55 tcplistener mylistener; 56 mylistener = new tcplistener (localaddress, Port); 57 mylistener. start (); // The staty () function starts to listen to 58 tcpclient newclient = NULL; 59 newclient = mylistener. accepttcpclient (); // on the server, the tcpclient object must use accepttcpclient (); method to obtain 60 mylistener. stop (); // code example 61


62 8. thread thread concepts and usage: 63 (1) a thread needs to be created for server listening operations; 64 (2) the server is in the thread for listening operations, if a client connection exists, create another thread to process interaction with the corresponding user. 65 (3) Example of thread usage: 66 example: thread mythread = new thread (listenclientconnect); 67 mythread. start (); 68 // In the above usage, the listenclientconnect parameter in the thread constructor is a method pointer, that is, the method name. If this method has no parameters, as shown in the preceding figure, the creation thread 69 is: thread threadreceive = new thread (receivedata); 70 threadreceive. start (User); 71 // This is the case where the receivedata has parameters. You can only enter the corresponding parameters in the START () method.

 

Important Notes on TCP Application Programming Server

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.