1. 网络流
NetworkStream
public NetworkStream{
Socket socket;
}
public NetworkStream{
Socket socket;
bool ownsSocket;
}
public NetworkStream{
Socket socket;
FileAccess access;
}
public NetworkStream{
Socket socket;
FileAccess access;
bool ownsSocket;
}
exam:
NetWorkStream newStream = new NetworkStream(mesock,System.IO.FileAccess.Read,true);
Read方法
public override int Read{
int byte[] buffer,
int offset,
int size
}
exam:
NetWorkStream newStream = new NetworkStream(mesock);
Byte[] myByte = new Byte[64];
newStream.Read(myByte,0,myByte.Length);
newSteam.Flush();
Write方法
public override void Write{
byte[] buffer,
int offset,
int size
}
2. 文本流
向文本写文本流
StreamWriter sw = null;
//新数据代替旧数据,若参数为true,则新数据在旧数据之后
sw = new StreamWriter(“e://temp//aa.txt”,false,System.Text.Encoding.UTF8);
sw.Write(“aaaaaaaaa”);
sw.Close();
向网络写入文本
string str = “aaaaaaaaaaaaa”;
byte[] myByte = System.Text.Encoding.UTF8.GetBytes(str);
NetworkStream netStream = new NetworkStream(mysock);
netStream.Write(myByte,0,myByte.Length);
从文件读出文本
StreamReader sr = null;
sr = new StreamReader(“e://temp//aa.txt”,System.Text.Encoding.UTF8);
string myStr = sr.ReadToEnd();
sr.Close();
从网络读取文本
string str = “aaaaaaaaaaaa”;
byte[] myByte = System.Text.Encoding.UTF8.GetBytes(str);
NetworkStream netStream = new NetStream(mysock);
netStream.Read(myByte,0,myByte.Length);
3.文件流
将网络数据写入文件
FileStream filestream = new FileStream(path,FileMode.OpenOrCreate,FileAccess.Write);
int readNumber = 0;
byte[] bye = new byte[8];
while((readNumber=stream.Read(bye,0,8))>0)
{
filestream.Write(bye,0,readNumber);
filestream.Flush();
}
filestream.Close();
读文件写入网络流
FileStream filestream = new FileStream(path,FileMode.OpenOrCreate,FileAccess.Read);
int number;
byte[] bb = new byte[8];
NetworkStream stream = new NetworkStream(newClient);
while((number = fileStream.Read(bb,0,8))!=0)
{
stream.Write(bb,0,8);
stream.Flush();
bb = new byte[8];
}
filestream.Close();
4. 命令解析
过滤无用字符(命令与参数之间一般以空格<SP>间隔,命令以换行符<CRLF>结束)
byte[] myByte = new Byte[1024];
NetworkStream netStream = new NetworkStream(mysock);
netStream.Read(myByte,0,myByte.Length);
string str = System.Text.Encoding.ASCII.GetString(myByte);
int x = str.Indexof(“/r/n”);
string allcommand = str.Substring(0,x);
char[] a = new char[]{‘ ’};
string commStr = allcommand.Split(a);
string command = commStr[0];
sting parameter1 = commStr[1];
sting parameter2 = commStr[2];
5. 方法参数
params关键字 指定参数数目可变
exam:
private void UseParams(params string[] list)
{
for(int I=0;I<list.Length;i++)
{
textBox1.AppendText(list[i]+”/r/n”);
}
}
out 关键字 主要用于返回多个值,也是传递引用
6. 线程
创建:
Thread thread = new Thread(new ThreadStart(accp));
private void accp()
{
…………….
}
启动:
thread.start();
暂停与重新开始
thread.Sleep(10000)//毫秒
thread.Suspend();//暂停
thread.Resume();
销毁:
thread.Abort();
thread.Join(10000);
thread.Join(10000);
thread.Interrupt();