Guest User

Untitled

a guest
Jan 22nd, 2018
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.20 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using System.Net.Sockets;
  4. using System.Windows.Forms;
  5.  
  6. namespace TCPchatic
  7. {
  8. public partial class Form1 : Form
  9. {
  10. TcpClient client = new TcpClient();
  11. TcpListener server = new TcpListener(System.Net.IPAddress.Any, 20113);
  12. delegate void RecieveTextDelegate(string text);
  13. RecieveTextDelegate param;
  14. public Form1()
  15. {
  16. InitializeComponent();
  17. server.Start();
  18. param = new RecieveTextDelegate(textBoxChat.AppendText);
  19. BeginAcceptTcpClient();
  20. }
  21. void BeginAcceptTcpClient()
  22. {
  23. server.BeginAcceptTcpClient(x =>
  24. {
  25. var remoteClient = server.EndAcceptTcpClient(x);
  26. BeginAcceptTcpClient();
  27. var from = remoteClient.Client.RemoteEndPoint.ToString();
  28. var binReader = new BinaryReader(remoteClient.GetStream());
  29. while (remoteClient.Connected)
  30. {
  31. var type = (MsgType)binReader.ReadByte();
  32. switch (type)
  33. {
  34. case MsgType.Text:
  35. var mtext = new MsgText(binReader);
  36. this.Invoke(param, new object[] { from + ": " + mtext.Text + "rn" });
  37. break;
  38. case MsgType.File:
  39. var mFile = new MsgFile(binReader);
  40. this.Invoke(param, new object[] { string.Format("{0}: файл "{1}" Размер {2}б rn", from, mFile.Name, mFile.FileLen) });
  41. if (MessageBox.Show(string.Format("{0} отправил файл "{1}", размер {2} б. Принять?", from, mFile.Name, mFile.FileLen), "Прием файла", MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes)
  42. {
  43. var saveDialog = new SaveFileDialog();
  44. saveDialog.FileName = mFile.Name;
  45. if (saveDialog.ShowDialog() == DialogResult.OK)
  46. {
  47. using (var file = saveDialog.OpenFile())
  48. {
  49. new BinaryWriter(file).Write(mFile.File);
  50. }
  51. }
  52. }
  53. break;
  54. }
  55. }
  56. }, null);
  57. }
  58.  
  59. private void button1_Click(object sender, EventArgs e)
  60. {
  61. try
  62. {
  63. if (client.Connected) client.Close();
  64. client.Connect(textBoxIP.Text, 20113);
  65. }
  66. catch (Exception ex)
  67. {
  68. MessageBox.Show(ex.Message);
  69. }
  70. }
  71.  
  72. private void button2_Click(object sender, EventArgs e)
  73. {
  74. new BinaryWriter(client.GetStream()).Write((byte)MsgType.Text);
  75. new BinaryWriter(client.GetStream()).Write(textBoxMsg.Text);
  76. textBoxChat.AppendText("Я: " + textBoxMsg.Text + " rn");
  77. textBoxMsg.Clear();
  78. }
  79.  
  80. private void button3_Click(object sender, EventArgs e)
  81. {
  82. var openDialog = new OpenFileDialog();
  83. if (openDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  84. {
  85. using(var file = openDialog.OpenFile())
  86. {
  87. var mFile = new MsgFile()
  88. {
  89. Name = openDialog.SafeFileName,
  90. FileLen = (int)file.Length,
  91. File = new BinaryReader(file).ReadBytes((int)file.Length)
  92. };
  93. mFile.Write(client.GetStream());
  94. }
  95. }
  96. }
  97. enum MsgType: byte
  98. {
  99. Text,
  100. File
  101. }
  102. struct MsgText
  103. {
  104. public string Text;
  105. public MsgText(BinaryReader br)
  106. {
  107. Text = br.ReadString();
  108. }
  109. }
  110. struct MsgFile
  111. {
  112. public string Name;
  113. public int FileLen;
  114. public byte[] File;
  115.  
  116. public MsgFile(BinaryReader br)
  117. {
  118. Name = br.ReadString();
  119. FileLen = br.ReadInt32();
  120. File = br.ReadBytes(FileLen);
  121. }
  122. public void Write(Stream s)
  123. {
  124. var bw = new BinaryWriter(s);
  125. bw.Write((byte)MsgType.File);
  126. bw.Write(Name);
  127. bw.Write(FileLen);
  128. bw.Write(File);
  129. }
  130. }
  131. }
  132. }
Add Comment
Please, Sign In to add comment