电脑桌面
添加小米粒文库到电脑桌面
安装后可以在桌面快捷访问

CTCPClient应用一个简单的消息发送和接收

CTCPClient应用一个简单的消息发送和接收_第1页
1/6
CTCPClient应用一个简单的消息发送和接收_第2页
2/6
CTCPClient应用一个简单的消息发送和接收_第3页
3/6
C#TCPClient应用-一个简单的消息发送和接收 TcpSend 窗口用于发送消息,另外写一个用于接收消息的应用程序,消息接受到以后,必须要关闭接收消息的窗口,才能在接收新的消息,不知道怎么能解决这个问题。 源代码: 发送消息的窗口代码 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; namespaceTCPSocket { public partial class FrmTcpSend : Form { public FrmTcpSend() { InitializeComponent(); } private void buttonSendFile_Click(object sender, EventArgs e) { // TcpClienttcpClient = new TcpClient(textBoxHostName.Text, Int32.Parse(textBoxPort.Text)); TcpClienttcpClient = new TcpClient(); tcpClient.Connect(IPAddress.Parse(textBoxHostName.Text), Int32.Parse(textBoxPort.Text)); NetworkStream ns = tcpClient.GetStream(); FileStreamfs = 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) { TcpClienttcpClient = 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("不能写入数据流", "终止", 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(); } } } 接收消息的窗口代码 using System; using System.Collections.Generic; using System.ComponentModel;...

1、当您付费下载文档后,您只拥有了使用权限,并不意味着购买了版权,文档只能用于自身使用,不得用于其他商业用途(如 [转卖]进行直接盈利或[编辑后售卖]进行间接盈利)。
2、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。
3、如文档内容存在违规,或者侵犯商业秘密、侵犯著作权等,请点击“违规举报”。

碎片内容

CTCPClient应用一个简单的消息发送和接收

确认删除?
VIP
微信客服
  • 扫码咨询
会员Q群
  • 会员专属群点击这里加入QQ群
客服邮箱
回到顶部