首页

C# winform Socket通讯实例

c#

2021-5-11

下载前请先看说明!!

此程序是C# winform 用Socket编写。包含服务端和客户端代码。

程序界面,我偷懒了,直接把我前面写的Web页面的素材图片照搬过来,包括界面结构都仿照Web那个程序了。不过winform按照Web风格去写,真tm累。点击网站上的名字,可以看到前面写的web程序,以及形成这个winform程序的学习过程。

客户端:

服务端:

1、程序主要部分只是用了 Panel、Picturebox、Label、RicheditBox四个组件,滚动条、编辑框什么的都自定义了,应该比较容易看清楚。

2、服务端设定地址为192.168.0.103,端口设定为5188,客户端指向服务端也是这个地址,根据需要自行修改。

服务端:

//监听端口

private int port = 51888;

private TcpListener myListener;

ArrayList MessList = new ArrayList();

int MessCount = 0;





IPAddress[] addrIP = Dns.GetHostAddresses(“192.168.0.103”); //ip地址

localAddress = addrIP[0];

客户端:

//建立网络通讯

try

{

client = new TcpClient(“192.168.0.103”, 51888); //定义服务器端ip地址和端口,与服务器端定义要一致

TcpInfo.AppendText(“本机EndPoint:” client.Client.LocalEndPoint); //本机的ip地址和端口号

TcpInfo.AppendText(“与服务器建立连接成功”);

linkFlag = true;

}

catch

{

TcpInfo.AppendText(“与服务器连接失败”);

return;

}

3、程序运行时必须首先运行“服务端”的ChatServer.exe,点击“开始监听”后,再运行“客户端”的可执行文件,用户名随便起一个,密码随便填。服务端每个登录用户开1个线程,客户端开1个线程进行数据交互。

4、数据传输定义:

string[] sArray = getMess.Split(new char[6] { ‘[‘, ‘]’, ‘$’, ‘$’, ‘$’, ‘$’ });

//sArray[1] 发送信息用户的ip

//sArray[3] 发送消息的版块

//sArray[4] 发送消息的内容

//sArray[5] 用户名

//sArray[6] 时间

服务端据此进行消息派发,客户端根据返回的消息分类显示。

5、由于使用绘图功能,会产生屏幕的闪烁,所以用生成位图的方式处理,达到减小闪烁。可能会产生信息更新较慢或不连贯,因为在最右侧将全部数据动作写了一遍。同时,由于在线程上不能对原组件进行添加等操作,所以按照右侧内容变化来更新数据信息显示:

 private void TcpInfo_TextChanged(object sender, EventArgs e)

 {………}

绘图处理:

……

Bitmap localBitmap = new Bitmap(conter_panel.Width, conter_panel.Height);

 Graphics bitmapGraphics = Graphics.FromImage(localBitmap);

bitmapGraphics.Clear(BackColor);

bitmapGraphics.SmoothingMode = SmoothingMode.AntiAlias;

Draw(conter_panel.ClientRectangle, bitmapGraphics, 18, true, 0, Color.FromArgb(90, 143, 0), Color.FromArgb(90, 143, 0));

conter_panel.BackgroundImage = localBitmap;

conter_panel.Controls.Add(rc);

……

6、程序为了学习方便未使用数据库,所有数据交互都放在内存中进行。

7、程序显示英文不太正常,中文正常。

    只做了“频道”部分,其他原理相同,就没做了。其他主要就是消息的分类处理,原理都差不多了。像在线用户处理、统计等都是数据处理过程,结合数据库最方便。这个程序只是学习过程中的一个练习,只是一个练习。更多学习关注我的博客http://www.cnblogs.com/qiaoke/

