C#tcpclient application-A simple message to send and receive

Source: Internet
Author: User
Tags getstream

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

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.