using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
namespace server
{
class Program
{
Thread threadWatch = null; // 负责监听客户端连接请求的 线程;
Socket socketWatch = null;
Dictionary<string, Socket> dict = new Dictionary<string, Socket>();//存放套接字
Dictionary<string, Thread> dictThread = new Dictionary<string, Thread>();//存放线程
static void Main(string[] args)
{
Program pr = new Program();
pr.startService();
/**********************************************************************************/
Cmd p= new Cmd();
ArrayList strlist = p.ListNetfile("SAP项目文档");
foreach (string str in strlist)
{
Console.WriteLine(str);
}
/************************************************************************************/
string ConsoleLine =Console.ReadLine();
while(true)
{
if ("exit".Equals(ConsoleLine))
{
System.Environment.Exit(0);
}
ConsoleLine = Console.ReadLine();
}
}
void startService()
{
IPEndPoint newipep = new IPEndPoint(IPAddress.Any, 8031);
socketWatch = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
try
{
socketWatch.Bind(newipep);
}
catch (SocketException se)
{
Console.WriteLine("异常:" + se.Message);
return;
}
// 设置监听队列的长度;
socketWatch.Listen(10000);
// 创建负责监听的线程;
threadWatch = new Thread(WatchConnecting);
threadWatch.IsBackground = true;
threadWatch.Start();
Console.WriteLine("服务器启动监听成功!");
}
void WatchConnecting()
{
while (true)
{
// 开始监听客户端连接请求,Accept方法会阻断当前的线程;
Socket sokConnection = socketWatch.Accept(); // 一旦监听到一个客户端的请求,就返回一个与该客户端通信的 套接字;
// 将与客户端连接的 套接字 对象添加到集合中;
Console.WriteLine(sokConnection.RemoteEndPoint.ToString()+"新客户端连接成功!");
dict.Add(sokConnection.RemoteEndPoint.ToString(), sokConnection);
Thread thr = new Thread(RecMsg);
thr.IsBackground = true;
thr.Start(sokConnection);
dictThread.Add(sokConnection.RemoteEndPoint.ToString(), thr); // 将新建的线程 添加 到线程的集合中去。
}
}
void RecMsg(object sokConnectionparn)
{
Socket sokClient = sokConnectionparn as Socket;
while (true)
{
// 定义一个缓存区;
byte[] arrMsgRec = new byte[1024];
// 将接受到的数据存入到输入 arrMsgRec中;
int length = -1;
try
{
length = sokClient.Receive(arrMsgRec); // 接收数据,并返回数据的长度;
if (length > 0)
{
//主业务
string cmdstr = System.Text.Encoding.UTF8.GetString(arrMsgRec).TrimEnd('\0');//一定要加TrimEnd('\0')不然字符串还是原来的字符数组
string[] cmds = cmdstr.Split(' ');
if ("select".Equals(cmds[0]))
{
Cmd cmd= new Cmd();
string fileNames = cmds[1].Trim();
ArrayList result = cmd.ListNetfile(fileNames);
foreach (string str in result)
{
byte[] b= System.Text.Encoding.UTF8.GetBytes(str);
sokClient.Send(b);
Thread.Sleep(500);
Console.WriteLine(str);
}
}
else if ("delete".Equals(cmds[0]))
{
Console.WriteLine(cmds[1]);
Cmd cmd = new Cmd();
string fileNames = cmds[1].Trim();
String result = cmd.CloseShareFile(fileNames);
Console.WriteLine(result);
StringBuilder sb = new StringBuilder();
sb.Append(fileNames);
sb.Append(" ");
if (result.Contains("命令成功完成"))
{
sb.Append("关闭成功");
}
else
{
sb.Append("关闭失败");
}
result = sb.ToString();
byte[] b = System.Text.Encoding.Default.GetBytes(result);
sokClient.Send(b);
Thread.Sleep(10);
}
Thread.Sleep(500);
sokClient.Send(BitConverter.GetBytes(0x04), 1, new SocketFlags());
}
else
{
// 从 通信套接字 集合中删除被中断连接的通信套接字;
dict.Remove(sokClient.RemoteEndPoint.ToString());
// 从通信线程集合中删除被中断连接的通信线程对象;
dictThread.Remove(sokClient.RemoteEndPoint.ToString());
Console.WriteLine("" + sokClient.RemoteEndPoint.ToString() + "断开连接\r\n");
//log.log("遇见异常"+se.Message);
break;
}
}
catch (SocketException se)
{
// 从 通信套接字 集合中删除被中断连接的通信套接字;
dict.Remove(sokClient.RemoteEndPoint.ToString());
// 从通信线程集合中删除被中断连接的通信线程对象;
dictThread.Remove(sokClient.RemoteEndPoint.ToString());
Console.WriteLine("" + sokClient.RemoteEndPoint.ToString() + "断开,异常消息:" + se.Message + "\r\n");
//log.log("遇见异常"+se.Message);
break;
}
catch (Exception e)
{
// 从 通信套接字 集合中删除被中断连接的通信套接字;
dict.Remove(sokClient.RemoteEndPoint.ToString());
// 从通信线程集合中删除被中断连接的通信线程对象;
dictThread.Remove(sokClient.RemoteEndPoint.ToString());
// 从列表中移除被中断的连接IP
Console.WriteLine("异常消息:" + e.Message + "\r\n");
// log.log("遇见异常" + e.Message);
break;
}
}
}
}
}