資源簡介
服務器端:
申請一個socket
綁定到一個IP地址和一個端口上
開啟偵聽,等待接授連接
客戶端:
申請一個socket
連接服務器(指明IP地址和端口號)
服務器端接到連接請求后,產生一個新的socket(端口大于1024)與客戶端建立連接并進行通訊,原監聽socket繼續監聽。
代碼片段和文件信息
using?System;
using?System.Collections.Generic;
using?System.ComponentModel;
using?System.Data;
using?System.Drawing;
using?System.IO;
using?System.Linq;
using?System.Net;
using?System.Net.Sockets;
using?System.Text;
using?System.Threading;
using?System.Threading.Tasks;
using?System.Windows.Forms;
namespace?_06Server
{
????public?partial?class?Form1?:?Form
????{
????????public?Form1()
????????{
????????????InitializeComponent();
????????}
????????private?void?btnStart_Click(object?sender?EventArgs?e)
????????{
????????????try
????????????{
????????????????//當點擊開始監聽的時候?在服務器端創建一個負責監IP地址跟端口號的Socket
????????????????Socket?socketWatch?=?new?Socket(AddressFamily.InterNetwork?SocketType.Stream?ProtocolType.Tcp);
????????????????IPAddress?ip?=?IPAddress.Any;//IPAd
評論
共有 條評論