Client 代碼網絡
private void btnSend_Click(object sender, EventArgs e)
{
TcpClient tcpClient = new TcpClient(txthost.Text, Int32.Parse(txtport.Text)); //建立tcp客戶端
NetworkStream ns = tcpClient.GetStream(); //建立網絡流
FileStream fs = File.Open("..\\..\\Form1.cs", FileMode.Open); //打開文件
int data = fs.ReadByte();
while(data!=-1)
{
ns.WriteByte((byte)data);
data = fs.ReadByte(); //讀寫文件操做
}
fs.Close();
ns.Close();
tcpClient.Close();
}tcp
Receive端代碼this
public Form1()
{
InitializeComponent();
//經過線程 獲取網絡 tcp監聽獲取網絡數據
new Thread(()=>{
IPAddress loacaladdr = IPAddress.Parse("127.0.0.1");
Int32 port = 2112;
TcpListener tcplistener = new TcpListener(loacaladdr, port);
tcplistener.Start();
TcpClient tcpClient = tcplistener.AcceptTcpClient();
NetworkStream ns = tcpClient.GetStream();
StreamReader sr = new StreamReader(ns);
string result = sr.ReadToEnd();
//this.Invoke(new UpdateDisplayDelegate(DisPlay), new object[] { result });
this.Invoke((MethodInvoker)delegate
{
txtDisplay.Text = result;線程
});
}).Start();
}orm