MSMQ的应用测试(传输文本,附件)
根据学习:
http://www.cnblogs.com/rickie/archive/2004/11/16/64345.html
http://rickie.cnblogs.com/archive/2004/11/17/64712.aspx
(注意,需要在计算机管理里面设置 服务和应用程序/消息队列/专用队列/建立MSMQDemo队列)
进行消息传输(这里使用的是MSMQ,不是IBMMQ),
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Messaging;
using System.Xml;
using System.IO;

namespace S1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
// Open queue
System.Messaging.MessageQueue queue = new System.Messaging.MessageQueue("jhtchina\\Private$\\MSMQDemo");
// Create message
System.Messaging.Message message = new System.Messaging.Message();
message.Body = txtMessage.Text.Trim();
message.Formatter = new System.Messaging.XmlMessageFormatter(new Type[] { typeof(string) });
// Put message into queue
try
{
queue.Send(message);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
}

private void button2_Click(object sender, EventArgs e)
{
// Open queue
System.Messaging.MessageQueue queue = new System.Messaging.MessageQueue("jhtchina\\Private$\\MSMQDemo");
// Receive message, 同步的Receive方法阻塞当前执行线程,直到一个message可以得到
try
{
System.Messaging.Message message = queue.Receive();
message.Formatter = new System.Messaging.XmlMessageFormatter(new Type[] { typeof(string) });
txtReceiveMessage.Text = message.Body.ToString();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
return;
}



}

private void button3_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.InitialDirectory = "c:\\";
//openFileDialog1.Filter = "jpg files (*.jpg)";
openFileDialog1.Multiselect = false;
openFileDialog1.RestoreDirectory = true;
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
txtOpenFile.Text = openFileDialog1.FileName;

}


// Open queue
System.Messaging.MessageQueue queue = new System.Messaging.MessageQueue("jhtchina\\Private$\\MSMQDemo");
// Create message
System.Messaging.Message message = new System.Messaging.Message();
message.Body = Tran_XML(txtOpenFile.Text);
message.Formatter = new System.Messaging.XmlMessageFormatter(new Type[] { typeof(string) });
// Put message into queue
try
{
queue.Send(message);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}

}

private string Tran_XML(string strFilename)
{
try
{
XmlDocument myXmlDoc = new XmlDocument();
myXmlDoc.LoadXml("<picture><name>picture</name></picture>");
XmlElement elem = myXmlDoc.CreateElement("image");
// 打开图片文件,利用该图片构造一个文件流
FileStream fs = new FileStream(strFilename, FileMode.Open);
// 使用文件流构造一个二进制读取器将基元数据读作二进制值
BinaryReader br = new BinaryReader(fs);
byte[] imageBuffer = new byte[br.BaseStream.Length];
br.Read(imageBuffer, 0, Convert.ToInt32(br.BaseStream.Length));
string textString = System.Convert.ToBase64String(imageBuffer);
fs.Close();
br.Close();
XmlText text = myXmlDoc.CreateTextNode(textString);
myXmlDoc.DocumentElement.AppendChild(elem);
myXmlDoc.DocumentElement.LastChild.AppendChild(text);
//myXmlDoc.Save("e:\\docSave.xml");
return myXmlDoc.OuterXml.ToString();
//MessageBox.Show("读写结束!");
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
return "error";
}

}

private string ReadMessageOfJpg()
{
System.Messaging.MessageQueue queue = new System.Messaging.MessageQueue("jhtchina\\Private$\\MSMQDemo");
// Receive message, 同步的Receive方法阻塞当前执行线程,直到一个message可以得到
try
{
System.Messaging.Message message = queue.Receive();
message.Formatter = new System.Messaging.XmlMessageFormatter(new Type[] { typeof(string) });
//txtReceiveMessage.Text = message.Body.ToString();
return message.Body.ToString();

}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
return "";
}

}

