91av视频/亚洲h视频/操亚洲美女/外国一级黄色毛片 - 国产三级三级三级三级

  • 大小: 36KB
    文件類型: .7z
    金幣: 1
    下載: 0 次
    發(fā)布日期: 2021-06-07
  • 語言: C#
  • 標簽: wpf??socket??C#??

資源簡介

兩個wpf程序,一個是服務器,一個是客戶端。兩個進行通信。

資源截圖

代碼片段和文件信息

using?System;
using?System.Net;
using?System.Net.Sockets;
using?System.Text;
using?System.Threading;
using?System.Windows.Forms;
?
namespace?ClientSocket
{
????public?partial?class?Form1?:?Form
????{
????????//定義Socket對象
????????Socket?clientSocket;
????????//創(chuàng)建接收消息的線程
????????Thread?threadReceive;
????????//接收服務端發(fā)送的數(shù)據(jù)
????????string?str;
?
????????public?Form1()
????????{
????????????InitializeComponent();
????????}
?
????????private?void?Form1_Load(object?sender?EventArgs?e)
????????{
?
????????}
?
????????private?void?button_connect_Click(object?sender?EventArgs?e)
????????{
????????????IPAddress?ip?=?IPAddress.Parse(this.text_ip.Text.Trim());
????????????clientSocket?=?new?Socket(AddressFamily.InterNetwork?SocketType.Stream?ProtocolType.Tcp);
????????????try?{
????????????????//連接服務端
????????????????clientSocket.Connect(ip?Convert.ToInt32(this.text_port.Text.Trim()));
????????????????//開啟線程不停的接收服務端發(fā)送的數(shù)據(jù)
????????????????threadReceive?=?new?Thread(new?ThreadStart(Receive));
????????????????threadReceive.IsBackground?=?true;
????????????????threadReceive.Start();
????????????????//設(shè)置連接按鈕在連接服務端后狀態(tài)為不可點
????????????????this.button_connect.Enabled?=?false;
????????????}?catch?{
????????????????MessageBox.Show(“連接服務端失敗,請確認ip和端口是否填寫正確“?“連接服務端失敗“);
????????????}
????????}
?
????????//接收服務端消息的線程方法
????????private?void?Receive()
????????{
????????????try?{
????????????????while?(true)?{
????????????????????byte[]?buff?=?new?byte[20000];
????????????????????int?r?=?clientSocket.Receive(buff);
????????????????????str?=?Encoding.Default.GetString(buff0r);
????????????????????this.Invoke(new?Action(()?=>?{?this.text_log1.Text?+=?(str?+?“\r\n“);?}));
????????????????}
????????????}?catch?{
????????????????MessageBox.Show(“獲取服務端參數(shù)失敗““獲取服務端參數(shù)失敗“);
????????????}
????????}
?
????????private?void?button_close_Click(object?sender?EventArgs?e)
????????{
????????????//clientSocket關(guān)閉
????????????clientSocket.Close();
????????????//threadReceive關(guān)閉
????????????threadReceive.Abort();
????????}
?
????????private?void?button_send_Click(object?sender?EventArgs?e)
????????{
????????????try?{
????????????????string?strMsg?=?this.text_log2.Text.Trim();
????????????????byte[]?buffer?=?new?byte[20000];
????????????????buffer?=?Encoding.Default.GetBytes(strMsg);
????????????????clientSocket.Send(buffer);
????????????}?catch?{
????????????????MessageBox.Show(“發(fā)送數(shù)據(jù)失敗“?“發(fā)送數(shù)據(jù)失敗“);
????????????}
????????}
????}
}

評論

共有 條評論