對於文字流,檔案流,網路流和網路上通訊的操作
//檔案流
FileStream fs = new FileStream("", FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite);
byte[] b = new byte[1024];
int i = fs.Read(b, 0, b.Length);
fs.Write(b, 0, b.Length);
fs.Flush();//只有在寫時候才會用到FLUSH清記憶體
fs.Close();
//文字流
StreamReader sr = new StreamReader("", Encoding.UTF32);
sr.Read();
sr.Close();
StreamWriter sw = new StreamWriter("", true, Encoding.UTF8);
sw.WriteLine("abcd");
sw.Flush();//只有在FLUSH後才正真將內容寫入檔案中。要麼就在CLOSE後才會寫入檔案。在主發前一直是儲存在綬存中。
sw.Close();
//網路流一般是提供給網路TCPClient和TCPListener用的
IPHostEntry iphostentry = Dns.GetHostEntry(Dns.GetHostName());
IPAddress ipaddress = iphostentry.AddressList[0];
IPEndPoint ipendpoint = new IPEndPoint(ipaddress, 8060);
Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
s.Connect(ipendpoint);
//網路套接字的傳送和接收
s.Send(b);
s.Receive(b);
//NetworkStream nws = new NetworkStream(s);
//nws.Write(b, 0, b.Length);
//nws.Read(b, 0, b.Length);
//nws.Flush();
//nws.Close();
//客戶端
TcpClient tcpclient = new TcpClient();//若不指定本機的IP及埠,預設為本機的IP和0號埠
tcpclient.Connect("www.163.com
NetworkStream nws1 = tcpclient.GetStream();
nws1.Write(b, 0, b.Length);
nws1.Read(b, 0, b.Length);
//伺服器端
TcpListener tcplistener = new TcpListener(8086);//指定伺服器端監聽埠
tcplistener.Start();//開始監聽
TcpClient tcpclient1 = tcplistener.AcceptTcpClient();//接受客戶端的請求。
nws1 = tcpclient1.GetStream();
nws1.Read(b, 0, b.Length);
nws1.Write(b, 0, b.Length);