static void Main()
{
Application.Run(new Form1());
} private void button1_Click(object sender, System.EventArgs e)
{ int port=0;
IPAddress myIP=IPAddress.Parse("127.0.0.1");
try
{
myIP=IPAddress.Parse(textBox2.Text);}
catch{MessageBox.Show("您输入的IP地址格式不正确!");}
client =new TcpClient();
try{
port=Int32.Parse(textBox3.Text);
}
catch{MessageBox.Show("请输入整数。");}
try
{
if(textBox1.Text!=""&&textBox2.Text=="")
{
client.Connect(textBox1.Text,port);
statusBarPanel1.Text="与服务器建立连接";
netStream=client.GetStream();
byte[] bb=new byte[6400];
i=netStream.Read(bb,0,6400);
string ss=System.Text.Encoding.BigEndianUnicode.GetString(bb);
richTextBox1.AppendText(ss);
int j=richTextBox1.Lines.Length;
for(int k=0;k<j-1;k++)
{
comboBox1.Items.Add(richTextBox1.Lines[k]);
}
comboBox1.Text=comboBox1.Items[0].ToString();
}
if(textBox2.Text!=""&&textBox1.Text==""){
client.Connect(myIP,port);
statusBarPanel1.Text="与服务器建立连接";
netStream=client.GetStream();
byte[] bb=new byte[6400];
int i=netStream.Read(bb,0,6400);
string ss=System.Text.Encoding.BigEndianUnicode.GetString(bb);
richTextBox1.AppendText(ss);
int j=richTextBox1.Lines.Length;
for(int k=0;k<j-1;k++)
{
comboBox1.Items.Add(richTextBox1.Lines[k]);
}
comboBox1.Text=comboBox1.Items[0].ToString();
} if(textBox2.Text!=""&&textBox1.Text!=""){
client.Connect(myIP,port);
statusBarPanel1.Text="与服务器建立连接";
netStream=client.GetStream();
byte[] bb=new byte[6400];
int i=netStream.Read(bb,0,6400);
string ss=System.Text.Encoding.BigEndianUnicode.GetString(bb);
richTextBox1.AppendText(ss);
int j=richTextBox1.Lines.Length;
for(int k=0;k<j-1;k++)
{
comboBox1.Items.Add(richTextBox1.Lines[k]);
}
comboBox1.Text=comboBox1.Items[0].ToString();
}
}
catch(Exception ee){MessageBox.Show(ee.Message);}
} private void button2_Click(object sender, System.EventArgs e)
{
try
{netStream=client.GetStream();
string clo="@@@@@@"+"\r\n";
byte[] by=System.Text.Encoding.BigEndianUnicode.GetBytes(clo.ToCharArray());
netStream.Write(by,0,by.Length);
netStream.Flush(); client.Close();
statusBarPanel1.Text="与服务器断开连接";
}
catch{} } private void button3_Click(object sender, System.EventArgs e)
{
if(saveFileDialog1.ShowDialog()==DialogResult.OK)
{ //构造新的文件流
filestream=new FileStream(saveFileDialog1.FileName,FileMode.OpenOrCreate,FileAccess.Write);
//获取服务器网络流
netStream=client.GetStream();
string down=comboBox1.Text+"\r\n";
byte[] by=System.Text.Encoding.BigEndianUnicode.GetBytes(down.ToCharArray());
//向服务器发送要下载的文件名
netStream.Write(by,0,by.Length);
//刷新流
netStream.Flush();
//启动接收文件的线程
Thread thread=new Thread(new ThreadStart(download));
thread.Start();
}//对应if(saveFileDialog1.ShowDialog()==DialogResult.OK)
}
private void download()
{
down(ref netStream);
}
private void down(ref NetworkStream stream)
{
int length=1024;
byte[] bye=new byte[1024]; int tt=stream.Read(bye,0,length);
//下行循环读取网络流并写进文件
while(tt>0)
{
string ss=System.Text.Encoding.ASCII.GetString(bye);
int x=ss.IndexOf("<EOF>");
if(x!=-1)
{
filestream.Write(bye,0,x);
filestream.Flush();
break;
}
else
{
filestream.Write(bye,0,tt);
filestream.Flush();
}
tt=stream.Read(bye,0,length);
}//对用于while(!control)的“{”
filestream.Close();
MessageBox.Show("下载完毕!");
}
//是不是文件流写的时候就是 BMP格式呢
//saveFileDialog1.FileName 看他的格式是不是BMP