C # network programming-client-server chat using TCP

Source: Internet
Author: User
Tags getstream

Recently, in Socket network socket programming, the client and server are always unable to respond in synchronous and asynchronous communication, but the communication chat function has been completed in TCP programming, the following is a brief description of the source code and detailed comments of the TCP chat I recently learned.

TCP is a transport layer protocol. In TCP programming, it usually uses three classes, and its namespace is system. net. sockets:

1. tcplistener: developed based on the TCP server and monitors whether the IP address and port number are connected.

Common methods of this type include START () Start listening, acceptsocket () return socket accept connection request, accepttcpclient () return client object accept connection request, stop () Stop listening

2. tcpclient: provides client connection based on tcp client programming and sends and receives data through network connection.

This type of frequently used methods include connect () and server host connection, getstream () used to obtain the agreed data stream, close () closed connection

3. networkstream: Used to get and operate network streams. In this program, you can write streams and read stream objects to write and read data.

Common methods of this type include read () reading data from network streams, write () writing data from network streams.

The source code of this example is as follows)

1. server code (tcpserver)

Using system; using system. collections. generic; using system. LINQ; using system. text; using system. threading. tasks; // Add a new namespace using system. net; using system. net. sockets; using system. io; // stream streamreadernamespace tcpserver {class program {static void main (string [] ARGs) {// parse converts string to IP Address type IPaddress myip = IPaddress. parse ("127.0.0.1"); // construct a tcplistener (IP address, Port) object, TCP server tcplistener myserver = new tcplistener (myip, 6688); // start listening to myserver. start (); console. writeline ("waiting for a connection... "); // construct a tcp client: accept the connection request tcpclient client = myserver. accepttcpclient (); console. writeline ("the client has been connected... "); // construct the networkstream class, which is used to obtain and operate the network stream networkstream = client. getstream (); // read data stream object streamreader sr = new streamreader (Stream); // write data stream object streamwriter Sw = new streamwriter (Stream); While (true) {console. writeline ("client:" + sr. readline (); string MSG = console. readline (); Sw. writeline (MSG); Sw. flush (); // refresh the stream} client. close (); // close the client }}}

2. client code (tcpclient)

Using system; using system. collections. generic; using system. LINQ; using system. text; using system. threading. tasks; // Add a new namespace using system. net; using system. net. sockets; using system. io; // stream streamreadernamespace tcpclient {class program {static void main (string [] ARGs) {// parse converts string to IP Address type IPaddress myip = IPaddress. parse ("127.0.0.1"); // construct a tcpclient Class Object, tcp client tcpclient client = new tcpclient (); // connect to the TCP Server Client. connect (myip, 6688); console. writeline ("the server is connected... enter the dialog content... "); // create a network stream and obtain the networkstream stream = client of the data stream. getstream (); // read data stream object streamreader sr = new streamreader (Stream); // write data stream object streamwriter Sw = new streamwriter (Stream); While (true) {string MSG = console. readline (); Sw. writeline (MSG); Sw. flush (); // refresh the stream console. writeline ("server:" + sr. readline ();} client. close (); console. read ();}}}

3. Program demonstration

Run the server (tcpserver) code first, and it will display "waiting for a connection... ". run the client (tcpclient) code again. After running, the server displays "the client is connected... ", the client displays" the server is connected... enter the dialog content ". then, enter the chat content in the client and server in sequence, and the other party will display the transmitted content to implement TCP chat calls. Enter "Hello! I am a client. "response.

More calls are as follows:

Conclusion: This is an article that I think is well written. It is more practical. More importantly, I can write the above Code to run it. We only need a slight interface design to implement an interactive network chat room. it involves the knowledge of TCP programming in C # network programming. Recently, Socket socket communication has always failed, and many blogs have downloaded many programs, but none of them are satisfied. I hope that the above article will help you with the mistakes and shortcomings. Sorry!

Finally, I would like to thank Liu lixia for his book "C # example development Daquan" and Zhou cunjie's "C # network programming instance tutorial". The above knowledge points and codes are based on the knowledge of their predecessors, I hope you can take a look at these two good C # books.

(By: eastmount 2013-7-20)

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.