【實驗室筆記】C#的Socket客戶端接收和發送數據


采用socket發送和接收數據的實驗中,服務器采用的是網絡助手作為模擬服務器端。

客戶端程序流程:

應用的命名空間:

1 using System.Net; 2 using System.Net.Sockets; 3 using System.Threading; 4 using System.Timers;

【1】首先新建一個Socket;

【2】建立ip地址應用值;

【3】Socket連接;

【4】判斷連接狀態;

 

 1      Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);  2 
 3         private void button1_Click(object sender, EventArgs e)  4  {  5                    if (textBox1.Text != "" || textBox2.Text != "")  6  {  7 
 8                         IPAddress ip = IPAddress.Parse(textBox2.Text);  9 
10                         try
11  { 12  s.Connect(ip, Convert.ToInt16(textBox1.Text)); 13                             MessageBox.Show("服務器連接中。。。"); 14  } 15                         catch
16  { 17                             MessageBox.Show("服務器連接失敗。。。"); 18  } 19                         try
20  { 21                             if (s.Connected == true) 22  { 23                                 MessageBox.Show("與服務器連接成功"); 24                                 aTimer.Enabled = true; 25  } 26                             else
27  { 28                                 MessageBox.Show("與服務器連接失敗"); 29  } 30  } 31                         catch
32  { 33                             MessageBox.Show("檢測連接狀態出錯"); 34  } 35  } 36                     else
37  { 38                         MessageBox.Show("請輸入端口號和IP地址"); 39  } 40                     
41         }

 

 

Socket數據的發送

 

 1         private void button2_Click(object sender, EventArgs e)  2  {  3             if (s.Connected == true)  4  {  5                 try
 6  {  7                     string abc = textBox3.Text;  8 
 9  s.Send(Encoding.ASCII.GetBytes(abc)); 10 
11                     MessageBox.Show("向服務器發送:" + abc); 12  } 13                 catch
14  { 15                     MessageBox.Show("發送失敗"); 16  } 17  } 18         }

 

Socket數據接收

 

數據接收要交給線程去做,然后調用定時器去做,這樣會防止在數據接收時,其他程序不可用的狀況。

 

 1         System.Timers.Timer aTimer = new System.Timers.Timer();  2 
 3         byte[] res = new byte[1024];  4 
 5         private void Form1_Load(object sender, EventArgs e)  6  {  7             Control.CheckForIllegalCrossThreadCalls = false;  8             aTimer.Enabled = false;  9             Thread thread1 = new Thread(TimerMange); 10             thread1.IsBackground = true; 11  thread1.Start(); 12  } 13 
14         void TimerMange() 15  { 16             aTimer.Elapsed += new ElapsedEventHandler(socket_rev);    //定時事件的方法
17             aTimer.Interval = 100; 18  } 19 
20         private void socket_rev(object sender, EventArgs e) 21  { 22             int receiveLength = s.Receive(res, res.Length, SocketFlags.None); 23 
24             if (receiveLength > 0) 25  { 26                 textBox4.Text = Encoding.ASCII.GetString(res, 0, receiveLength); 27                 string abc = "HaveReceive"; 28  s.Send(Encoding.ASCII.GetBytes(abc)); 29  } 30         }  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM