本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《 阿里云开发者社区用户服务协议 》和 《 阿里云开发者社区知识产权保护指引 》。如果您发现本社区中有涉嫌抄袭的内容,填写 侵权投诉表单 进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

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;

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);
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;
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 thread = new Thread(new ThreadStart(Listen));

Thread thread = new Thread(new ThreadStart(SocketListen));

thread.Start();

IPAddress ipAddress = IPAddress.Any; //IPAddress.Parse("172.16.102.11");

this.Text = ipAddress.ToString()+"正在监听...";

protected delegate void UpdateDisplayDelegate(string text);

public void Listen()
IPAddress ipAddress = IPAddress.Any; //IPAddress.Parse("172.16.102.11");

TcpListener tcpListener = new TcpListener(ipAddress,9999);
tcpListener.Start();

TcpClient 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;

本文转自tiasys博客园博客,原文链接:http://www.cnblogs.com/tiasys/archive/2009/11/24/1609286.html,如需转载请自行联系原作者

和生产者的消息确认机制不同,因为消息接收本来就是在监听消息,符合条件的消息就会消费下来。 所以,消息接收的确认机制主要存在三种模式 1.自动确认 这也是默认的消息确认情况。AcknowledgeMode.NONE,RabbitMQ成功将消息发出(即将消息成功写入TCP Socket)中立即 认为本次投递已经被正确处理,不管消费者端是否成功处理本次投递 当自动应答等于true的时候,表示当消费者一收到消息就表示消费者收到了消息,消费者收到了消息就会立即从队列中删除 2.不确认(不介绍) 3.手动确认(多数选择的模式) 十六分的音符