Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.IO;
- using System.Threading;
- using System.Text;
- using System.Net;
- using System.Net.Sockets;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- namespace DSIKKomunikator
- {
- public partial class Form2 : Form
- {
- private void Form2_Load(object sender, EventArgs e)
- {
- }
- private void ChatBox_TextChanged(object sender, EventArgs e)
- {
- }
- private void Form2_FormClosing(object sender, FormClosingEventArgs e)
- {
- }
- private void listEmotes_ItemCheck(object sender, ItemCheckEventArgs e)
- {
- }
- public int rectime = 0;
- public Form2() //ladowanie forma
- {
- InitializeComponent();
- this.Height = 437;
- listEmotes.Height = 0;
- ConnectAsync(IPAddress.Parse(Form1.server), Form1.port);
- var lines = File.ReadLines("C:\\APP\\emotes.txt");
- foreach(var line in lines)
- {
- listEmotes.Items.Add(line);
- }
- }
- private TcpClient _tcpClient = new TcpClient();
- Task task;
- private async void ConnectAsync(IPAddress ipadress, int port) //laczenie
- {
- await _tcpClient.ConnectAsync(ipadress, port);
- ChatBox.Text += "Podลฤ czony do serwera." + Environment.NewLine;
- task = new Task(Receive);
- task.Start();
- }
- private void SendMsg() //funkcja wysylania
- {
- byte[] toSend = Encoding.Unicode.GetBytes(Form1.username + ":" + Environment.NewLine + MsgBox.Text);
- _tcpClient.Client.Send(toSend);
- MsgBox.Text = "";
- }
- private void button1_Click(object sender, EventArgs e)//przycisk wysylania
- {
- SendMsg();
- }
- private void Receive() //odbieranie
- {
- while (true) {
- try {
- byte[] rec = new byte[_tcpClient.ReceiveBufferSize];
- _tcpClient.Client.Receive(rec);
- string temp = Encoding.UTF8.GetString(rec);
- if (temp.Contains("PNG"))
- {
- /*using(FileStream fileStream = new FileStream("C:\\APP\\img.png", FileMode.OpenOrCreate))
- {
- for(int i = 0; i < rec.Length; i++)
- {
- fileStream.WriteByte(rec[i]);
- }
- }*/
- File.WriteAllBytes("C:\\APP\\img.png", rec);
- RecImgBox.SizeMode = PictureBoxSizeMode.Zoom;
- RecImgBox.ImageLocation = "C:\\APP\\img.png";
- }
- else
- {
- temp = Encoding.Unicode.GetString(rec);
- this.Invoke((MethodInvoker)delegate { ChatBox.Text += Environment.NewLine; ChatBox.AppendText(temp); });
- }
- //Thread.Sleep(100);
- //Receive();
- }
- catch (Exception e)
- {
- Console.WriteLine("Eลoล " + e.Message);
- }
- }
- }
- private void MsgBox_KeyDown(object sender, KeyEventArgs e) //enter aby wyslac
- {
- if(e.KeyCode == Keys.Enter)
- {
- SendMsg();
- }
- }
- private void button2_Click(object sender, EventArgs e) // rozsuwanie panelu dolnego
- {
- if (this.Height < 515)
- {
- this.Height = 515;
- listEmotes.Height = 75;
- }
- else
- {
- this.Height = 437;
- listEmotes.Height = 0;
- }
- }
- private void listEmotes_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e) //wybieranie emotki
- {
- foreach (ListViewItem item in listEmotes.SelectedItems)
- {
- MsgBox.Text += item.Text;
- }
- listEmotes.SelectedItems.Clear();
- }
- private void button3_Click(object sender, EventArgs e) //otwarcie i wyslanie pliku
- {
- OpenFileDialog ofile = new OpenFileDialog();
- ofile.Filter = "PNG Files(*.png)|*.png";
- ofile.RestoreDirectory = true;
- if(ofile.ShowDialog() == DialogResult.OK)
- {
- FileInfo fi = new FileInfo(ofile.FileName);
- if(fi.Length < 64000) {
- _tcpClient.Client.SendFile(ofile.FileName);
- }
- else
- {
- MessageBox.Show("Plik jest za duzy! Max 64Kb");
- }
- //_tcpClient.Client.SendFile(ofile.FileName);
- }
- }
- private void button4_Click(object sender, EventArgs e) //wylogowanie
- {
- _tcpClient.Close();
- Application.Exit();
- }
- private void button5_Click(object sender, EventArgs e) //rozsuwanie panelu w bok
- {
- if(this.Width < 1107)
- {
- this.Width = 1107;
- }
- else
- {
- this.Width = 685;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement