C # tcpclient application-a simple message sending and receiving

Source: Internet
Author: User
Tags getstream

 

The tcpsend window is used to send messages, and an application is written to receive messages.ProgramAfter a message is received, you must close the message receiving window to receive new messages.

Source code:

Message sending windowCode

 

Using system;
Using system. Collections. Generic;
Using system. componentmodel;
Using system. Data;
Using system. drawing;
Using system. text;
Using system. Windows. forms;
Using system. net;
Using system. net. Sockets;
Using system. IO;

Namespace tcpsocket
{
Public partial class frmtcpsend: Form
{
Public frmtcpsend ()
{
Initializecomponent ();
}

Private void buttonsendfile_click (Object sender, eventargs E)
{
// Tcpclient = new tcpclient (textboxhostname. Text, int32.parse (textboxport. Text ));
Tcpclient = new tcpclient ();
Tcpclient. Connect (IPaddress. parse (textboxhostname. Text), int32.parse (textboxport. Text ));

Networkstream NS = tcpclient. getstream ();

Filestream FS = file. Open (".. \ frmtcpsend. cs", filemode. Open );

Int DATA = FS. readbyte ();

While (Data! =-1)
{
NS. writebyte (byte) data );
Data = FS. readbyte ();
}

FS. Close ();
NS. Close ();
Tcpclient. Close ();

}

Private void buttonsendmessage_click (Object sender, eventargs E)
{
Tcpclient = new tcpclient ();
Tcpclient. Connect (IPaddress. parse (textboxhostname. Text), int32.parse (textboxport. Text ));

Networkstream NS = tcpclient. getstream ();

If (NS. canwrite)
{
Byte [] sendbytes = encoding. utf8.getbytes (textboxmessage. Text );
NS. Write (sendbytes, 0, sendbytes. Length );
}
Else
{
MessageBox. Show ("cannot write data streams", "Terminate", messageboxbuttons. OK, messageboxicon. Stop );
// Console. writeline ("You cannot write data to this stream .");
Tcpclient. Close ();

// Closing the tcpclient instance does not close the network stream.
NS. Close ();
Return;
}

NS. Close ();
Tcpclient. Close ();

}
}
}

 

 

Message Receiving Window code

 

 

Using system;
Using system. Collections. Generic;
Using system. componentmodel;
Using system. Data;
Using system. drawing;
Using system. text;
Using system. Windows. forms;
Using system. net;
Using system. net. Sockets;
Using system. IO;
Using system. Threading;

Namespace tcpreceive
{
Public partial class form1: Form
{
Public form1 ()
{
Initializecomponent ();

// Thread = new thread (New threadstart (Listen ));

Thread thread = new thread (New threadstart (socketlisten ));

Thread. Start ();

IPaddress = IPaddress. Any; // IPaddress. parse ("172.16.102.11 ");

This. Text = IPaddress. tostring () + "listening ...";

}

Protected Delegate void updatedisplaydelegate (string text );

Public void listen ()
{
IPaddress = IPaddress. Any; // IPaddress. parse ("172.16.102.11 ");



Tcplistener = new tcplistener (IPaddress, 9999 );
Tcplistener. Start ();

Tcpclient = tcplistener. accepttcpclient ();


Networkstream NS = tcpclient. getstream ();

Streamreader sr = new streamreader (NS );

String result = Sr. readtoend ();

Invoke (New updatedisplaydelegate (updatedisplay), new object [] {result });

Tcpclient. Close ();
Tcplistener. Stop ();
}

Public void socketlisten ()
{
Socket listener = new socket (addressfamily. InterNetwork, sockettype. Stream, protocoltype. TCP );

Listener. BIND (New ipendpoint (IPaddress. Any, 9999 ));

Listener. Listen (0 );

Socket socket = listener. Accept ();
Stream netstream = new networkstream (socket );
Streamreader reader = new streamreader (netstream );

String result = reader. readtoend ();
Invoke (New updatedisplaydelegate (updatedisplay), new object [] {result });

Socket. Close ();
Listener. Close ();

}

Public void updatedisplay (string text)
{
Richtextbox1.text = text;
}

}
}

 

 

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.