国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計 > 正文

C# Socket編程

2019-11-18 17:21:58
字體:
供稿:網(wǎng)友

//Socket基本編程

//服務(wù)端:

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; //設(shè)定標(biāo)示位,判斷偵聽狀態(tài)

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

private StreamReader srRead;

private System.Windows.Forms.StatusBar statusBar1;

private System.Windows.Forms.Button button1;

private System.Windows.Forms.ListBox listBox1; //從網(wǎng)絡(luò)基礎(chǔ)數(shù)據(jù)流中讀取數(shù)據(jù)

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ā)送、接收數(shù)據(jù)的網(wǎng)絡(luò)基礎(chǔ)數(shù)據(jù)流

srRead=new StreamReader(nsStream);//以得到的網(wǎng)絡(luò)基礎(chǔ)數(shù)據(jù)流來初始化StreamReader實例

statusBar1.Text = "已經(jīng)連接!";

 

while( blistener ) //循環(huán)偵聽

{

string sMessage = srRead.ReadLine();//從網(wǎng)絡(luò)基礎(chǔ)數(shù)據(jù)流中讀取一行數(shù)據(jù)

if ( sMessage == "STOP" ) //判斷是否為斷開TCP連接控制碼

{

tlTcpListen.Stop(); //關(guān)閉偵聽

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

srRead.Close();

statusBar1.Text = "連接已經(jīng)關(guān)閉!" ;

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

return;

}

 

string sTime = DateTime.Now.ToShortTimeString ( ) ; //獲取接收數(shù)據(jù)時的時間

listBox1.Items.Add ( sTime + " " + sMessage ) ;

}

}

catch ( System.Security.SecurityException )

{

MessageBox.Show ( "偵聽失敗!" , "錯誤" ) ;

}

}

//開始監(jiān)聽

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

{

thThreadRead = new Thread ( new ThreadStart ( Listen ) );

thThreadRead.Start();//啟動線程

button1.Enabled=false;

}

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

protected override void Dispose( bool disposing )

{

try

{

tlTcpListen.Stop(); //關(guān)閉偵聽

nsStream.Close();

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

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

}

catch{}

 

if( disposing )

{

if (components != null)

{

components.Dispose();

}

}

base.Dispose( disposing );

}

 

TCP協(xié)議的發(fā)送端

 

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

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

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

using System.Net; //使用IPAddress類、IPHostEntry類等

 

private StreamWriter swWriter; //用以向網(wǎng)絡(luò)基礎(chǔ)數(shù)據(jù)流傳送數(shù)據(jù) 

private NetworkStream nsStream; //創(chuàng)建發(fā)送數(shù)據(jù)的網(wǎng)絡(luò)基礎(chǔ)數(shù)據(jù)流 

private TcpClient tcpClient;

private System.Windows.Forms.Button button1;

private System.Windows.Forms.TextBox textBox1;

private System.Windows.Forms.Button button2;

private System.Windows.Forms.TextBox textBox2;

private System.Windows.Forms.StatusBar statusBar1;

private System.Windows.Forms.Label label1;

private System.Windows.Forms.Label label2; //通過它實現(xiàn)向遠程主機提出TCP連接申請 

private bool tcpConnect = false; //定義標(biāo)識符,用以表示TCP連接是否建立

 

//連接 

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

{

IPAddress ipRemote ;

try

{

ipRemote = IPAddress.Parse ( textBox1.Text ) ;

}

catch //判斷給定的IP地址的合法性

{

MessageBox.Show ( "輸入的IP地址不合法!" , "錯誤提示!" ) ;

return ;

}

 

IPHostEntry ipHost ;

try

{

ipHost = Dns.Resolve ( textBox1.Text ) ; 

}

catch //判斷IP地址對應(yīng)主機是否在線

{

MessageBox.Show ("遠程主機不在線!" , "錯誤提示!" ) ;

return ;

}

 

string sHostName = ipHost.HostName ;

try

{

TcpClient tcpClient = new TcpClient(sHostName,8000);//對遠程主機的8000端口提出TCP連接申請

nsStream = tcpClient.GetStream();//通過申請,并獲取傳送數(shù)據(jù)的網(wǎng)絡(luò)基礎(chǔ)數(shù)據(jù)流  

swWriter = new StreamWriter(nsStream);//使用獲取的網(wǎng)絡(luò)基礎(chǔ)數(shù)據(jù)流來初始化StreamWriter實例

button1.Enabled = false ;

button2.Enabled = true ;

tcpConnect = true ;

statusBar1.Text = "已經(jīng)連接!" ;

}

catch

{

MessageBox.Show ( "無法和遠程主機8000端口建立連接!" , "錯誤提示!" ) ;

return ;

}

}

 

//發(fā)送

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

{

if (textBox2.Text !="")

{

swWriter.WriteLine(textBox2.Text);//刷新當(dāng)前數(shù)據(jù)流中的數(shù)據(jù)

swWriter.Flush();

}

else

{

MessageBox.Show("發(fā)送信息不能為空!","錯誤提示!");

}

}

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

protected override void Dispose( bool disposing )

{

if ( tcpConnect )

{

swWriter.WriteLine ( "STOP" ) ; //發(fā)送控制碼  

swWriter.Flush (); //刷新當(dāng)前數(shù)據(jù)流中的數(shù)據(jù)  

nsStream.Close (); //清除資源

swWriter.Close ();

}

if( disposing )

{

if (components != null)

{

components.Dispose();

}

}

base.Dispose( disposing );

}


發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 资溪县| 高青县| 行唐县| 贞丰县| 二连浩特市| 通河县| 汽车| 奉新县| 抚顺市| 景洪市| 广州市| 房山区| 虹口区| 墨竹工卡县| 宿州市| 昭觉县| 西乡县| 邯郸市| 宁安市| 铜陵市| 辉县市| 长葛市| 宝兴县| 日土县| 平度市| 灌南县| 留坝县| 黄平县| 阿坝| 南漳县| 黄骅市| 新田县| 紫云| 闵行区| 西平县| 京山县| 民和| 光山县| 金门县| 通州区| 进贤县|