Next we try to implement the simplest socket client, in order to ensure that only the problem of your code, the server first with the others mature code test ( This is also a technique of programming, do not write their own client test their own written server, so there is a problem you do not know who has the problem, May also have problems, but the same to come, unsure of the first use of others , first open the socket server
?
Try to connect to the server, just refer to the practice on the Internet, call the socket connect method and the Disconnect method (the specified IP address and port as required to the IPEndPoint type), You can find that sockettest software does not only prompt the new client and client closed connection, which is what we want to learn, do the client if the connection is successful to prompt, do the server if the client connection is successful also to prompt.
?
If in order to be more rigorous can be before the conversion to determine whether a qualified IP address and port, IP address online can find a lot of detection means, Each xxx.xxx value has a limit on the line, the port I fill in the 1234, in fact, can also fill larger or smaller, the specific maximum minimum value has not been studied, but do not fill in some of the default is occupied by the port, such as 80 port by default is the HTTP protocol, 21 is the default FTP port, open its software ports can see often Definition of TCP ports, these ports do not occupy
?
After the connection is considered to send a message to the server, the sending of data is relatively simple, note can not be sent directly to the string, but to a byte array (readers can go to see the relevant text, why to turn into a byte array, may consider different operating systems, software for the processing of the text is different, So you need to make a universal byte stream, and you can also find that the simple send method is not supported in Chinese, he received something is garbled (this problem we will consider later)
?
Consider accepting data from the server after the send is complete. Receiving data is much more troublesome than sending it, and here we demonstrate the simplest method of receiving (s). recieve), once the user clicks on the Received button, then the window can not move, which is also the biggest feature of synchronous Socekt communication (that is, after receiving the completion of the next to do other).
?
Readers or imagine a hotel room, if the client does not have recieve, the server is to send data, after the client after the recieve will be what? The test can find that if the server first throws something to the socket, the client receives it immediately, and of course s. Once the recieve method is executed, it is not blocked and the window is active immediately.
?
With the above two-step test, we find that receiving data will cause the program to die, so how to solve the problem that the Receive method causes the whole program to die? (In fact, the user should not click on the reception to perform the reception, but at any time can receive the server sent the data, as we can at any time to send a message to the server), the simplest way is to put the dead part of a thread to execute, as long as the connection is successful, we will start the thread and into a dead loop, Blocking of recieve in thread does not cause the main program to die
?
But how does it update to the main interface after collecting the data? If we are in the thread to update the main interface Label,textbox will be error, once the server clicked on the Send button, we want to update the program Interface text box, the label and so on will be prompted from the thread is not the creation of the control to access it
?
If readers surf the web, adding this "Turn off cross-thread access control detection" When the program is initialized is the simplest and most brutal solution, but actually if the reader does the actual project, there are a lot of threads in this way to update the interface control, the program is not stable (you can test one hours, You don't have to run for three days in a row. Our next section will address this issue in conjunction with the event and delegate approach.
?
?
For more instructional videos and downloads, please pay attention to the following information:
My Youku Space:
Http://i.youku.com/acetaohai123
?
My online forum:
http://csrobot.gz01.bdysite.com/
?
Problem Exchange:
qq:910358960
Email:[email protected]
?
?
?
?
?
C # application Video tutorial 1.2 Socket Communication Client implementation