private void button4_Click(object sender, EventArgs e)
{

try
{
int readByte = 0;
int bytesToRead = 1044;
string strJpg = ReadMessageOfJpg().Trim();


XmlTextReader xmlTxtRd = new XmlTextReader(new StringReader(strJpg));

FileStream fs = new FileStream("e:\\002.jpg", FileMode.Create);
BinaryWriter bw = new BinaryWriter(fs);
byte[] base64buffer = new byte[bytesToRead];
while (xmlTxtRd.Read())
{
if (xmlTxtRd.NodeType == XmlNodeType.Element && xmlTxtRd.Name == "image")
{
do
{
readByte = xmlTxtRd.ReadBase64(base64buffer, 0, bytesToRead);
bw.Write(base64buffer, 0, readByte);
}
while (bytesToRead <= readByte);
}
}
bw.Flush();
bw.Close();
fs.Close();
xmlTxtRd.Close();
MessageBox.Show("读写结束!");



}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}

}




}
}
http://www.cnblogs.com/rickie/archive/2004/11/16/64345.html
http://rickie.cnblogs.com/archive/2004/11/17/64712.aspx
(注意,需要在计算机管理里面设置 服务和应用程序/消息队列/专用队列/建立MSMQDemo队列)
进行消息传输(这里使用的是MSMQ,不是IBMMQ),
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Messaging;
using System.Xml;
using System.IO;
namespace S1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
// Open queue
System.Messaging.MessageQueue queue = new System.Messaging.MessageQueue("jhtchina\\Private$\\MSMQDemo");
// Create message
System.Messaging.Message message = new System.Messaging.Message();
message.Body = txtMessage.Text.Trim();
message.Formatter = new System.Messaging.XmlMessageFormatter(new Type[] { typeof(string) });
// Put message into queue
try
{
queue.Send(message);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
}
private void button2_Click(object sender, EventArgs e)
{
// Open queue
System.Messaging.MessageQueue queue = new System.Messaging.MessageQueue("jhtchina\\Private$\\MSMQDemo");
// Receive message, 同步的Receive方法阻塞当前执行线程,直到一个message可以得到
try
{
System.Messaging.Message message = queue.Receive();
message.Formatter = new System.Messaging.XmlMessageFormatter(new Type[] { typeof(string) });
txtReceiveMessage.Text = message.Body.ToString();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
return;
}


}
private void button3_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.InitialDirectory = "c:\\";
//openFileDialog1.Filter = "jpg files (*.jpg)";
openFileDialog1.Multiselect = false;
openFileDialog1.RestoreDirectory = true;
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
txtOpenFile.Text = openFileDialog1.FileName;

}

// Open queue
System.Messaging.MessageQueue queue = new System.Messaging.MessageQueue("jhtchina\\Private$\\MSMQDemo");
// Create message
System.Messaging.Message message = new System.Messaging.Message();
message.Body = Tran_XML(txtOpenFile.Text);
message.Formatter = new System.Messaging.XmlMessageFormatter(new Type[] { typeof(string) });
// Put message into queue
try
{
queue.Send(message);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
}
private string Tran_XML(string strFilename)
{
try
{
XmlDocument myXmlDoc = new XmlDocument();
myXmlDoc.LoadXml("<picture><name>picture</name></picture>");
XmlElement elem = myXmlDoc.CreateElement("image");
// 打开图片文件,利用该图片构造一个文件流
FileStream fs = new FileStream(strFilename, FileMode.Open);
// 使用文件流构造一个二进制读取器将基元数据读作二进制值
BinaryReader br = new BinaryReader(fs);
byte[] imageBuffer = new byte[br.BaseStream.Length];
br.Read(imageBuffer, 0, Convert.ToInt32(br.BaseStream.Length));
string textString = System.Convert.ToBase64String(imageBuffer);
fs.Close();
br.Close();
XmlText text = myXmlDoc.CreateTextNode(textString);
myXmlDoc.DocumentElement.AppendChild(elem);
myXmlDoc.DocumentElement.LastChild.AppendChild(text);
//myXmlDoc.Save("e:\\docSave.xml");
return myXmlDoc.OuterXml.ToString();
//MessageBox.Show("读写结束!");
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
return "error";
}
}
private string ReadMessageOfJpg()
{
System.Messaging.MessageQueue queue = new System.Messaging.MessageQueue("jhtchina\\Private$\\MSMQDemo");
// Receive message, 同步的Receive方法阻塞当前执行线程,直到一个message可以得到
try
{
System.Messaging.Message message = queue.Receive();
message.Formatter = new System.Messaging.XmlMessageFormatter(new Type[] { typeof(string) });
//txtReceiveMessage.Text = message.Body.ToString();
return message.Body.ToString();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
return "";
}
}
private void button4_Click(object sender, EventArgs e)
{
try
{
int readByte = 0;
int bytesToRead = 1044;
string strJpg = ReadMessageOfJpg().Trim();

XmlTextReader xmlTxtRd = new XmlTextReader(new StringReader(strJpg));
FileStream fs = new FileStream("e:\\002.jpg", FileMode.Create);
BinaryWriter bw = new BinaryWriter(fs);
byte[] base64buffer = new byte[bytesToRead];
while (xmlTxtRd.Read())
{
if (xmlTxtRd.NodeType == XmlNodeType.Element && xmlTxtRd.Name == "image")
{
do
{
readByte = xmlTxtRd.ReadBase64(base64buffer, 0, bytesToRead);
bw.Write(base64buffer, 0, readByte);
}
while (bytesToRead <= readByte);
}
}
bw.Flush();
bw.Close();
fs.Close();
xmlTxtRd.Close();
MessageBox.Show("读写结束!");


}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}



}
}
注意:如果跨网段传输需要:
1 FORMATNAME:DIRECT=TCP:1.1.1.1\PRIVATE$\test1
2 打开1801端口


浙公网安备 33010602011771号