The Tcpsend window is used to send a message, and another application to receive the message, the message is accepted later, you must close the window to receive the message, in order to receive new messages, do not know how to solve the problem.
Source:
The window code to send the message
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 TcpClient = new TcpClient (Textboxhostname.text, Int32.Parse (Textboxport.text));
TcpClient 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 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 stream", "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 ();
}
}
}
C#tcpclient application-A simple message to send and receive