udp广播----------如何同时传播文字和图片?

lucbesson 2005-03-31 02:56:33
rt

CILENT 和 SERVER 都使用RICHTEXTBOX 控件。
...全文
440 20 打赏 收藏 转发到动态 举报
写回复
用AI写文章
20 条回复
切换为时间正序
请发表友善的回复…
发表回复
milkbb 2005-04-07
  • 打赏
  • 举报
回复
这么快就结了?我昨天培训去了,很无奈啊。
我做了一个,很简单,运行良好,帖下面了。
有一点要注意的,UPD包是限大小的,包太大是发不出去的,好象是4096字节,大的要分包发,我的程序里没有分包。图片用COPY&PASTE搞。


using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Net.Sockets;
using System.Net;
using System.Threading;
using System.IO;

namespace RTFMessenger
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class mainForm : System.Windows.Forms.Form
{
private System.Windows.Forms.RichTextBox rtfSend;
private System.Windows.Forms.Splitter splitter;
private System.Windows.Forms.RichTextBox rtfReceive;
private System.Windows.Forms.Button btnSend;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
public mainForm()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

//
// TODO: Add any constructor code after InitializeComponent call
//
thread = new Thread(new ThreadStart(Listen));
thread.Start();
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.rtfSend = new System.Windows.Forms.RichTextBox();
this.splitter = new System.Windows.Forms.Splitter();
this.rtfReceive = new System.Windows.Forms.RichTextBox();
this.btnSend = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// rtfSend
//
this.rtfSend.Dock = System.Windows.Forms.DockStyle.Top;
this.rtfSend.Location = new System.Drawing.Point(0, 0);
this.rtfSend.Name = "rtfSend";
this.rtfSend.Size = new System.Drawing.Size(292, 96);
this.rtfSend.TabIndex = 0;
this.rtfSend.Text = "";
//
// splitter
//
this.splitter.Dock = System.Windows.Forms.DockStyle.Top;
this.splitter.Location = new System.Drawing.Point(0, 96);
this.splitter.Name = "splitter";
this.splitter.Size = new System.Drawing.Size(292, 3);
this.splitter.TabIndex = 1;
this.splitter.TabStop = false;
//
// rtfReceive
//
this.rtfReceive.Dock = System.Windows.Forms.DockStyle.Top;
this.rtfReceive.Location = new System.Drawing.Point(0, 99);
this.rtfReceive.Name = "rtfReceive";
this.rtfReceive.Size = new System.Drawing.Size(292, 133);
this.rtfReceive.TabIndex = 2;
this.rtfReceive.Text = "";
//
// btnSend
//
this.btnSend.Location = new System.Drawing.Point(216, 240);
this.btnSend.Name = "btnSend";
this.btnSend.TabIndex = 3;
this.btnSend.Text = "Send";
this.btnSend.Click += new System.EventHandler(this.btnSend_Click);
//
// mainForm
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.Add(this.btnSend);
this.Controls.Add(this.rtfReceive);
this.Controls.Add(this.splitter);
this.Controls.Add(this.rtfSend);
this.Name = "mainForm";
this.Text = "mainForm";
this.Closing += new System.ComponentModel.CancelEventHandler(this.mainForm_Closing);
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new mainForm());
}

//****************************************************************************
// 只需要看以下的代码,另外上面构造的时候开了一个新线程

private Thread thread;

private void Listen()
{
int port = 39763;

UdpClient listener = new UdpClient(port);

while(true)
{
try
{
IPEndPoint ipep = null;

byte[] bytes = listener.Receive(ref ipep);
this.Invoke(new dele(load), new object[]{bytes});
}
catch(Exception)
{
break;
}
}

listener.Close ();
}

private delegate void dele(byte[] bytes);

private void load(byte[] bytes)
{
MemoryStream stream = new MemoryStream(bytes);
rtfReceive.LoadFile(stream,RichTextBoxStreamType.RichText);
stream.Close();
}
private void Send()
{
int port = 39763;
UdpClient sender = new UdpClient();
IPEndPoint ipep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), port);

MemoryStream stream = new MemoryStream();
rtfSend.SaveFile(stream,RichTextBoxStreamType.RichText);
byte[] bytes = stream.GetBuffer();
sender.Send(bytes, bytes.Length, ipep);
stream.Close();
}

private void btnSend_Click(object sender, System.EventArgs e)
{
Send();
}

private void mainForm_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
thread.Abort();
}
}
}
lucbesson 2005-04-07
  • 打赏
  • 举报
回复
无奈的结帖啦
xhm1027 2005-04-06
  • 打赏
  • 举报
回复
up
zj369 2005-04-06
  • 打赏
  • 举报
回复
我顶
lucbesson 2005-04-05
  • 打赏
  • 举报
回复
winform
henryfan1 2005-04-05
  • 打赏
  • 举报
回复
你定义传协议,简单的文字加图片信息可以定义如下:
<Message>sfsddfsd<IMG>SDFSD</IMG>SFS<IMG>SDFS</IMG></Message>;
byte是可以转成字符的,分解后把IMG部分转成byte用Image类加载就可以还原图象.
athossmth 2005-04-05
  • 打赏
  • 举报
回复
winform or webform?
lucbesson 2005-04-05
  • 打赏
  • 举报
回复
{\rtf1\ansi\ansicpg936\deff0\deflang1033\deflangfe2052{\fonttbl{\f0\fnil\fcharset134 \'cb\'ce\'cc\'e5;}}
\viewkind4\uc1\pard\lang2052\f0\fs18{\pict\wmetafile8\picw5396\pich3888\picwgoal3059\pichgoal2204
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
}\par
}
例如这样一个图片的rtf代码,怎么样把它再变成图片 ?
----------------------------------------------------

楼上那位老师的定义传协议,有朋友能清楚的讲述一下吗 ?


谢谢
stepman 2005-04-04
  • 打赏
  • 举报
回复
mark
pc_csharp 2005-04-04
  • 打赏
  • 举报
回复
QQ里把函数告诉你了.

但是把图片放到stream里面怎么样让richtextbox显示我不知道.
lovelxj 2005-04-04
  • 打赏
  • 举报
回复
不懂这个机制 楼猪 要不介绍一下吧
你这个的用途
cnming 2005-04-04
  • 打赏
  • 举报
回复
肯定要分开传,建立排队机制

要传输这些内容,首先要把它们转换为byte数组

真个题太大,不好一一说,你还是自己去学习,当中有疑问的再问

如果有耐心,建议去下载一个myicq的源码来看看,这是开放源码的
新鲜鱼排 2005-04-04
  • 打赏
  • 举报
回复
不是很懂,帮顶
lucbesson 2005-04-04
  • 打赏
  • 举报
回复
我的意思是:client 的richtextbox怎么样发送图片+文字的内容,server如何接收 ?

继续收集意见

_-_-_-_- 2005-04-03
  • 打赏
  • 举报
回复
????
沈逸 2005-04-03
  • 打赏
  • 举报
回复
不好同时
任何东西都是 要先来后到的
zhupan 2005-04-03
  • 打赏
  • 举报
回复
什么意思呀
lucbesson 2005-03-31
  • 打赏
  • 举报
回复
chenla
lucbesson 2005-03-31
  • 打赏
  • 举报
回复
up
lucbesson 2005-03-31
  • 打赏
  • 举报
回复
up

110,539

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