Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- using System.Net;
- using System.Net.Sockets;
- using System.IO;
- namespace Client
- {
- public partial class ClientMainForm : Form
- {
- TcpClient client = new TcpClient();
- public ClientMainForm()
- {
- InitializeComponent();
- Connect();
- }
- private void Connect()
- {
- //try to connect since it's not connected...
- try
- {
- client = new TcpClient("127.0.0.1", 3030);
- client.GetStream().BeginRead(new Byte[] {0}, 0, 0, new AsyncCallback(Read), null);
- }
- catch (Exception)
- {
- //wait 1 second till trying again
- System.Threading.Thread.Sleep(1000);
- Connect();
- }
- }
- private void Read(IAsyncResult ar)
- {
- try
- {
- using (NetworkStream stream = client.GetStream())
- {
- if (stream.CanRead)
- {
- byte[] buffer = new byte[client.ReceiveBufferSize];
- int bytesRead = stream.Read(buffer, 0, buffer.Length);
- //reads the buffer as a string for a command...
- string message = Encoding.UTF8.GetString(buffer);
- Console.WriteLine("data from client: " + message);
- string[] messageParts = message.Split(Shared.Separator);
- switch (messageParts[0])
- {
- case Shared.Message:
- Console.WriteLine(messageParts[1]);
- //MessageBox.Show(messageParts[1]);
- break;
- case Shared.FileRecieve:
- Console.WriteLine("FileRecieve Message");
- FileRecieve(messageParts[1], messageParts[2], buffer);
- break;
- }
- }
- }
- client.GetStream().BeginRead(new byte[] { 0 }, 0, 0, Read, null);
- }
- catch (Exception)
- {
- //error occured tell the server that this client exited...
- Connect();
- }
- }
- private void FileRecieve(string FileName, string FileSize, byte[] InitialBuffer)
- {
- NetworkStream NS = client.GetStream();
- // Check to see if this NetworkStream is readable.
- if (NS.CanRead)
- {
- byte[] fileInfo = new byte[FileName.Length + FileSize.Length + 2];
- byte[] myReadBuffer = new byte[1024];
- byte[] myCompleteMessage = new byte[Convert.ToInt32(FileSize)];
- int numberOfBytesRead = 0;
- int totalBytesRead = 0;
- //gets the initial buffer file data and puts it into the completed message...
- Array.ConstrainedCopy(InitialBuffer, fileInfo.Length, myCompleteMessage, 0, InitialBuffer.Length - fileInfo.Length);
- totalBytesRead += InitialBuffer.Length - fileInfo.Length;
- // Incoming message may be larger than the buffer size.
- do
- {
- numberOfBytesRead = NS.Read(myReadBuffer, 0, myReadBuffer.Length);
- totalBytesRead += numberOfBytesRead;
- Array.ConstrainedCopy(myReadBuffer, 0, myCompleteMessage, totalBytesRead, myReadBuffer.Length);
- }
- while (NS.DataAvailable);
- // Print out the received message to the console.
- Console.WriteLine("You received the following message : " + myCompleteMessage);
- FileStream FS = new FileStream(FileName, FileMode.Create);
- FS.Write(myCompleteMessage, 0, myCompleteMessage.Length);
- }
- else
- {
- Console.WriteLine("Sorry. You cannot read from this NetworkStream.");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment