Posted on 2009-07-16 13:14
Hero 阅读(483)
评论(0) 编辑 收藏 引用 所属分类:
C#积累
1
using System;
2
using System.Collections.Generic;
3
using System.Text;
4
using System.IO;
5
6
namespace Server
7

{
8
class PictureHelper
9
{
10
public static string[] GetFileList()
11
{
12
//注意得到的是完整的路径名称
13
string[] files = Directory.GetFiles( Properties.Settings.Default.PictureDirectory );
14
15
//remove the directory path from the filename
16
for ( int i = 0; i < files.Length; i++ )
17
{
18
files[i] = Path.GetFileName( files[i] );
19
}
20
21
return files;
22
}
23
24
//返回图片文件的字节数组
25
public static byte[] GetPictureBytes( string filename )
26
{
27
//通过FileInfo来获得文件的长度
28
FileInfo fileInfo = new FileInfo( filename );
29
byte[] buffer = new byte[fileInfo.Length];
30
using ( FileStream fileStream = fileInfo.OpenRead() )
31
{
32
fileStream.Read( buffer, 0, buffer.Length );
33
}
34
35
return buffer;
36
}
37
38
//在字节数组中返回文件列表
39
public static byte[] GetFileListBytes()
40
{
41
string[] files = PictureHelper.GetFileList();
42
43
StringBuilder responseMessage = new StringBuilder();
44
45
foreach ( string str in files )
46
{
47
responseMessage.Append( str );
48
responseMessage.Append( ":" );
49
}
50
51
byte[] responseBuffer = Encoding.ASCII.GetBytes( responseMessage.ToString() );
52
53
return responseBuffer;
54
}
55
}
56
}
57
1
using System;
2
using System.Collections.Generic;
3
using System.Windows.Forms;
4
using System.Net;
5
using System.Net.Sockets;
6
using System.IO;
7
8
namespace Server
9

{
10
static class Program
11
{
12
/**//// <summary>
13
/// 应用程序的主入口点。
14
/// </summary>
15
[STAThread]
16
static void Main()
17
{
18
//Application.EnableVisualStyles();
19
//Application.SetCompatibleTextRenderingDefault( false );
20
//Application.Run( new Form1() );
21
22
//新建一个监听器
23
TcpListener listener = new TcpListener(IPAddress.Any, Properties.Settings.Default.Port);
24
25
listener.Start();//开始监听
26
27
while ( true )
28
{
29
const int bufferSize = 256;
30
31
//创建一个客户端
32
TcpClient client = listener.AcceptTcpClient();
33
//从客户端获得网络流
34
NetworkStream stream = client.GetStream();
35
36
//从客户端网络流中读取查询数据
37
byte[] buffer = new byte[bufferSize];
38
//记录实际获得的字节数
39
int readBytes = 0;
40
readBytes = stream.Read( buffer, 0, bufferSize );
41
//获得查询字符串
42
string request = (System.Text.Encoding.ASCII.GetString( buffer )).Substring( 0, readBytes );
43
44
if ( request.StartsWith( "LIST" ) )
45
{//如果请求的是列表
46
47
byte[] responseBuffer = PictureHelper.GetFileListBytes();
48
49
stream.Write( responseBuffer, 0, responseBuffer.Length );
50
}
51
else if ( request.StartsWith( "FILE" ) )
52
{//如果请求的是文件
53
54
//获得要下载的文件名字
55
string[] requestMessage = request.Split( ':' ) ;
56
string filename = requestMessage[1];
57
58
//string curpath = (( Properties.Settings.Default.PictureDirectory ).ToString() + @"\" + filename );
59
//curpath = @"D:\Picture\" + filename ;
60
//byte[] data = File.ReadAllBytes( curpath );
61
62
string curpath = Path.Combine( Properties.Settings.Default.PictureDirectory, filename );
63
MessageBox.Show( curpath.ToString() );
64
//从绝对路径中获取文件的全部字节流
65
byte[] data = File.ReadAllBytes( curpath );
66
67
//将图片发送给客户端
68
stream.Write( data, 0, data.Length );
69
}
70
71
//不要忘记关闭流
72
stream.Close();
73
client.Close();
74
}
75
}
76
}
77
}