Use TCP communication in C #

Source: Internet
Author: User
Tags getstream

TCP communication requires both parties to communicate online. Therefore, the server must be started to listen before the client can obtain the connection. The server code:

Static void Main (string [] args) {TcpClient client = null; NetworkStream = null; byte [] buffer = null; string receiveString = null; IPAddress localIP = IPAddress. parse ("127.0.0.1"); int localPort = 11000; TcpListener listener = new TcpListener (localIP, localPort); // instantiate Listener with a local IP address and port. start (); // Start listening while (true) {client = listener. acceptTcpClient (); // accept a Client buffer = new byte [client. receiveBufferSize]; stream = client. getStream (); // get the network stream. read (buffer, 0, buffer. length); // read the data stream in the network stream. close (); // Close the stream client. close (); // Close Client receiveString = Encoding. default. getString (buffer ). trim ('\ 0'); // converts it to a string Console. writeLine (receiveString );}}

The client can be correctly connected only after the server enables the listener. Therefore, the server must always enable the listener. Each time the client sends data, it must first establish a connection with the server. After the connection is established, data is sent. Client code:

Static void Main (string [] args) {string sendString = null; // The string byte [] sendData = null; // The byte array TcpClient client to be sent = null; // TcpClient instance NetworkStream = null; // network stream IPAddress remoteIP = IPAddress. parse ("127.0.0.1"); // Remote Host IP int remotePort = 11000; // remote host port while (true) // endless loop {sendString = Console. readLine (); // obtain the string sendData = Encoding to be sent. default. getBytes (sendString); // get the byte array to be sent client = New TcpClient (); // instantiate TcpClient try {client. connect (remoteIP, remotePort); // Connect to a remote host} catch (System. exception ex) {Console. writeLine ("connection timeout, no response from the server! "); // Connection failure Console. readKey (); return;} stream = client. getStream (); // get the network stream. write (sendData, 0, sendData. length); // write data to the network stream. close (); // Close the network stream client. close (); // Close the client }}


Related Article

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.