blondedude092

ClientMainForm.cs

Feb 1st, 2016
29
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.52 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using System.Net;
  11. using System.Net.Sockets;
  12. using System.IO;
  13.  
  14. namespace Client
  15. {
  16. public partial class ClientMainForm : Form
  17. {
  18. TcpClient client = new TcpClient();
  19. public ClientMainForm()
  20. {
  21. InitializeComponent();
  22.  
  23. Connect();
  24. }
  25.  
  26. private void Connect()
  27. {
  28.  
  29. //try to connect since it's not connected...
  30. try
  31. {
  32. client = new TcpClient("127.0.0.1", 3030);
  33. client.GetStream().BeginRead(new Byte[] {0}, 0, 0, new AsyncCallback(Read), null);
  34.  
  35. }
  36. catch (Exception)
  37. {
  38. //wait 1 second till trying again
  39. System.Threading.Thread.Sleep(1000);
  40. Connect();
  41. }
  42.  
  43. }
  44.  
  45. private void Read(IAsyncResult ar)
  46. {
  47. try
  48. {
  49. using (NetworkStream stream = client.GetStream())
  50. {
  51. if (stream.CanRead)
  52. {
  53. byte[] buffer = new byte[client.ReceiveBufferSize];
  54. int bytesRead = stream.Read(buffer, 0, buffer.Length);
  55. //reads the buffer as a string for a command...
  56. string message = Encoding.UTF8.GetString(buffer);
  57. Console.WriteLine("data from client: " + message);
  58.  
  59. string[] messageParts = message.Split(Shared.Separator);
  60.  
  61. switch (messageParts[0])
  62. {
  63. case Shared.Message:
  64. Console.WriteLine(messageParts[1]);
  65. //MessageBox.Show(messageParts[1]);
  66. break;
  67. case Shared.FileRecieve:
  68. Console.WriteLine("FileRecieve Message");
  69. FileRecieve(messageParts[1], messageParts[2], buffer);
  70.  
  71. break;
  72.  
  73. }
  74. }
  75.  
  76.  
  77. }
  78.  
  79.  
  80.  
  81. client.GetStream().BeginRead(new byte[] { 0 }, 0, 0, Read, null);
  82.  
  83. }
  84. catch (Exception)
  85. {
  86. //error occured tell the server that this client exited...
  87. Connect();
  88. }
  89. }
  90.  
  91.  
  92. private void FileRecieve(string FileName, string FileSize, byte[] InitialBuffer)
  93. {
  94. NetworkStream NS = client.GetStream();
  95.  
  96. // Check to see if this NetworkStream is readable.
  97. if (NS.CanRead)
  98. {
  99.  
  100. byte[] fileInfo = new byte[FileName.Length + FileSize.Length + 2];
  101.  
  102. byte[] myReadBuffer = new byte[1024];
  103. byte[] myCompleteMessage = new byte[Convert.ToInt32(FileSize)];
  104. int numberOfBytesRead = 0;
  105. int totalBytesRead = 0;
  106.  
  107. //gets the initial buffer file data and puts it into the completed message...
  108. Array.ConstrainedCopy(InitialBuffer, fileInfo.Length, myCompleteMessage, 0, InitialBuffer.Length - fileInfo.Length);
  109. totalBytesRead += InitialBuffer.Length - fileInfo.Length;
  110. // Incoming message may be larger than the buffer size.
  111.  
  112. do
  113. {
  114. numberOfBytesRead = NS.Read(myReadBuffer, 0, myReadBuffer.Length);
  115. totalBytesRead += numberOfBytesRead;
  116. Array.ConstrainedCopy(myReadBuffer, 0, myCompleteMessage, totalBytesRead, myReadBuffer.Length);
  117.  
  118. }
  119. while (NS.DataAvailable);
  120.  
  121. // Print out the received message to the console.
  122. Console.WriteLine("You received the following message : " + myCompleteMessage);
  123. FileStream FS = new FileStream(FileName, FileMode.Create);
  124. FS.Write(myCompleteMessage, 0, myCompleteMessage.Length);
  125.  
  126. }
  127. else
  128. {
  129. Console.WriteLine("Sorry. You cannot read from this NetworkStream.");
  130. }
  131. }
  132. }
  133. }
Advertisement
Add Comment
Please, Sign In to add comment