Posted on 2009-07-16 13:23
Hero 阅读(222)
评论(0) 编辑 收藏 引用 所属分类:
C#积累
1dir#region dir
2using System;
3using System.Collections.Generic;
4using System.ComponentModel;
5using System.Data;
6using System.Drawing;
7using System.Text;
8using System.Windows.Forms;
9
10using System.IO;
11using System.Net;
12using System.Net.Sockets;
13#endregion
14
15namespace Client
16{
17 public partial class PictureClient : Form
18 {
19 public PictureClient()
20 {
21 InitializeComponent();
22 }
23
24 private void button_ListPicture_Click( object sender, EventArgs e )
25 {
26 const int bufferSize = 4096;
27 //连接服务器
28 TcpClient client = new TcpClient();
29 IPHostEntry host = Dns.GetHostEntry( Properties.Settings.Default.Server );
30 client.Connect( host.AddressList[0], Properties.Settings.Default.Port );
31
32 //给服务器发送一个请求
33 NetworkStream clientStream = client.GetStream();
34 string request = "LIST";
35 byte[] requestBuffer = Encoding.ASCII.GetBytes(( request ) );
36
37 clientStream.Write( requestBuffer, 0, requestBuffer.Length ) ;
38
39 //从服务器读取响应
40 byte[] responseBuffer = new byte[bufferSize] ;
41 MemoryStream memstream = new MemoryStream() ;
42 int bytesRead = 0 ;
43
44 do
45 {
46 bytesRead = clientStream.Read( responseBuffer, 0, bufferSize ) ;
47 memstream.Write( responseBuffer, 0, bytesRead ) ;
48 } while ( bytesRead > 0 );
49
50 clientStream.Close();
51 client.Close();
52
53 byte[] buffer = memstream.GetBuffer();
54 string response = Encoding.ASCII.GetString( buffer );
55 string[] filenames = response.Split( ':' );
56
57 //为listBox设置源
58 this.listBox_list.DataSource = filenames;
59 }
60
61 private void button_GetPicture_Click( object sender, EventArgs e )
62 {
63 const int bufferSize = 4096;
64
65 //连接服务器
66 TcpClient client = new TcpClient();
67 IPHostEntry host = Dns.GetHostEntry( Properties.Settings.Default.Server );
68 client.Connect( host.AddressList[0], Properties.Settings.Default.Port );
69
70 //创建网络流
71 NetworkStream clientStream = client.GetStream();
72
73 //向服务器发出请求
74 string request = "FILE:" + this.listBox_list.SelectedValue.ToString();
75 byte[] requestBuffer = Encoding.ASCII.GetBytes( request );
76 clientStream.Write( requestBuffer, 0, requestBuffer.Length );
77
78 byte[] responseBuffer = new byte[bufferSize] ;
79 MemoryStream memstream = new MemoryStream();
80 int bytesRead = 0;
81
82 do
83 {
84 bytesRead = clientStream.Read( responseBuffer, 0, bufferSize );
85 //写入内存中
86 memstream.Write( responseBuffer, 0, bytesRead );
87 } while ( bytesRead > 0 );
88
89 clientStream.Close();
90 client.Close();
91
92 pictureBox_Picture.Image = Image.FromStream( memstream );
93 }
94
95 private void button_GetPicture_Click_1( object sender, EventArgs e )
96 {
97
98 }
99 }
100}