1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
| public partial class Form1 : Form
{
string CorS;
public Form1()
{
InitializeComponent();
}
private TcpListener Server;
private TcpClient remoteClient;
private TcpClient localClient;
private Thread thClient;
private Thread thServer;
private void btnServer_Click(object sender, EventArgs e)
{
Server = new TcpListener(IPAddress.Any, 6785);
Server.Start();
thServer = new Thread(() =>
{
remoteClient = Server.AcceptTcpClient();
byte[] buffer = new byte[1024];
while (remoteClient.Connected)
{
Array.Clear(buffer, 0, buffer.Length);
remoteClient.GetStream().Read(buffer, 0, 1024);
string strData = Encoding.Default.GetString(buffer);
}
});
thServer.Start();
CorS = "s";
ClientServer();
}
private void btnConnect_Click(object sender, EventArgs e)
{
localClient = new TcpClient();
System.Net.IPAddress ip = System.Net.Dns.GetHostByName(System.Net.Dns.GetHostName()).AddressList[0];
localClient.Connect(ip, 8000);// выдает ошибку "подключение не удалось"
thClient = new Thread(() =>
{
while (localClient.Connected)
{
byte[] buffer = new byte[1024];
while (localClient.Connected)
{
Array.Clear(buffer, 0, buffer.Length);
localClient.GetStream().Read(buffer, 0, 1024);
string strData = Encoding.Default.GetString(buffer);
}
}
});
thClient.Start();
CorS = "c";
ClientServer();
}
#region //Клиент сервер
public void ClientServer()
{
if (CorS == "c")
{
//Если подключен клиент то выводить файлы и папки с компьютера-сервера
}
//else if (CorS == "s")
//{
//}
}
#endregion
#region //ВЫВОД ФАЙЛОВ
public static string FPath;
public static string backPath;
List<string> ls = new List<string>();
public static string GetLocDir()
{
String[] LogicalDrives = Environment.GetLogicalDrives();
string dir = String.Join("/", LogicalDrives);
return dir;
}
public static string GetDirectories()
{
string[] dir = Directory.GetDirectories(FPath);
string poddir = String.Join("/", dir);
return poddir;
}
public static string GetFiles()
{
string[] files = Directory.GetFiles(FPath);
string file = String.Join("/", files);
return file;
}
public void listView1_ItemActivate(object sender, EventArgs e)
{
// if (listView1.SelectedItems.Count == 0)
// return;
// ListViewItem item = listView1.SelectedItems[0];
// if (item.ImageIndex == 1)
// {
// string it = item.Text;
// string title = "";
// foreach (string s in ls)
// {
// try
// {
// if (s.Substring(s.Length - it.Length, it.Length) == it)
// {
// FPath = s;
// title = s;
// }
// }
// catch { }
// }
// try
// {
// string[] dirs = Directory.GetDirectories(FPath);
// this.Text = title;
// GetFiles();
// }
// catch (Exception ex) { MessageBox.Show(ex.Message); }
// }
// else if (item.ImageIndex == 0)
// {
// string start = this.Text + "" + item.Text;
// System.Diagnostics.Process.Start(start);
// }
}
#endregion
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
if (Server != null) Server.Stop();
if (remoteClient != null && remoteClient.Connected) remoteClient.Close();
if (localClient != null && localClient.Connected) localClient.Close();
}
private void btnDownload_Click(object sender, EventArgs e)
{
//загрузка выбранного файла
}
} |