/*
* WinClientUdpDelFile2; 30 nov, 22
* */
using System; //EventHandler
using [Link]; //Form
using [Link]; //Point
using [Link]; //PrivateFontCollection
using [Link]; //Encoding
using [Link]; //File
using [Link]; //IPAddress, IPEndpoint
using [Link]; //UdpClient
using [Link]; //Sleep
using [Link]; //Ping
using [Link].Drawing2D; //GraphicsPath
using [Link]; //Start
//класс CircularButton
class CircularButton : Button
{
protected override void OnPaint(PaintEventArgs pevent)
{
GraphicsPath gp = new GraphicsPath();
[Link](0, 0, [Link], [Link]);
[Link] = new Region(gp);
[Link](pevent);
}
}
//class My1Form
class My1Form : Form
{
private CircularButton btnSend, btnStart, btnStop;
private Label lb, lbip, lbfolder, lbfile, lbnum;
private TextBox tbox1, tbox2, tbox3, tbox4;
private bool stopReceive;
private Thread rec;
private UdpClient udp;
//1 constructor
public My1Form()
{
//сокет
rec = null;
udp = new UdpClient(15000); //15000 - порт сервера
stopReceive = false;
string pathFonts = "D:\\LIM\\EnglishFonts";
//вставить фонты
PrivateFontCollection pfc = new PrivateFontCollection();
try
{
DirectoryInfo d = new DirectoryInfo(pathFonts);
FileInfo[] fi = [Link]("*.ttf");
foreach (FileInfo file in fi)
{
[Link](pathFonts + "\\" + [Link]);
}
}
catch (DirectoryNotFoundException)
{
[Link]("Folder " + pathFonts + " not found..");
}
//form
ClientSize = new Size(290, 400); //size of my form
Text = "Client";
StartPosition = [Link];
Left = 1330;
Top = 20;
BackColor = [Link](51, 190, 255);
ControlBox = false;
MinimizeBox = false;
MaximizeBox = false;
//button Start
btnStart = new CircularButton();
[Link] = true;
[Link] = "Start";
[Link] = new Font("Comic Sans MS", 11.9F, [Link],
[Link]);
[Link] = new Size(65, 65);
[Link] = 0;
[Link] = [Link];
[Link] = [Link];
[Link] = [Link]; //border color
[Link] = [Link];
[Link] = new Point(30, 310); //button Start coordinates
[Link](btnStart);
[Link] += new EventHandler(btnStart_Click);
//button Send
btnSend = new CircularButton();
[Link] = true;
[Link] = "Send";
[Link] = new Font("Comic Sans MS", 11.9F, [Link],
[Link]);
[Link] = new Size(65, 65);
[Link] = 0;
[Link] = [Link];
[Link] = [Link];
[Link] = [Link]; //border color
[Link] = [Link];
[Link] = new Point(115, 310); //button Send coordinates
[Link](btnSend);
[Link] += new EventHandler(btnSend_Click);
//button Stop
btnStop = new CircularButton();
[Link] = true;
[Link] = "Stop";
[Link] = new Font("Comic Sans MS", 11.9F, [Link],
[Link]);
[Link] = new Size(65, 65);
[Link] = 0;
[Link] = [Link];
[Link] = [Link];
[Link] = [Link]; //border color
[Link] = [Link];
[Link] = new Point(200, 310); //button Stop coordinates
[Link](btnStop);
[Link] += new EventHandler(btnStop_Click);
//label
lb = new Label();
[Link] = true;
//[Link][0]: AlexBrush-Regular
[Link] = new Font([Link][0], 30.5F, [Link]);//0,1,7
[Link] = new Point(5, 1); //coordinates of the question
[Link] = new Size(352, 24);
[Link] = "Protocol UDP";
[Link] = [Link](255, 17, 134);
[Link](lb);
//label ip
lbip = new Label();
[Link] = true;
[Link] = new Font("Ariel", 15, [Link]);
[Link] = new Point(5, 80); //coordinates
[Link] = "ip:";
[Link] = [Link];
[Link](lbip);
//label folder
lbfolder = new Label();
[Link] = true;
[Link] = new Font("Ariel", 15, [Link]);
[Link] = new Point(5, 130); //coordinates
[Link] = new Size(352, 24);
[Link] = "folder:";
[Link] = [Link];
[Link](lbfolder);
//label file
lbfile = new Label();
[Link] = true;
[Link] = new Font("Ariel", 15, [Link]);
[Link] = new Point(5, 180); //coordinates
[Link] = new Size(352, 24);
[Link] = "file:";
[Link] = [Link];
[Link](lbfile);
//label number
lbnum = new Label();
[Link] = true;
[Link] = new Font("Ariel", 15, [Link]);
[Link] = new Point(5, 230); //coordinates
[Link] = new Size(352, 24);
[Link] = "num:";
[Link] = [Link];
[Link](lbnum);
//textbox1
tbox1 = new TextBox();
[Link] = new Point(75, 80); //textbox location
[Link] = new Size(160, 20); //textbox size
[Link] = [Link];
[Link] = [Link]; //font color
Font myfont = new Font("Arial", 16.0f); //font size
[Link] = myfont;
[Link] = [Link];
[Link] = [Link];
[Link] = "[Link]"; //this is a server IP (or [Link] for
cumputer without network)
[Link](tbox1);
//textbox2
tbox2 = new TextBox();
[Link] = new Point(75, 130); //textbox location
[Link] = new Size(160, 20); //textbox size
[Link] = [Link];
[Link] = [Link]; //font color
myfont = new Font("Arial", 16.0f); //font size
[Link] = myfont;
[Link] = [Link];
[Link] = [Link];
[Link] = "";
[Link](tbox2);
//textbox3
tbox3 = new TextBox();
[Link] = new Point(75, 180); //textbox location
[Link] = new Size(160, 20); //textbox size
[Link] = [Link];
[Link] = [Link]; //font color
myfont = new Font("Arial", 16.0f); //font size
[Link] = myfont;
[Link] = [Link];
[Link] = [Link];
[Link] = "";
[Link](tbox3);
//textbox4
tbox4 = new TextBox();
[Link] = new Point(75, 230); //textbox location
[Link] = new Size(160, 20); //textbox size
[Link] = [Link];
[Link] = [Link]; //font color
myfont = new Font("Arial", 16.0f); //font size
[Link] = myfont;
[Link] = [Link];
[Link] = [Link];
[Link] = "";
[Link](tbox4);
}
//2 button1
private void btnSend_Click(object sender, EventArgs e)
{
if (IsConnectedToInternet())
{
string ipAddress = [Link]; //this is a server ip
int port = 15000; //this is a server port
string nameOfFolder = [Link]; //this is a name of the folder
string nameOfFile = [Link]; //this is a name of the file
string num = [Link]; //this is a number
//send a name of the folder to server
int theNumberOfBytes = udpSend(ipAddress, port, nameOfFolder + "&" +
nameOfFile + "&" + num);
}
else
[Link]("Check your internet connection..");
}
//3 button Start
private void btnStart_Click(object sender, EventArgs e)
{
stopReceive = false;
rec = new Thread(new ThreadStart(ReceiveMessage));
[Link]();
[Link] = "Started listening";
[Link] = new [Link](20, 15); //coordinates of the
question
}
//4 button Stop
private void btnStop_Click(object sender, EventArgs e)
{
[Link] = "Stop";
[Link] = new [Link](100, 15);
stopReceive = true;
if (udp != null) [Link]();
if (rec != null) [Link]();
Close();
}
//5 udpSend
private int udpSend(string ipAddress, int port, string myMessage)
{
byte[] myMessageBytes = [Link](myMessage);
UdpClient udp = new UdpClient();
IPAddress ipad = [Link](ipAddress);
IPEndPoint ipen = new IPEndPoint(ipad, port);
int send = [Link](myMessageBytes, [Link], ipen);
[Link]();
return send;
}
//6 IsConnectedToInternet
public bool IsConnectedToInternet()
{
try
{
Ping myPing = new Ping();
String host = "[Link]";
byte[] buffer = new byte[32];
int timeout = 1000;
PingOptions pingOptions = new PingOptions();
PingReply reply = [Link](host, timeout, buffer, pingOptions);
return ([Link] == [Link]);
}
catch (Exception)
{
return false;
}
}
//7. функция ReceiveMessage для передачи процессу
void ReceiveMessage()
{
try
{
while (true)
{
//получаем данные от servera
IPEndPoint ipendpoint = null;
byte[] message = [Link](ref
ipendpoint);//[Link]() = [Link] - server ip
//for example, messFromServer = ""שם של הקובץ קיבלתי
string messFromServer = "";
for (int i = 0; i < [Link]; i++)
messFromServer += (char)message[i];
if (messFromServer[0] == '<')
{
[Link]("D:\\LIM\\" + "[Link]",
[Link](message)); //стирание старого и запись нового
[Link]("D:\\LIM\\[Link]");
}
else
[Link](messFromServer);
if (stopReceive == true) break;
}
}
catch (Exception e)
{
[Link]([Link]);
}
}
//8 Main
[STAThread]
static void Main()
{
My1Form f = new My1Form();
[Link](f);
}
}