I checked the materials and practiced socket programming myself. Today I want to implement point-to-point chat. The principle is almost the same. It can be divided into two operations: server and client.
Server:
1) create a tcplistener object and initialize
Private tcplistener;
Private tcpclient client;
Private networkstream netstream;
Private streamreader streamr;
Private streamwriter streamw;
Private thread mainthread;
Private thread;
Private void start ()
{
Tcplistener = new tcplistener(ipaddress.parse(this.txt IP. Text), 8080); // enter the IP address and port
Tcplistener. Start (); // start listening
Client = tcplistener. accepttcpclient (); // you know that the client is connected.
Tcplistener. Stop (); // disable the listener.
Netstream = client. getstream (); // gets the data stream sent from the client.
Streamr = new streamreader (netstream, encoding. Default );
Streamw = new streamwriter (netstream, encoding. Default );
Thread = new thread (New threadstart (reading ));
Thread. Start ();
}
2) read the messages sent from the client.
Private void reading () // read content
{
For (INT I = 0; I <4; I ++) // The test shows that mysread. Readline () reads the content four times after each chat is sent ,// I am not sure the specific reason for finding this information. If you want to know this, please let me know.
{// The first time is the header flag information line, and the second time reads the chat content,
If (I = 0) // The content read for the last two times is empty. The display and dyeing functions are designed accordingly.
{
String youmsg = "\ r \ n" + Sr. Readline ();
Rtxtlog. appendtext (youmsg );
}
Else
{
String youmsg = "\ r \ n" + Sr. Readline ();
Rtxtlog. appendtext (youmsg );
}
}
}
3) send a message button _ event to the client
Private void btnsend_click (Object sender, eventargs E)
{
Streamw. writeline (New datetime (). tow.datestring () + "Message from server ");
Streamcompreswriteline(this.txt content. Text );
Streamw. Flush ();
}
Client:
1) establish a connection
Client = new tcpclient ();
Client. Connect (IPaddress. parse ("127.0.0.1"), 8080 );
Netstream = client. getstream (); // gets the data stream from the server.
Sr = new streamreader (netstream, encoding. Default );
Sw = new streamwriter (netstream, encoding. Default );
Mythread = new thread (New threadstart (reading ));
Mythread. Start ();
MessageBox. Show ("connection successful ");
2) Data Reading Method
Private void reading ()
{
For (INT I = 0; I <4; I ++) // The test shows that mysread. Readline () reads the content four times after each chat is sent,
{// The first time is the header flag information line, and the second time reads the chat content,
If (I = 0) // The content read for the last two times is empty. The display and dyeing functions are designed accordingly.
{
String youmsg = "\ r \ n" + Sr. Readline ();
Rtxtlog. appendtext (youmsg );
}
Else
{
String youmsg = "\ r \ n" + Sr. Readline ();
Rtxtlog. appendtext (youmsg );
Rtxtlog. Select (rtxtlog. textlength-youmsg. Length + 1, youmsg. Length );
Rtxtlog. selectioncolor = color. Red;
}
}
}
3) send data
Private void btnsend_click (Object sender, eventargs E)
{
Sw. writeline ("time:" + new datetime (). tow.datestring () + "Message from client ");
Sw.writeline(this.txt info. Text );
Sw. Flush ();
// Client. Sen
}
Enable the server before testing.ProgramEnter 127.0.0.1 as the IP address, and then enable the client program. To test. /Files/hantianwei/tcpapptest.rar