c# tcp显示服务器图片,C# TCP通迅原码(服务器与客户端相互发送文本与图片)

【实例简介】

【实例截图】

143a9dd9c5ed8d5993d914b489b58de0.png

29ae200181e9a677ed04a1c8cd2ad78d.png

【核心代码】

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

using System.IO;

using System.Runtime.Serialization.Formatters.Binary;

using TCPLibrary;

using TCPLibrary.Abstracts;

using TCPLibrary.DefaultImplements;

namespace TCPServer

{

public partial class Form1 : Form

{

BaseServerSocket _server;

public Form1()

{

InitializeComponent();

}

List list = new List();

private void Form1_Load(object sender, EventArgs e)

{

_server = new BaseServerSocket();

_server.Connected = new ConnectedEventHandler(_server_Connected);

_server.DisConnected = new DisConnectedEventHandler(_server_DisConnected);

_server.MessageReceived = new MessageReceivedEventHandler(_server_MessageReceived);

_server.StartAccept(9100);

textBox1.AppendText("服务器启动,监听端口 " 9000 "...\r\n");

}

///

/// 接受消息

///

///

///

void _server_MessageReceived(ZProxySocket proxySocket, ZMessage message)

{

this.Invoke((Action)(delegate()

{

BaseMessage msg = message as BaseMessage;

if (msg.MsgType == 1)  //文本

{

textBox1.AppendText(proxySocket.RemoteIP ":" proxySocket.RemotePort "发送一条文本消息:\r\n");

textBox1.AppendText(Encoding.Unicode.GetString(msg.MsgContent) "\r\n");

}

if (msg.MsgType == 2)  //图片

{

textBox1.AppendText(proxySocket.RemoteIP ":" proxySocket.RemotePort "发送一条图片消息:\r\n");

BinaryFormatter bf = new BinaryFormatter();

pictureBox1.Image = bf.Deserialize(new MemoryStream(msg.MsgContent)) as Bitmap;  //将内容反序列化为bitmap

}

}));

}

///

/// 断开连接

///

///

void _server_DisConnected(ZProxySocket proxySocket)

{

this.Invoke((Action)(delegate()

{

list.Remove(proxySocket);

textBox1.AppendText(proxySocket.RemoteIP ":" proxySocket.RemotePort "断开服务器\r\n");

}));

}

///

/// 建立连接

///

///

void _server_Connected(ZProxySocket proxySocket)

{

this.Invoke((Action)(delegate()

{

list.Add(proxySocket);

textBox1.AppendText(proxySocket.RemoteIP ":" proxySocket.RemotePort "连接服务器\r\n");

}));

}

///

/// 发送文本

///

///

///

private void button1_Click(object sender, EventArgs e)

{

foreach (ZProxySocket proxy in list)

{

proxy.SendMessage(new BaseMessage(1,Encoding.Unicode.GetBytes(textBox2.Text)));  //发送BaseMessage消息

}

}

///

/// 发送图片(可序列化对象)

///

///

///

private void button2_Click(object sender, EventArgs e)

{

using (OpenFileDialog ofd = new OpenFileDialog())

{

ofd.Filter = "图片文件|*.jpg;*.jpeg";

if (ofd.ShowDialog() == DialogResult.OK)

{

BinaryFormatter bf = new BinaryFormatter();

using (MemoryStream ms = new MemoryStream())

{

bf.Serialize(ms, Image.FromFile(ofd.FileName));

foreach (ZProxySocket proxy in list)

{

proxy.SendMessage(new BaseMessage(2, ms.ToArray()));  //发送BaseMessage消息

}

}

}

}

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值