资源下载此资源下载价格为3D币(VIP免费),请先
资源文件列表
C# winform Socket/客户端/.vs/Thedog/v14/.suo , 55296
C# winform Socket/客户端/App.config , 189
C# winform Socket/客户端/bin/Debug/Res/1.png , 16744
C# winform Socket/客户端/bin/Debug/Res/10.png , 16951
C# winform Socket/客户端/bin/Debug/Res/11.png , 17133
C# winform Socket/客户端/bin/Debug/Res/12.png , 17094
C# winform Socket/客户端/bin/Debug/Res/13.png , 16944
C# winform Socket/客户端/bin/Debug/Res/14.png , 16862
C# winform Socket/客户端/bin/Debug/Res/15.png , 16756
C# winform Socket/客户端/bin/Debug/Res/16.png , 16898
C# winform Socket/客户端/bin/Debug/Res/17.png , 16961
C# winform Socket/客户端/bin/Debug/Res/18.png , 17067
C# winform Socket/客户端/bin/Debug/Res/19.png , 16748
C# winform Socket/客户端/bin/Debug/Res/2.png , 16890
C# winform Socket/客户端/bin/Debug/Res/20.png , 16983
C# winform Socket/客户端/bin/Debug/Res/21.png , 16895
C# winform Socket/客户端/bin/Debug/Res/22.png , 17030
C# winform Socket/客户端/bin/Debug/Res/23.png , 17170
C# winform Socket/客户端/bin/Debug/Res/24.png , 16994
C# winform Socket/客户端/bin/Debug/Res/25.png , 17006
C# winform Socket/客户端/bin/Debug/Res/26.png , 17290
C# winform Socket/客户端/bin/Debug/Res/27.png , 16844
C# winform Socket/客户端/bin/Debug/Res/28.png , 17072
C# winform Socket/客户端/bin/Debug/Res/29.png , 17109
C# winform Socket/客户端/bin/Debug/Res/3.png , 17057
C# winform Socket/客户端/bin/Debug/Res/30.png , 17031
C# winform Socket/客户端/bin/Debug/Res/31.png , 17014
C# winform Socket/客户端/bin/Debug/Res/32.png , 17077
C# winform Socket/客户端/bin/Debug/Res/33.png , 16964
C# winform Socket/客户端/bin/Debug/Res/34.png , 16944
C# winform Socket/客户端/bin/Debug/Res/35.png , 17036
C# winform Socket/客户端/bin/Debug/Res/4.png , 17127
C# winform Socket/客户端/bin/Debug/Res/5.png , 16938
C# winform Socket/客户端/bin/Debug/Res/6.png , 16884
C# winform Socket/客户端/bin/Debug/Res/7.png , 17168
C# winform Socket/客户端/bin/Debug/Res/8.png , 17036
C# winform Socket/客户端/bin/Debug/Res/9.png , 17219
C# winform Socket/客户端/bin/Debug/Res/clos.png , 535
C# winform Socket/客户端/bin/Debug/Res/login_1.png , 36270
C# winform Socket/客户端/bin/Debug/Res/logo_bg.png , 90436
C# winform Socket/客户端/bin/Debug/Res/logo_me.png , 1238
C# winform Socket/客户端/bin/Debug/Res/me.png , 104819
C# winform Socket/客户端/bin/Debug/Res/other.png , 7054
C# winform Socket/客户端/bin/Debug/Res/pasw.png , 5081
C# winform Socket/客户端/bin/Debug/Res/q1.jpg , 10909
C# winform Socket/客户端/bin/Debug/Res/q2.jpg , 8106
C# winform Socket/客户端/bin/Debug/Res/q3.jpg , 7893
C# winform Socket/客户端/bin/Debug/Res/s-arrow.png , 14593
C# winform Socket/客户端/bin/Debug/Res/s-track.png , 149
C# winform Socket/客户端/bin/Debug/Res/send.png , 21261
C# winform Socket/客户端/bin/Debug/Res/smail.png , 2956
C# winform Socket/客户端/bin/Debug/Res/tx1.jpg , 7420
C# winform Socket/客户端/bin/Debug/Res/tx2.jpg , 5980
C# winform Socket/客户端/bin/Debug/Res/tx3.jpg , 9513
C# winform Socket/客户端/bin/Debug/Res/tx4.jpg , 6833
C# winform Socket/客户端/bin/Debug/Res/tx5.jpg , 7451
C# winform Socket/客户端/bin/Debug/Res/tx6.jpg , 6661
C# winform Socket/客户端/bin/Debug/Res/tx7.jpg , 5349
C# winform Socket/客户端/bin/Debug/Thedog.exe , 301568
C# winform Socket/客户端/bin/Debug/Thedog.exe.config , 189
C# winform Socket/客户端/bin/Debug/Thedog.pdb , 77312
C# winform Socket/客户端/bin/Debug/Thedog.vshost.exe , 22696
C# winform Socket/客户端/bin/Debug/Thedog.vshost.exe.config , 189
C# winform Socket/客户端/bin/Debug/Thedog.vshost.exe.manifest , 490
C# winform Socket/客户端/Form1.cs , 51551
C# winform Socket/客户端/Form1.Designer.cs , 37881
C# winform Socket/客户端/Form1.resx , 312331
C# winform Socket/客户端/Form2.cs , 4840
C# winform Socket/客户端/Form2.Designer.cs , 2858
C# winform Socket/客户端/Form2.resx , 9964
C# winform Socket/客户端/Formlogin.cs , 4911
C# winform Socket/客户端/Formlogin.Designer.cs , 5725
C# winform Socket/客户端/Formlogin.resx , 73209
C# winform Socket/客户端/obj/Debug/Thedog.csproj.FileListAbsolute.txt , 69
C# winform Socket/客户端/Program.cs , 851
C# winform Socket/客户端/Properties/AssemblyInfo.cs , 1320
C# winform Socket/客户端/Properties/Resources.Designer.cs , 2825
C# winform Socket/客户端/Properties/Resources.resx , 5612
C# winform Socket/客户端/Properties/Settings.Designer.cs , 1093
C# winform Socket/客户端/Properties/Settings.settings , 249
C# winform Socket/客户端/richtext_pic.cs , 6001
C# winform Socket/客户端/Thedog.csproj , 4631
C# winform Socket/客户端/Thedog.sln , 978
C# winform Socket/服务端/.vs/ChatServer/v14/.suo , 50176
C# winform Socket/服务端/ChatServer/bin/Debug/ChatServer.exe , 14848
C# winform Socket/服务端/ChatServer/bin/Debug/ChatServer.pdb , 28160
C# winform Socket/服务端/ChatServer/bin/Debug/ChatServer.vshost.exe , 21680
C# winform Socket/服务端/ChatServer/bin/Debug/ChatServer.vshost.exe.manifest , 490
C# winform Socket/服务端/ChatServer/ChatServer.csproj , 3595
C# winform Socket/服务端/ChatServer/FormServer.cs , 10800
C# winform Socket/服务端/ChatServer/FormServer.designer.cs , 7162
C# winform Socket/服务端/ChatServer/FormServer.resx , 5814
C# winform Socket/服务端/ChatServer/obj/Debug/DesignTimeResolveAssemblyReferencesInput.cache , 6112
C# winform Socket/服务端/ChatServer/Program.cs , 474
C# winform Socket/服务端/ChatServer/Properties/AssemblyInfo.cs , 1176
C# winform Socket/服务端/ChatServer/Properties/Resources.Designer.cs , 2860
C# winform Socket/服务端/ChatServer/Properties/Resources.resx , 5612
C# winform Socket/服务端/ChatServer/Properties/Settings.Designer.cs , 1110
C# winform Socket/服务端/ChatServer/Properties/Settings.settings , 249
C# winform Socket/服务端/ChatServer/User.cs , 506
C# winform Socket/服务端/ChatServer.sln , 997
C# winform Socket/服务端/ChatServer.suo , 16896
C# winform Socket/服务端/UpgradeLog.htm , 41626
没有账号? 忘记密码?

社交账号快速登录