資源簡介
TcpListener類與TcpClient類的使用

代碼片段和文件信息
using?System;
using?System.Collections.Generic;
using?System.ComponentModel;
using?System.Data;
using?System.Drawing;
using?System.Linq;
using?System.Text;
using?System.Threading.Tasks;
using?System.Windows.Forms;
using?System.Net;
using?System.Net.Sockets;
using?System.Threading;
namespace?Tcpclient
{
????public?partial?class?Form1?:?Form
????{
????????
????????public?Form1()
????????{
????????????InitializeComponent();
????????}
????????private?void?button3_Click(object?sender?EventArgs?e)
????????{
????????????Thread?sendMsg?=?new?Thread(SendMessage);
????????????sendMsg.IsBackground?=?true;
????????????sendMsg.Start();
????????????if?(textBox3.Text.Equals(“$END$“))
????????????{
????????????????button1.Enabled?=?true;
????????????????txtIP.Enabled?=?true;
????????????????txtPort.Enabled?=?true;
????????????????textBox3.Enabled?=?false;
????????????????button3.Enabled?=?false;
????????????????label3.Text?=?“已關(guān)閉連接!\n“;
????????????}
????????}
????????TcpClient?client?=?null;
????????private?void?button1_Click(object?sender?EventArgs?e)
????????{
????????????client?=?new?TcpClient(txtIP.Text.Trim()?Convert.ToInt32(txtPort.Text));
????????????string?msg?=?string.Format(“已連接入服務(wù)器:{0}端口號:{1}\n“?txtIP.Text?txtPort.Text);
????????????label3.Text?=?msg;
????????????button1.Enabled?=?false;
????????????txtIP.Enabled?=?false;
????????????txtPort.Enabled?=?false;
????????????textBox3.Enabled?=?true;
????????????button3.Enabled?=?true;
????????}
????????private?void?SendMessage()
????????{
????????????string?msg?=?textBox3.Text;
????????????if?(client?==?null)?return;
????????????if?(client.Connected?==?false)?return;
????????????byte[]?data?=?Encoding.UTF8.GetBytes(msg);
????????????int?len?=?data.Length;
????????????byte[]?buffer?=?BitConverter.GetBytes(len);
????????????client.GetStream().Write(buffer?0?4);
????????????client.GetStream().Write(data?0?data.Length);
????????}
????}
}
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件????????679??2017-02-09?14:27??TcpListen\.git\config
?????文件?????????73??2017-02-09?14:27??TcpListen\.git\desc
?????文件?????????23??2017-02-09?14:27??TcpListen\.git\HEAD
?????文件????????177??2017-02-09?14:27??TcpListen\.git\hooks\README.sample
?????文件???????2416??2017-02-09?14:27??TcpListen\.git\index
?????文件????????113??2017-02-09?14:27??TcpListen\.git\info\exclude
?????文件????????336??2017-02-09?14:27??TcpListen\.git\logs\HEAD
?????文件????????336??2017-02-09?14:27??TcpListen\.git\logs\refs\heads\master
?????文件????????150??2017-02-09?14:27??TcpListen\.git\ms-persist.xm
?????文件????????573??2017-02-09?14:27??TcpListen\.git\ob
?????文件????????309??2017-02-09?14:27??TcpListen\.git\ob
?????文件????????317??2017-02-09?14:27??TcpListen\.git\ob
?????文件????????258??2017-02-09?14:27??TcpListen\.git\ob
?????文件???????1665??2017-02-09?14:27??TcpListen\.git\ob
?????文件????????751??2017-02-09?14:27??TcpListen\.git\ob
?????文件????????188??2017-02-09?14:27??TcpListen\.git\ob
?????文件????????718??2017-02-09?14:27??TcpListen\.git\ob
?????文件????????186??2017-02-09?14:27??TcpListen\.git\ob
?????文件???????1883??2017-02-09?14:27??TcpListen\.git\ob
?????文件???????1277??2017-02-09?14:27??TcpListen\.git\ob
?????文件????????138??2017-02-09?14:27??TcpListen\.git\ob
?????文件????????157??2017-02-09?14:27??TcpListen\.git\ob
?????文件????????192??2017-02-09?14:27??TcpListen\.git\ob
?????文件???????1180??2017-02-09?14:27??TcpListen\.git\ob
?????文件????????925??2017-02-09?14:27??TcpListen\.git\ob
?????文件?????????89??2017-02-09?14:27??TcpListen\.git\ob
?????文件????????153??2017-02-09?14:27??TcpListen\.git\ob
?????文件???????1264??2017-02-09?14:27??TcpListen\.git\ob
?????文件????????191??2017-02-09?14:27??TcpListen\.git\ob
?????文件????????185??2017-02-09?14:27??TcpListen\.git\ob
............此處省略140個文件信息
評論
共有 條評論