日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

DotNET(C#) Socket基本编程 (1)

發(fā)布時間:2023/11/27 生活经验 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 DotNET(C#) Socket基本编程 (1) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Socket基本編程

服務端:

using System.Net;

using System.Net.Sockets;

using System.Text;

using System.Threading;




Thread mythread ;

Socket socket;


// 清理所有正在使用的資源。

protected override void Dispose( bool disposing )

{

try

  {   

   socket.Close();//釋放資源

   mythread.Abort ( ) ;//中止線程

  }

  catch{ }



if( disposing )

{

if (components != null)

{

components.Dispose();

}

}

base.Dispose( disposing );

}

public static IPAddress GetServerIP()

{

IPHostEntry ieh=Dns.GetHostByName(Dns.GetHostName());

return ieh.AddressList[0];

}

private void BeginListen()

{

IPAddress ServerIp=GetServerIP();

IPEndPoint iep=new IPEndPoint(ServerIp,8000);

socket=new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);



byte[] byteMessage=new byte[100];

this.label1.Text=iep.ToString();

socket.Bind(iep);

// do

while(true)

{

try

{

socket.Listen(5);

Socket newSocket=socket.Accept();

newSocket.Receive(byteMessage);



string sTime = DateTime.Now.ToShortTimeString ( ) ;

string msg=sTime+":"+"Message from:";

msg+=newSocket.RemoteEndPoint.ToString()+Encoding.Default.GetString(byteMessage);

this.listBox1.Items.Add(msg);



}

catch(SocketException ex)

{

this.label1.Text+=ex.ToString();

}

}

// while(byteMessage!=null);

}

//開始監(jiān)聽

private void button1_Click(object sender, System.EventArgs e)

{

try

{

mythread = new Thread(new ThreadStart(BeginListen));

mythread.Start();



}

catch(System.Exception er)

{

MessageBox.Show(er.Message,"完成",MessageBoxButtons.OK,MessageBoxIcon.Stop);

}

}





客戶端:



using System.Net;

using System.Net.Sockets;

using System.Text;



private void button1_Click(object sender, System.EventArgs e)

{

BeginSend();

}

private void BeginSend()

{

string ip=this.txtip.Text;

string port=this.txtport.Text;



IPAddress serverIp=IPAddress.Parse(ip);

int serverPort=Convert.ToInt32(port);

IPEndPoint iep=new IPEndPoint(serverIp,serverPort);

byte[] byteMessage;

// do

// {

Socket socket=new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);

socket.Connect(iep);



byteMessage=Encoding.ASCII.GetBytes(textBox1.Text);

socket.Send(byteMessage);

socket.Shutdown(SocketShutdown.Both);

socket.Close();

// }

// while(byteMessage!=null);

}



基于TCP協(xié)議的發(fā)送和接收端



TCP協(xié)議的接收端



using System.Net.Sockets ; //使用到TcpListen類

using System.Threading ; //使用到線程

using System.IO ; //使用到StreamReader類



int port = 8000; //定義偵聽端口號

private Thread thThreadRead; //創(chuàng)建線程,用以偵聽端口號,接收信息

private TcpListener tlTcpListen; //偵聽端口號

private bool blistener = true; //設定標示位,判斷偵聽狀態(tài)

private NetworkStream nsStream; //創(chuàng)建接收的基本數據流

private StreamReader srRead;

private System.Windows.Forms.StatusBar statusBar1;

private System.Windows.Forms.Button button1;

private System.Windows.Forms.ListBox listBox1; //從網絡基礎數據流中讀取數據

private TcpClient tcClient ;



private void Listen ( )

{

try

{

tlTcpListen = new TcpListener ( port ) ; //以8000端口號來初始化TcpListener實例

tlTcpListen.Start ( ) ; //開始監(jiān)聽

statusBar1.Text = "正在監(jiān)聽..." ;

tcClient = tlTcpListen.AcceptTcpClient ( ) ; //通過TCP連接請求

nsStream = tcClient.GetStream ( ) ; //獲取用以發(fā)送、接收數據的網絡基礎數據流

srRead=new StreamReader(nsStream);//以得到的網絡基礎數據流來初始化StreamReader實例

statusBar1.Text = "已經連接!";

轉載于:https://www.cnblogs.com/lovenets/articles/422076.html

總結

以上是生活随笔為你收集整理的DotNET(C#) Socket基本编程 (1)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。