C # simple chat program

Source: Internet
Author: User
Tags getstream sendmsg

Suppose there are server programs, ChatServer and client programs ChatClient. the client can send messages to the server. step 1. start listen on the server, 2. then the client connect.3. the client sends the message as long as the server starts listen, and the client also connects. after the connection is established. it is convenient to accept the sent information. You only need to use writer and reader to operate on the NetworkStream server ChatServer to create a WinForm page reference namespace: using System. net. sockets; using System. net; using System. IO; using System. threading; public class ChatServer {private int port = 54321; // port number private IPAddress ip = IPAddress. pa Rse ("10.20.30.40"); // ip address private TcpListener tcpListener = null; private TcpClient tcpClient = null; private NetworkStream networkStream = null; private BinaryReader reader; // private BinaryWriter writer; private string getInfo = string. empty; // start listening to private void btnStartListen_Click (object sender, EventArgs e) {tcpListener = new TcpListener (ip, port); tcpListener. start (); // Start listening to Thread acceptClientMs GThread = new Thread (AcceptClientMsg); // run a Thread to process the information sent from the client acceptClientMsgThread. start () ;}// process the information sent from the client private void AcceptClientMsg () {tcpClient = tcpListener. acceptTcpClient (); if (tcpClient! = Null) {networkStream = tcpClient. getStream (); reader = new BinaryReader (networkStream); while (true) {getInfo + = reader. readString (); // read the information sent from the client} // if you want to display the information, you can display the entire button (of course, the best way is to use some threads) // click the button to display txtShowClientMsg. text = getInfo; // if the server wants to send messages to the client, you can send the entire button. then add the following code // string sendMsg = txtSendMsge. text; // writer = new BinaryWriter (networkStream); // writer. write (sendMsg);} client ChatClient using System. net. sockets; using System. net; using System. IO; using System. threading; public class ChatClient {private int port = 54321; private IPAddress ip = IPAddress. parse ("10.20.30.40"); private TcpClient tcpClient = null; private NetworkStream networkStream = null; // private BinaryReader reader; private BinaryWriter writer; // connect serverprivate void btnStartConnect_Click (object sender, eventArgs e) {tcpClient = new TcpClient (); tcpClient. connect (ip, port); networkStream = tcpClient. getStream () ;}// send message private void btnStartConnect_Click (object sender, EventArgs e) {string sendMsg = txtSendMsg. text; writer = new BinaryWriter (networkStream); writer. write (sendMsg); // send information} // if you want to accept the message from the server. // reader = new BinaryReader (networkStream); // string getInfo = reader. readString ();}

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.