資源簡介
一個服務器端和多個客戶端的Socket通信。啟動服務端后,服務端通過持續監聽客戶端發來的請求,一旦監聽到客戶端傳來的信息后,兩端便可以互發信息了。服務器端需要綁定一個IP和端口號,用于客戶端在網絡中尋找并建立連接。信息發送原理:將手動輸入字符串信息轉換成機器可以識別的字節數組,然后調用套接字的Send()方法將字節數組發送出去。信息接收原理:調用套接字的Receive()方法,獲取對端傳來的字節數組,然后將其轉換成人可以讀懂的字符串信息。

代碼片段和文件信息
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;
using?System.Net.Sockets;
namespace?SocketClient
{
????public?partial?class?FormClient?:?Form
????{
????????Socket?client;
????????public?FormClient()
????????{
????????????InitializeComponent();
????????}
????????private?void?button1_Click(object?sender?EventArgs?e)
????????{
????????????AsyncConnect();
????????}
????????///?
????????///?連接到服務器
????????///?
????????public?void?AsyncConnect()
????????{
????????????try
????????????{
????????????????//端口及IP
????????????????IPEndPoint?ipe?=?new?IPEndPoint(IPAddress.Parse(textBox_ip.Text)?int.Parse(textBox_port.Text));
????????????????//創建套接字
????????????????client?=?new?Socket(AddressFamily.InterNetwork?SocketType.Stream?ProtocolType.Tcp);
????????????????//開始連接到服務器
????????????????client.BeginConnect(ipe?asyncResult?=>
????????????????{
????????????????????client.EndConnect(asyncResult);
????????????????????//向服務器發送消息
????????????????????AsyncSend(client?“你好我是客戶端“);
????????????????????//接受消息
????????????????????AsyncReceive(client);
????????????????}?null);
????????????}
????????????catch?(Exception?ex)
????????????{
????????????}
????????}
????????///?
????????///?發送消息
????????///?
????????///?
????????///?
????????public?void?AsyncSend(Socket?socket?string?message)
????????{
????????????if?(socket?==?null?||?message?==?string.Empty)?return;
????????????//編碼
????????????byte[]?data?=?Encoding.UTF8.GetBytes(message);
????????????try
????????????{
????????????????socket.BeginSend(data?0?data.Length?SocketFlags.None?asyncResult?=>
????????????????{
????????????????????//完成發送消息
????????????????????int?length?=?socket.EndSend(asyncResult);
????????????????}?null);
????????????}
????????????catch?(Exception?ex)
????????????{
????????????}
????????}
????????///?
????????///?接收消息
????????///?
????????///?
????????public?void?AsyncReceive(Socket?socket)
????????{
????????????byte[]?data?=?new?byte[1024];
????????????try
????????????{
????????????????//開始接收數據
????????????????socket.BeginReceive(data?0?data.Length?SocketFlags.None
????????????????asyncResult?=>
????????????????{
????????????????????try
????????????????????{
????????????????????????int?length?=?socket.EndReceive(asyncResult);
????????????????????????setText(Encoding.UTF8.GetString(data));
????????????????????}
????????????????????catch?(Exception)
????????????????????{
????????????????????????AsyncReceive(socket);
????????????????????}
????????????????????AsyncReceive(socket);
????????????????}?null);
????????????}
????????????catch?(Exception?ex)
????????????{
???
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????目錄???????????0??2017-10-31?16:53??SocketTest\
?????目錄???????????0??2017-10-17?09:07??SocketTest\SocketClient\
?????目錄???????????0??2017-10-12?17:24??SocketTest\SocketClient\bin\
?????目錄???????????0??2017-10-31?16:53??SocketTest\SocketClient\bin\Debug\
?????文件???????11776??2017-10-19?21:23??SocketTest\SocketClient\bin\Debug\SocketClient.exe
?????文件???????26112??2017-10-19?21:23??SocketTest\SocketClient\bin\Debug\SocketClient.pdb
?????文件???????11600??2017-10-16?14:36??SocketTest\SocketClient\bin\Debug\SocketClient.vshost.exe
?????文件?????????490??2016-07-16?19:44??SocketTest\SocketClient\bin\Debug\SocketClient.vshost.exe.manifest
?????目錄???????????0??2017-10-12?17:24??SocketTest\SocketClient\bin\Release\
?????文件????????3596??2017-10-19?21:22??SocketTest\SocketClient\FormClient.cs
?????文件????????5892??2017-10-19?17:39??SocketTest\SocketClient\FormClient.Designer.cs
?????文件????????5817??2017-10-19?17:39??SocketTest\SocketClient\FormClient.resx
?????目錄???????????0??2017-10-12?17:24??SocketTest\SocketClient\obj\
?????目錄???????????0??2017-10-12?17:24??SocketTest\SocketClient\obj\x86\
?????目錄???????????0??2017-10-19?21:23??SocketTest\SocketClient\obj\x86\Debug\
?????文件????????5420??2017-10-17?08:19??SocketTest\SocketClient\obj\x86\Debug\DesignTimeResolveAssemblyReferences.cache
?????文件????????6341??2017-10-19?21:23??SocketTest\SocketClient\obj\x86\Debug\DesignTimeResolveAssemblyReferencesInput.cache
?????文件?????????656??2017-10-19?21:23??SocketTest\SocketClient\obj\x86\Debug\SocketClient.csproj.FileListAbsolute.txt
?????文件?????????980??2017-10-19?21:23??SocketTest\SocketClient\obj\x86\Debug\SocketClient.csproj.GenerateResource.Cache
?????文件???????11776??2017-10-19?21:23??SocketTest\SocketClient\obj\x86\Debug\SocketClient.exe
?????文件?????????180??2017-10-19?21:23??SocketTest\SocketClient\obj\x86\Debug\SocketClient.FormClient.resources
?????文件???????26112??2017-10-19?21:23??SocketTest\SocketClient\obj\x86\Debug\SocketClient.pdb
?????文件?????????180??2017-10-19?21:23??SocketTest\SocketClient\obj\x86\Debug\SocketClient.Properties.Resources.resources
?????目錄???????????0??2017-10-12?17:24??SocketTest\SocketClient\obj\x86\Debug\TempPE\
?????文件?????????498??2017-10-12?17:24??SocketTest\SocketClient\Program.cs
?????目錄???????????0??2017-10-12?17:24??SocketTest\SocketClient\Properties\
?????文件????????1356??2017-10-12?17:24??SocketTest\SocketClient\Properties\AssemblyInfo.cs
?????文件????????2876??2017-10-12?17:24??SocketTest\SocketClient\Properties\Resources.Designer.cs
?????文件????????5612??2017-10-12?17:24??SocketTest\SocketClient\Properties\Resources.resx
?????文件????????1099??2017-10-12?17:24??SocketTest\SocketClient\Properties\Settings.Designer.cs
?????文件?????????249??2017-10-12?17:24??SocketTest\SocketClient\Properties\Settings.settings
............此處省略35個文件信息
- 上一篇:基于C#的實驗室設備管理系統
- 下一篇:C#實現坐標轉換
評論
共有 條評論