Advertisement
Kapshtag

C# dsik kom cl

May 14th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.42 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using System.Threading;
  4. using System.Text;
  5. using System.Net;
  6. using System.Net.Sockets;
  7. using System.Threading.Tasks;
  8. using System.Windows.Forms;
  9.  
  10. namespace DSIKKomunikator
  11. {
  12. public partial class Form2 : Form
  13. {
  14. private void Form2_Load(object sender, EventArgs e)
  15. {
  16.  
  17. }
  18. private void ChatBox_TextChanged(object sender, EventArgs e)
  19. {
  20.  
  21. }
  22. private void Form2_FormClosing(object sender, FormClosingEventArgs e)
  23. {
  24. }
  25. private void listEmotes_ItemCheck(object sender, ItemCheckEventArgs e)
  26. {
  27.  
  28. }
  29. public int rectime = 0;
  30. public Form2() //ladowanie forma
  31. {
  32. InitializeComponent();
  33. this.Height = 437;
  34. listEmotes.Height = 0;
  35. ConnectAsync(IPAddress.Parse(Form1.server), Form1.port);
  36. var lines = File.ReadLines("C:\\APP\\emotes.txt");
  37. foreach(var line in lines)
  38. {
  39. listEmotes.Items.Add(line);
  40. }
  41. }
  42. private TcpClient _tcpClient = new TcpClient();
  43. Task task;
  44.  
  45. private async void ConnectAsync(IPAddress ipadress, int port) //laczenie
  46. {
  47. await _tcpClient.ConnectAsync(ipadress, port);
  48. ChatBox.Text += "Podล‚ฤ…czony do serwera." + Environment.NewLine;
  49. task = new Task(Receive);
  50. task.Start();
  51. }
  52. private void SendMsg() //funkcja wysylania
  53. {
  54. byte[] toSend = Encoding.Unicode.GetBytes(Form1.username + ":" + Environment.NewLine + MsgBox.Text);
  55. _tcpClient.Client.Send(toSend);
  56. MsgBox.Text = "";
  57. }
  58. private void button1_Click(object sender, EventArgs e)//przycisk wysylania
  59. {
  60. SendMsg();
  61. }
  62. private void Receive() //odbieranie
  63. {
  64. while (true) {
  65. try {
  66. byte[] rec = new byte[_tcpClient.ReceiveBufferSize];
  67. _tcpClient.Client.Receive(rec);
  68. string temp = Encoding.UTF8.GetString(rec);
  69. if (temp.Contains("PNG"))
  70. {
  71. /*using(FileStream fileStream = new FileStream("C:\\APP\\img.png", FileMode.OpenOrCreate))
  72. {
  73. for(int i = 0; i < rec.Length; i++)
  74. {
  75. fileStream.WriteByte(rec[i]);
  76. }
  77. }*/
  78. File.WriteAllBytes("C:\\APP\\img.png", rec);
  79. RecImgBox.SizeMode = PictureBoxSizeMode.Zoom;
  80. RecImgBox.ImageLocation = "C:\\APP\\img.png";
  81. }
  82. else
  83. {
  84. temp = Encoding.Unicode.GetString(rec);
  85. this.Invoke((MethodInvoker)delegate { ChatBox.Text += Environment.NewLine; ChatBox.AppendText(temp); });
  86. }
  87. //Thread.Sleep(100);
  88. //Receive();
  89. }
  90. catch (Exception e)
  91. {
  92. Console.WriteLine("Eล‚oล‚ " + e.Message);
  93. }
  94. }
  95. }
  96.  
  97. private void MsgBox_KeyDown(object sender, KeyEventArgs e) //enter aby wyslac
  98. {
  99. if(e.KeyCode == Keys.Enter)
  100. {
  101. SendMsg();
  102. }
  103. }
  104.  
  105. private void button2_Click(object sender, EventArgs e) // rozsuwanie panelu dolnego
  106. {
  107. if (this.Height < 515)
  108. {
  109. this.Height = 515;
  110. listEmotes.Height = 75;
  111. }
  112. else
  113. {
  114. this.Height = 437;
  115. listEmotes.Height = 0;
  116. }
  117. }
  118.  
  119. private void listEmotes_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e) //wybieranie emotki
  120. {
  121. foreach (ListViewItem item in listEmotes.SelectedItems)
  122. {
  123. MsgBox.Text += item.Text;
  124. }
  125. listEmotes.SelectedItems.Clear();
  126. }
  127.  
  128. private void button3_Click(object sender, EventArgs e) //otwarcie i wyslanie pliku
  129. {
  130. OpenFileDialog ofile = new OpenFileDialog();
  131. ofile.Filter = "PNG Files(*.png)|*.png";
  132. ofile.RestoreDirectory = true;
  133. if(ofile.ShowDialog() == DialogResult.OK)
  134. {
  135. FileInfo fi = new FileInfo(ofile.FileName);
  136. if(fi.Length < 64000) {
  137. _tcpClient.Client.SendFile(ofile.FileName);
  138. }
  139. else
  140. {
  141. MessageBox.Show("Plik jest za duzy! Max 64Kb");
  142. }
  143. //_tcpClient.Client.SendFile(ofile.FileName);
  144.  
  145. }
  146. }
  147.  
  148. private void button4_Click(object sender, EventArgs e) //wylogowanie
  149. {
  150. _tcpClient.Close();
  151. Application.Exit();
  152.  
  153. }
  154.  
  155. private void button5_Click(object sender, EventArgs e) //rozsuwanie panelu w bok
  156. {
  157. if(this.Width < 1107)
  158. {
  159. this.Width = 1107;
  160. }
  161. else
  162. {
  163. this.Width = 685;
  164. }
  165. }
  166. }
  167. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement