資源簡(jiǎn)介
包括服務(wù)器端和客戶端源碼,采用C#實(shí)現(xiàn)服務(wù)器與客戶端通過(guò)TCP協(xié)議進(jìn)行數(shù)據(jù)通信,對(duì)于多個(gè)連接的客戶端,服務(wù)器端采用多線程處理客戶端發(fā)送的數(shù)據(jù)。

代碼片段和文件信息
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.Net.Sockets;
using?System.Net;
using?System.Threading;
namespace?Client
{
????public?partial?class?Form1?:?Form
????{
????????private?IPAddress?serverIP?=?IPAddress.Parse(“127.0.0.1“);//以本機(jī)作測(cè)試
????????public?Socket?sock;
????????private?IPEndPoint?serverFullAddr;//完整終端地址
????????public?Form1()
????????{
????????????InitializeComponent();
????????????System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls?=?false;?
????????}
????????
????????private?void?Form1_Load(object?sender?EventArgs?e)
????????{
????????}
????????public?void?ReceiveData()
????????{
????????????while?(true)
????????????{
????????????????string?mess?=?““;
????????????????try
????????????????{
????????????????????byte[]?message?=?new?byte[1024];
????????????????????int?bytes?=?0;
????????????????????try
????????????????????{
????????????????????????bytes?=?sock.Receive(message);//接收數(shù)據(jù)
????????????????????????mess?=?mess?+?Encoding.Default.GetString(message?0?bytes);
????????????????????????if?(message.Length?>?1024)
????????????????????????{
????????????????????????????do
????????????????????????????{
????????????????????????????????bytes?=?sock.Receive(message?message.Length?0);
????????????????????????????????mess?=?mess?+?Encoding.ASCII.GetString(message?0?bytes);
????????????????????????????}
????????????????????????????while?(bytes?>?0);
????????????????????????}
????????????????????????MessageBox.Show(mess);
????????????????????}
????????????????????catch
????????????????????{
????????????????????????//?MessageBox.Show(“空數(shù)據(jù)!“);
????????????????????}
????????????????????
????????????????}
????????????????catch?(Exception?ex)
????????????????{
????????????????????
????????????????}
????????????}
????????}//調(diào)用該函數(shù)將socket接收的字符使用RSA私鑰解密后以string形式賦值給全局變量dedata,注意多線程調(diào)用
????????private?void?button1_Click(object?sender?EventArgs?e)
????????{
????????????try
????????????{
????????????????serverIP?=?IPAddress.Parse(textBox1.Text.ToString());
????????????????serverFullAddr?=?new?IPEndPoint(serverIP?int.Parse(textBox2.Text.ToString()));//設(shè)置IP,端口
????????????????sock?=?new?Socket(AddressFamily.InterNetwork?SocketType.Stream?ProtocolType.Tcp);
????????????????//指定本地主機(jī)地址和端口號(hào)
????????????????sock.Connect(serverFullAddr);
????????????????StatuLabel.Text?=?“連接服務(wù)器成功!“;
????????????????Thread?thr?=?new?Thread(ReceiveData);
????????????????thr.IsBackground?=?true;
????????????????thr.Start();
????????????}
????????????catch?(Exception)
????????????{
????????????????StatuLabel.Text?=?“連接服務(wù)器失敗!“;
????????????}
????????}
????????private?void?button2_Click(object?sender?EventArgs?e)
????????{
????????????sock.Send(Encoding.Default.GetBytes(textBox3.Text.ToString()));
????????}
?
?屬性????????????大小?????日期????時(shí)間???名稱
-----------?---------??----------?-----??----
?????目錄???????????0??2018-05-12?12:43??coresource\
?????目錄???????????0??2018-05-12?12:44??coresource\Client\
?????目錄???????????0??2018-05-12?12:44??coresource\Client\Client\
?????文件???????11776??2018-05-12?12:43??coresource\Client\Client.exe
?????文件?????????987??2018-05-11?14:36??coresource\Client\Client.sln
?????文件???????45568??2018-05-12?12:43??coresource\Client\Client.v12.suo
?????文件?????????189??2018-05-11?14:36??coresource\Client\Client\App.config
?????文件????????3865??2018-05-11?14:52??coresource\Client\Client\Client.csproj
?????文件????????3462??2018-05-12?12:43??coresource\Client\Client\Form1.cs
?????文件????????7332??2018-05-12?12:43??coresource\Client\Client\Form1.Designer.cs
?????文件????????5817??2018-05-12?12:43??coresource\Client\Client\Form1.resx
?????文件?????????518??2018-05-11?14:36??coresource\Client\Client\Program.cs
?????目錄???????????0??2018-05-12?12:43??coresource\Client\Client\Properties\
?????文件????????1336??2018-05-11?14:36??coresource\Client\Client\Properties\AssemblyInfo.cs
?????文件????????2864??2018-05-11?14:36??coresource\Client\Client\Properties\Resources.Designer.cs
?????文件????????5612??2018-05-11?14:36??coresource\Client\Client\Properties\Resources.resx
?????文件????????1093??2018-05-11?14:36??coresource\Client\Client\Properties\Settings.Designer.cs
?????文件?????????249??2018-05-11?14:36??coresource\Client\Client\Properties\Settings.settings
?????目錄???????????0??2018-05-12?12:43??coresource\Server\
?????目錄???????????0??2018-05-12?12:44??coresource\Server\Server\
?????文件???????14336??2018-05-12?12:43??coresource\Server\Server.exe
?????文件?????????987??2018-05-11?13:38??coresource\Server\Server.sln
?????文件???????38400??2018-05-12?12:43??coresource\Server\Server.v12.suo
?????文件?????????189??2018-05-11?13:38??coresource\Server\Server\App.config
?????文件????????9853??2018-05-12?12:43??coresource\Server\Server\Form1.cs
?????文件????????6528??2018-05-12?12:36??coresource\Server\Server\Form1.Designer.cs
?????文件????????6011??2018-05-12?12:36??coresource\Server\Server\Form1.resx
?????文件?????????518??2018-05-11?13:38??coresource\Server\Server\Program.cs
?????目錄???????????0??2018-05-12?12:43??coresource\Server\Server\Properties\
?????文件????????1336??2018-05-11?13:38??coresource\Server\Server\Properties\AssemblyInfo.cs
?????文件????????2864??2018-05-11?13:38??coresource\Server\Server\Properties\Resources.Designer.cs
............此處省略4個(gè)文件信息
評(píng)論
共有 條評(píng)論