how to send a file using "Tcp client-server" chat program ?

Trips

Well-known member
Joined
Aug 7, 2010
Messages
2,788
Hi there,
I am trying to implement a file transfer option in my client/server chat program (C++/CLR). I have included my server program in this page. My client program is also similar to the server in sending & writing chat messages except it searches
for a server to accept its request.
Description : <br/>
my user thread is only for continuously reading the chat messages coming from the other end (using the StreamReader->ReadLine() method which is a blocking function) as you can see in the code below. I am writing the chat messages from the main thread
using StreamWriter->WriteLine() method. My "sendfile" option is also in main thread.
So when I start my server and connect the client to it, I am able to send chat messages from both sides. Things are working fine till now.
Now, when I click on my sendfile option in the main thread, I want to send a file to the client where I got struck. My doubt here is-- Is it possible to read & write chat data & send a file using these two threads only, while using the streamwriter->WriteLine()
method for file sending & chat sending ? Also the StreamReader->ReadLine() in the other side should also be able to distinguish them and do accordingly. The main problem here is how the client side can distinguish whether its receiving file data or
chat data ?
few more doubts in my mind--
Is there any class with methods like StreamReader->ReadLine() which are non-blocking ? If so I can implement file sending while blocking the reading chat operation and then again resume chat reading.
Is there any thing wrong to send all these data using a single port number ?
<br/>
Below is the code of my server program, ( please ignore me if I have not followed any standards coz I am new to writing posts)

<div style="color:Black;background-color:White; <pre>
#pragma once

<span style="color:Blue; namespace SampleServer {

<span style="color:Blue; using <span style="color:Blue; namespace System;
<span style="color:Blue; using <span style="color:Blue; namespace System::ComponentModel;
<span style="color:Blue; using <span style="color:Blue; namespace System::Collections;
<span style="color:Blue; using <span style="color:Blue; namespace System::Windows::Forms;
<span style="color:Blue; using <span style="color:Blue; namespace System::Data;
<span style="color:Blue; using <span style="color:Blue; namespace System::Drawing;
<span style="color:Blue; using <span style="color:Blue; namespace System::Threading;
<span style="color:Blue; using <span style="color:Blue; namespace System::IO;
<span style="color:Blue; using <span style="color:Blue; namespace System::Net;
<span style="color:Blue; using <span style="color:Blue; namespace System::Net::Sockets;
<span style="color:Green; //using namespace System::Text;



<span style="color:Green; /// <summary>
<span style="color:Green; /// Summary for Form1
<span style="color:Green; /// </summary>
<span style="color:Blue; public <span style="color:Blue; ref class Form1 : <span style="color:Blue; public System::Windows::Forms::Form
{
<span style="color:Blue; public:
Form1(<span style="color:Blue; void)
{
InitializeComponent();
connected = <span style="color:Blue; false;
filemode = <span style="color:Blue; false;
<span style="color:Green; //
<span style="color:Green; //TODO: Add the constructor code here
<span style="color:Green; //
}

<span style="color:Blue; protected:
<span style="color:Green; /// <summary>
<span style="color:Green; /// Clean up any resources being used.
<span style="color:Green; /// </summary>
~Form1()
{
<span style="color:Blue; if (components)
{
<span style="color:Blue; delete components;
}
}


<span style="color:Blue; protected:



<span style="color:Blue; private: System::Windows::Forms::Button^ Start_btn;

<span style="color:Blue; private:
<span style="color:Green; /// <summary>
<span style="color:Blue; delegate <span style="color:Blue; void status_delegate(String^ strMessage, <span style="color:Blue; bool x);
<span style="color:Green; //Socket^ connection;
TcpClient^ client;
NetworkStream^ dataStream;
StreamWriter^ srwriter;
StreamReader^ srreader;
ThreadStart^ ThreadDelegate;
Thread^ startthread;
Thread^ filereaderthread;
TcpListener^ mainServer;
<span style="color:Blue; bool connected,filemode;

<span style="color:Blue; private: System::Windows::Forms::Label^ label1;
<span style="color:Blue; private: System::Windows::Forms::TextBox^ IP_addr;

<span style="color:Blue; private: System::Windows::Forms::RichTextBox^ edittxtbox;
<span style="color:Blue; private: System::Windows::Forms::RichTextBox^ textdisplay_txtbox;
<span style="color:Blue; private: System::Windows::Forms::Label^ status_msg;
<span style="color:Blue; private: System::Windows::Forms::Label^ label2;
<span style="color:Blue; private: System::Windows::Forms::Button^ send_btn;
<span style="color:Blue; private: System::Windows::Forms::Button^ clear_btn;
<span style="color:Blue; private: System::Windows::Forms::LinkLabel^ linkLabel1;

<span style="color:Blue; private: System::Windows::Forms::LinkLabel^ port;
<span style="color:Blue; private: System::Windows::Forms::LinkLabel^ linkLabel2;
<span style="color:Blue; private: System::Windows::Forms::Label^ label3;
<span style="color:Blue; private: System::Windows::Forms::TextBox^ textBox1;
<span style="color:Blue; private: System::Windows::Forms::LinkLabel^ sendfile;

<span style="color:Green; /// Required designer variable.
<span style="color:Green; /// </summary>
System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code
<span style="color:Green; /// <summary>
<span style="color:Green; /// Required method for Designer support - do not modify
<span style="color:Green; /// the contents of this method with the code editor.
<span style="color:Green; /// </summary>
<span style="color:Blue; void InitializeComponent(<span style="color:Blue; void)
{
System::ComponentModel::ComponentResourceManager^ resources = (<span style="color:Blue; gcnew System::ComponentModel::ComponentResourceManager(Form1::<span style="color:Blue; typeid));
<span style="color:Blue; this->Start_btn = (<span style="color:Blue; gcnew System::Windows::Forms::Button());
<span style="color:Blue; this->label1 = (<span style="color:Blue; gcnew System::Windows::Forms::Label());
<span style="color:Blue; this->IP_addr = (<span style="color:Blue; gcnew System::Windows::Forms::TextBox());
<span style="color:Blue; this->status_msg = (<span style="color:Blue; gcnew System::Windows::Forms::Label());
<span style="color:Blue; this->label2 = (<span style="color:Blue; gcnew System::Windows::Forms::Label());
<span style="color:Blue; this->textdisplay_txtbox = (<span style="color:Blue; gcnew System::Windows::Forms::RichTextBox());
<span style="color:Blue; this->edittxtbox = (<span style="color:Blue; gcnew System::Windows::Forms::RichTextBox());
<span style="color:Blue; this->send_btn = (<span style="color:Blue; gcnew System::Windows::Forms::Button());
<span style="color:Blue; this->clear_btn = (<span style="color:Blue; gcnew System::Windows::Forms::Button());
<span style="color:Blue; this->linkLabel1 = (<span style="color:Blue; gcnew System::Windows::Forms::LinkLabel());
<span style="color:Blue; this->linkLabel2 = (<span style="color:Blue; gcnew System::Windows::Forms::LinkLabel());
<span style="color:Blue; this->label3 = (<span style="color:Blue; gcnew System::Windows::Forms::Label());
<span style="color:Blue; this->textBox1 = (<span style="color:Blue; gcnew System::Windows::Forms::TextBox());
<span style="color:Blue; this->sendfile = (<span style="color:Blue; gcnew System::Windows::Forms::LinkLabel());
<span style="color:Blue; this->SuspendLayout();
<span style="color:Green; //
<span style="color:Green; // Start_btn
<span style="color:Green; //
<span style="color:Blue; this->Start_btn->Anchor = <span style="color:Blue; static_cast<System::Windows::Forms::AnchorStyles>(((System::Windows::Forms::AnchorStyles::Top | System::Windows::Forms::AnchorStyles::Bottom)
| System::Windows::Forms::AnchorStyles::Right));
<span style="color:Blue; this->Start_btn->Location = System::Drawing::Point(253, 26);
<span style="color:Blue; this->Start_btn->Name = L<span style="color:#A31515; "Start_btn";
<span style="color:Blue; this->Start_btn->Size = System::Drawing::Size(75, 66);
<span style="color:Blue; this->Start_btn->TabIndex = 3;
<span style="color:Blue; this->Start_btn->Text = L<span style="color:#A31515; "Start Server";
<span style="color:Blue; this->Start_btn->UseVisualStyleBackColor = <span style="color:Blue; true;
<span style="color:Blue; this->Start_btn->Click += <span style="color:Blue; gcnew System::EventHandler(<span style="color:Blue; this, &Form1::Start_btn_Click);
<span style="color:Green; //
<span style="color:Green; // label1
<span style="color:Green; //
<span style="color:Blue; this->label1->Anchor = <span style="color:Blue; static_cast<System::Windows::Forms::AnchorStyles>((((System::Windows::Forms::AnchorStyles::Top | System::Windows::Forms::AnchorStyles::Bottom)
| System::Windows::Forms::AnchorStyles::Left)
| System::Windows::Forms::AnchorStyles::Right));
<span style="color:Blue; this->label1->AutoSize = <span style="color:Blue; true;
<span style="color:Blue; this->label1->Location = System::Drawing::Point(12, 53);
<span style="color:Blue; this->label1->Name = L<span style="color:#A31515; "label1";
<span style="color:Blue; this->label1->Size = System::Drawing::Size(67, 13);
<span style="color:Blue; this->label1->TabIndex = 7;
<span style="color:Blue; this->label1->Text = L<span style="color:#A31515; "IP Address :";
<span style="color:Green; //
<span style="color:Green; // IP_addr
<span style="color:Green; //
<span style="color:Blue; this->IP_addr->Anchor = <span style="color:Blue; static_cast<System::Windows::Forms::AnchorStyles>((((System::Windows::Forms::AnchorStyles::Top | System::Windows::Forms::AnchorStyles::Bottom)
| System::Windows::Forms::AnchorStyles::Left)
| System::Windows::Forms::AnchorStyles::Right));
<span style="color:Blue; this->IP_addr->Location = System::Drawing::Point(82, 50);
<span style="color:Blue; this->IP_addr->Name = L<span style="color:#A31515; "IP_addr";
<span style="color:Blue; this->IP_addr->Size = System::Drawing::Size(87, 20);
<span style="color:Blue; this->IP_addr->TabIndex = 0;
<span style="color:Blue; this->IP_addr->Text = L<span style="color:#A31515; "127.0.0.1";
<span style="color:Green; //
<span style="color:Green; // status_msg
<span style="color:Green; //
<span style="color:Blue; this->status_msg->AutoSize = <span style="color:Blue; true;
<span style="color:Blue; this->status_msg->Location = System::Drawing::Point(53, 83);
<span style="color:Blue; this->status_msg->Name = L<span style="color:#A31515; "status_msg";
<span style="color:Blue; this->status_msg->Size = System::Drawing::Size(44, 13);
<span style="color:Blue; this->status_msg->TabIndex = 13;
<span style="color:Blue; this->status_msg->Text = L<span style="color:#A31515; "Ready..";
<span style="color:Green; //
<span style="color:Green; // label2
<span style="color:Green; //
<span style="color:Blue; this->label2->AutoSize = <span style="color:Blue; true;
<span style="color:Blue; this->label2->Location = System::Drawing::Point(12, 83);
<span style="color:Blue; this->label2->Name = L<span style="color:#A31515; "label2";
<span style="color:Blue; this->label2->Size = System::Drawing::Size(46, 13);
<span style="color:Blue; this->label2->TabIndex = 9;
<span style="color:Blue; this->label2->Text = L<span style="color:#A31515; "Status : ";
<span style="color:Green; //
<span style="color:Green; // textdisplay_txtbox
<span style="color:Green; //
<span style="color:Blue; this->textdisplay_txtbox->Anchor = <span style="color:Blue; static_cast<System::Windows::Forms::AnchorStyles>((((System::Windows::Forms::AnchorStyles::Top | System::Windows::Forms::AnchorStyles::Bottom)
| System::Windows::Forms::AnchorStyles::Left)
| System::Windows::Forms::AnchorStyles::Right));
<span style="color:Blue; this->textdisplay_txtbox->BackColor = System::Drawing::SystemColors::ActiveCaptionText;
<span style="color:Blue; this->textdisplay_txtbox->Location = System::Drawing::Point(10, 99);
<span style="color:Blue; this->textdisplay_txtbox->Name = L<span style="color:#A31515; "textdisplay_txtbox";
<span style="color:Blue; this->textdisplay_txtbox->ReadOnly = <span style="color:Blue; true;
<span style="color:Blue; this->textdisplay_txtbox->ScrollBars = System::Windows::Forms::RichTextBoxScrollBars::Vertical;
<span style="color:Blue; this->textdisplay_txtbox->Size = System::Drawing::Size(319, 213);
<span style="color:Blue; this->textdisplay_txtbox->TabIndex = 8;
<span style="color:Blue; this->textdisplay_txtbox->Text = L<span style="color:#A31515; "";
<span style="color:Blue; this->textdisplay_txtbox->TextChanged += <span style="color:Blue; gcnew System::EventHandler(<span style="color:Blue; this, &Form1::textdisplay_txtbox_TextChanged);
<span style="color:Green; //
<span style="color:Green; // edittxtbox
<span style="color:Green; //
<span style="color:Blue; this->edittxtbox->Anchor = <span style="color:Blue; static_cast<System::Windows::Forms::AnchorStyles>((((System::Windows::Forms::AnchorStyles::Top | System::Windows::Forms::AnchorStyles::Bottom)
| System::Windows::Forms::AnchorStyles::Left)
| System::Windows::Forms::AnchorStyles::Right));
<span style="color:Blue; this->edittxtbox->Location = System::Drawing::Point(10, 356);
<span style="color:Blue; this->edittxtbox->Name = L<span style="color:#A31515; "edittxtbox";
<span style="color:Blue; this->edittxtbox->ScrollBars = System::Windows::Forms::RichTextBoxScrollBars::Vertical;
<span style="color:Blue; this->edittxtbox->Size = System::Drawing::Size(319, 80);
<span style="color:Blue; this->edittxtbox->TabIndex = 4;
<span style="color:Blue; this->edittxtbox->Text = L<span style="color:#A31515; "";
<span style="color:Blue; this->edittxtbox->TextChanged += <span style="color:Blue; gcnew System::EventHandler(<span style="color:Blue; this, &Form1::edittxtbox_TextChanged);
<span style="color:Blue; this->edittxtbox->KeyPress += <span style="color:Blue; gcnew System::Windows::Forms::KeyPressEventHandler(<span style="color:Blue; this, &Form1::checkkeys);
<span style="color:Green; //
<span style="color:Green; // send_btn
<span style="color:Green; //
<span style="color:Blue; this->send_btn->Anchor = <span style="color:Blue; static_cast<System::Windows::Forms::AnchorStyles>(((System::Windows::Forms::AnchorStyles::Top | System::Windows::Forms::AnchorStyles::Bottom)
| System::Windows::Forms::AnchorStyles::Right));
<span style="color:Blue; this->send_btn->Enabled = <span style="color:Blue; false;
<span style="color:Blue; this->send_btn->Location = System::Drawing::Point(254, 325);
<span style="color:Blue; this->send_btn->Name = L<span style="color:#A31515; "send_btn";
<span style="color:Blue; this->send_btn->Size = System::Drawing::Size(75, 24);
<span style="color:Blue; this->send_btn->TabIndex = 5;
<span style="color:Blue; this->send_btn->Text = L<span style="color:#A31515; "Send";
<span style="color:Blue; this->send_btn->UseVisualStyleBackColor = <span style="color:Blue; true;
<span style="color:Blue; this->send_btn->Click += <span style="color:Blue; gcnew System::EventHandler(<span style="color:Blue; this, &Form1::send_btn_Click);
<span style="color:Green; //
<span style="color:Green; // clear_btn
<span style="color:Green; //
<span style="color:Blue; this->clear_btn->Anchor = <span style="color:Blue; static_cast<System::Windows::Forms::AnchorStyles>(((System::Windows::Forms::AnchorStyles::Top | System::Windows::Forms::AnchorStyles::Bottom)
| System::Windows::Forms::AnchorStyles::Right));
<span style="color:Blue; this->clear_btn->Enabled = <span style="color:Blue; false;
<span style="color:Blue; this->clear_btn->Location = System::Drawing::Point(173, 325);
<span style="color:Blue; this->clear_btn->Name = L<span style="color:#A31515; "clear_btn";
<span style="color:Blue; this->clear_btn->Size = System::Drawing::Size(75, 24);
<span style="color:Blue; this->clear_btn->TabIndex = 6;
<span style="color:Blue; this->clear_btn->Text = L<span style="color:#A31515; "Clear";
<span style="color:Blue; this->clear_btn->UseVisualStyleBackColor = <span style="color:Blue; true;
<span style="color:Blue; this->clear_btn->Click += <span style="color:Blue; gcnew System::EventHandler(<span style="color:Blue; this, &Form1::clear_btn_Click);
<span style="color:Green; //
<span style="color:Green; // linkLabel1
<span style="color:Green; //
<span style="color:Blue; this->linkLabel1->AutoSize = <span style="color:Blue; true;
<span style="color:Blue; this->linkLabel1->Enabled = <span style="color:Blue; false;
<span style="color:Blue; this->linkLabel1->Location = System::Drawing::Point(12, 316);
<span style="color:Blue; this->linkLabel1->Name = L<span style="color:#A31515; "linkLabel1";
<span style="color:Blue; this->linkLabel1->Size = System::Drawing::Size(78, 13);
<span style="color:Blue; this->linkLabel1->TabIndex = 7;
<span style="color:Blue; this->linkLabel1->TabStop = <span style="color:Blue; true;
<span style="color:Blue; this->linkLabel1->Text = L<span style="color:#A31515; "Save Chat Log";
<span style="color:Blue; this->linkLabel1->LinkClicked += <span style="color:Blue; gcnew System::Windows::Forms::LinkLabelLinkClickedEventHandler(<span style="color:Blue; this, &Form1::linkLabel1_LinkClicked);
<span style="color:Green; //
<span style="color:Green; // linkLabel2
<span style="color:Green; //
<span style="color:Blue; this->linkLabel2->AutoSize = <span style="color:Blue; true;
<span style="color:Blue; this->linkLabel2->Location = System::Drawing::Point(170, 53);
<span style="color:Blue; this->linkLabel2->Name = L<span style="color:#A31515; "linkLabel2";
<span style="color:Blue; this->linkLabel2->Size = System::Drawing::Size(26, 13);
<span style="color:Blue; this->linkLabel2->TabIndex = 1;
<span style="color:Blue; this->linkLabel2->TabStop = <span style="color:Blue; true;
<span style="color:Blue; this->linkLabel2->Text = L<span style="color:#A31515; "Port";
<span style="color:Blue; this->linkLabel2->LinkClicked += <span style="color:Blue; gcnew System::Windows::Forms::LinkLabelLinkClickedEventHandler(<span style="color:Blue; this, &Form1::linkLabel2_LinkClicked);
<span style="color:Green; //
<span style="color:Green; // label3
<span style="color:Green; //
<span style="color:Blue; this->label3->AutoSize = <span style="color:Blue; true;
<span style="color:Blue; this->label3->Location = System::Drawing::Point(195, 53);
<span style="color:Blue; this->label3->Name = L<span style="color:#A31515; "label3";
<span style="color:Blue; this->label3->Size = System::Drawing::Size(10, 13);
<span style="color:Blue; this->label3->TabIndex = 16;
<span style="color:Blue; this->label3->Text = L<span style="color:#A31515; ":";
<span style="color:Green; //
<span style="color:Green; // textBox1
<span style="color:Green; //
<span style="color:Blue; this->textBox1->Enabled = <span style="color:Blue; false;
<span style="color:Blue; this->textBox1->Location = System::Drawing::Point(209, 50);
<span style="color:Blue; this->textBox1->Name = L<span style="color:#A31515; "textBox1";
<span style="color:Blue; this->textBox1->Size = System::Drawing::Size(39, 20);
<span style="color:Blue; this->textBox1->TabIndex = 2;
<span style="color:Blue; this->textBox1->Text = L<span style="color:#A31515; "55555";
<span style="color:Blue; this->textBox1->KeyPress += <span style="color:Blue; gcnew System::Windows::Forms::KeyPressEventHandler(<span style="color:Blue; this, &Form1::checkportnum);
<span style="color:Green; //
<span style="color:Green; // sendfile
<span style="color:Green; //
<span style="color:Blue; this->sendfile->AutoSize = <span style="color:Blue; true;
<span style="color:Blue; this->sendfile->Enabled = <span style="color:Blue; false;
<span style="color:Blue; this->sendfile->Location = System::Drawing::Point(96, 316);
<span style="color:Blue; this->sendfile->Name = L<span style="color:#A31515; "sendfile";
<span style="color:Blue; this->sendfile->Size = System::Drawing::Size(51, 13);
<span style="color:Blue; this->sendfile->TabIndex = 17;
<span style="color:Blue; this->sendfile->TabStop = <span style="color:Blue; true;
<span style="color:Blue; this->sendfile->Text = L<span style="color:#A31515; "Send File";
<span style="color:Blue; this->sendfile->LinkClicked += <span style="color:Blue; gcnew System::Windows::Forms::LinkLabelLinkClickedEventHandler(<span style="color:Blue; this, &Form1::sendfile_LinkClicked);
<span style="color:Green; //
<span style="color:Green; // Form1
<span style="color:Green; //
<span style="color:Blue; this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
<span style="color:Blue; this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
<span style="color:Blue; this->ClientSize = System::Drawing::Size(339, 448);
<span style="color:Blue; this->Controls->Add(<span style="color:Blue; this->sendfile);
<span style="color:Blue; this->Controls->Add(<span style="color:Blue; this->textBox1);
<span style="color:Blue; this->Controls->Add(<span style="color:Blue; this->label3);
<span style="color:Blue; this->Controls->Add(<span style="color:Blue; this->linkLabel2);
<span style="color:Blue; this->Controls->Add(<span style="color:Blue; this->linkLabel1);
<span style="color:Blue; this->Controls->Add(<span style="color:Blue; this->send_btn);
<span style="color:Blue; this->Controls->Add(<span style="color:Blue; this->edittxtbox);
<span style="color:Blue; this->Controls->Add(<span style="color:Blue; this->clear_btn);
<span style="color:Blue; this->Controls->Add(<span style="color:Blue; this->label1);
<span style="color:Blue; this->Controls->Add(<span style="color:Blue; this->IP_addr);
<span style="color:Blue; this->Controls->Add(<span style="color:Blue; this->textdisplay_txtbox);
<span style="color:Blue; this->Controls->Add(<span style="color:Blue; this->status_msg);
<span style="color:Blue; this->Controls->Add(<span style="color:Blue; this->Start_btn);
<span style="color:Blue; this->Controls->Add(<span style="color:Blue; this->label2);
<span style="color:Blue; this->FormBorderStyle = System::Windows::Forms::FormBorderStyle::Fixed3D;
<span style="color:Blue; this->Icon = (cli::safe_cast<System::Drawing::Icon^ >(resources->GetObject(L<span style="color:#A31515; "$this.Icon")));
<span style="color:Blue; this->MaximizeBox = <span style="color:Blue; false;
<span style="color:Blue; this->Name = L<span style="color:#A31515; "Form1";
<span style="color:Blue; this->Text = L<span style="color:#A31515; "Server";
<span style="color:Blue; this->Closing += <span style="color:Blue; gcnew System::ComponentModel::CancelEventHandler(<span style="color:Blue; this, &Form1::Form1_Closing);
<span style="color:Blue; this->ResumeLayout(<span style="color:Blue; false);
<span style="color:Blue; this->PerformLayout();

}

#pragma endregion

<span style="color:Green; // Function for the delegate to display the text in display text box
<span style="color:Blue; private: System::Void test_writer(String^ text,<span style="color:Blue; bool x)
{
textdisplay_txtbox->Text += text+<span style="color:#A31515; "rn";
}

<span style="color:Green; // Function for the delegate to change the properties of windows elements
<span style="color:Blue; private: System::Void status_display(String^ text, <span style="color:Blue; bool x)
{
status_msg->Text = text;
IP_addr->Enabled = x;
linkLabel2->Enabled = x;
Start_btn->Enabled = x;
sendfile->Enabled = x;
<span style="color:Blue; if(text == <span style="color:#A31515; "Connected..")
{
Start_btn->Enabled = <span style="color:Blue; true;
sendfile->Enabled = <span style="color:Blue; true;
}
<span style="color:Blue; if(text == <span style="color:#A31515; "Ready..")
{
textdisplay_txtbox->Clear();
sendfile->Enabled = <span style="color:Blue; false;
linkLabel1->Enabled = <span style="color:Blue; false;
}
<span style="color:Blue; if(x)
Start_btn->Text = <span style="color:#A31515; "Start Server";
}

<span style="color:Green; // Closing function for the form
<span style="color:Blue; private: System::Void Form1_Closing(System::Object^ sender, System::ComponentModel::CancelEventArgs^ e )
{
<span style="color:Green; // Display a Message box to get confirmation from the user
<span style="color:Blue; if(MessageBox::Show(<span style="color:#A31515; "Please Confirm to Close Server",<span style="color:#A31515; "Server",MessageBoxButtons::YesNo) == Windows::Forms::DialogResult::Yes)
{
System::Environment::Exit(System::Environment::ExitCode);
}
<span style="color:Blue; else
e->Cancel = <span style="color:Blue; true;
}

<span style="color:Green; // Button click function to initiate the Server for listening
<span style="color:Blue; private: System::Void Start_btn_Click
(System::Object^ sender, System::EventArgs^ e)
{
<span style="color:Blue; if(Start_btn->Text == <span style="color:#A31515; "Start Server")
{
<span style="color:Blue; try
{
<span style="color:Green; //String^ x = System::Net::Dns::GetHostName();
IP_addr->Enabled = <span style="color:Blue; false;
<span style="color:Blue; int port = Int32::Parse(textBox1->Text);
<span style="color:Blue; if (!(port >= 1023 && port <= 65535))
{
MessageBox::Show(<span style="color:#A31515; "Enter valid Port Number(Min : 1023,Max : 65535)! and Start again",<span style="color:#A31515; "Server");
<span style="color:Blue; return;
}
linkLabel2->Enabled = <span style="color:Blue; false;
Start_btn->Text = <span style="color:#A31515; "Close Server";
connected = <span style="color:Blue; true;
status_msg->Text = <span style="color:#A31515; "Monitoring for Client Request..";
ThreadDelegate = <span style="color:Blue; gcnew ThreadStart(<span style="color:Blue; this,&Form1::mythread);
startthread = <span style="color:Blue; gcnew Thread(ThreadDelegate);
startthread->Start();
}
<span style="color:Blue; catch(Exception^ e)
{
MessageBox::Show(e->ToString(),<span style="color:#A31515; "Server");
System::Environment::Exit(System::Environment::ExitCode);
}
}
<span style="color:Blue; else <span style="color:Blue; if (connected && client != <span style="color:Blue; nullptr)
{
<span style="color:Blue; if(MessageBox::Show(<span style="color:#A31515; "Connection existing with Client ! Please Confirm to Close Server",<span style="color:#A31515; "Server",MessageBoxButtons::YesNo) == Windows::Forms::DialogResult::Yes)
{
System::Environment::Exit(System::Environment::ExitCode);
}
}
<span style="color:Blue; else
{
MessageBox::Show(<span style="color:#A31515; "Closing Server at users request !",<span style="color:#A31515; "Server");
System::Environment::Exit(System::Environment::ExitCode);
}
}

<span style="color:Green; // Thread in the Server Form to keep on listening for the incoming messages
<span style="color:Blue; public: <span style="color:Blue; void mythread()

{
<span style="color:Blue; try
{
<span style="color:Green; // Parse the servers IP address out of the TextBox
IPAddress^ ipAddr = IPAddress::Parse(IP_addr->Text);
mainServer = <span style="color:Blue; gcnew TcpListener(ipAddr,Int32::Parse(textBox1->Text));
<span style="color:Green; // Start TcpListener for the Client Request
mainServer->Start();
client = mainServer->AcceptTcpClient();
<span style="color:Green; // call delegate method to change the status message
<span style="color:Blue; this->BeginInvoke(<span style="color:Blue; gcnew status_delegate(<span style="color:Blue; this, &Form1::status_display), <span style="color:#A31515; "Connected..", <span style="color:Blue; false);
<span style="color:Green; // stop the listener from Listening
mainServer->Stop();
dataStream = client->GetStream();
srreader = <span style="color:Blue; gcnew StreamReader(dataStream);
srwriter = <span style="color:Blue; gcnew StreamWriter(dataStream);
}
<span style="color:Blue; catch(Exception^)
{
MessageBox::Show(<span style="color:#A31515; "Please Enter valid IP Address & Port Number!",<span style="color:#A31515; "Server");
client->Close();
<span style="color:Blue; this->BeginInvoke(<span style="color:Blue; gcnew status_delegate(<span style="color:Blue; this, &Form1::status_display), <span style="color:#A31515; "Ready..",<span style="color:Blue; true);
connected = <span style="color:Blue; false;
<span style="color:Blue; return;
}

<span style="color:Blue; while(connected && client != <span style="color:Blue; nullptr && !filemode)
{
<span style="color:Blue; try
{
String^ recieve = srreader->ReadLine();
<span style="color:Blue; this->BeginInvoke(<span style="color:Blue; gcnew status_delegate(<span style="color:Blue; this,&Form1::test_writer), recieve,<span style="color:Blue; false);

}
<span style="color:Blue; catch (IOException^)
{
<span style="color:Blue; this->BeginInvoke(<span style="color:Blue; gcnew status_delegate(<span style="color:Blue; this, &Form1::status_display), <span style="color:#A31515; "Disconnected..",<span style="color:Blue; false);
MessageBox::Show(<span style="color:#A31515; "Connection Terminated,Press OK to Reset Sever",<span style="color:#A31515; "Server");
client = <span style="color:Blue; nullptr;
<span style="color:Blue; this->BeginInvoke(<span style="color:Blue; gcnew status_delegate(<span style="color:Blue; this, &Form1::status_display), <span style="color:#A31515; "Ready..",<span style="color:Blue; true);
connected = <span style="color:Blue; false;
<span style="color:Blue; return;
}
}
}

<span style="color:Green; // TextChanged event handler function for the edittxtbox to monitor for blank text
<span style="color:Blue; private: <span style="color:Blue; void edittxtbox_TextChanged(System::Object^ sender, System::EventArgs^ e)
{
<span style="color:Blue; if (edittxtbox->Text != <span style="color:#A31515; "")
{
send_btn->Enabled = <span style="color:Blue; true;
clear_btn->Enabled = <span style="color:Blue; true;
}
<span style="color:Blue; else
{
send_btn->Enabled = <span style="color:Blue; false;
clear_btn->Enabled = <span style="color:Blue; false;
}
}

<span style="color:Green; // key pressed event handler for edittxtbox to write message to the connection
<span style="color:Blue; private: System::Void checkkeys(System::Object^ sender, KeyPressEventArgs^ e)
{
<span style="color:Blue; if(e->KeyChar == (<span style="color:Blue; char)13 && edittxtbox->Text != String::Empty)
{
<span style="color:Blue; if (send_btn->Enabled && client != <span style="color:Blue; nullptr && !filemode)
{
textdisplay_txtbox->Text += <span style="color:#A31515; "Server : "+edittxtbox->Text;
srwriter->Write(<span style="color:#A31515; "Server : "+edittxtbox->Text);
srwriter->Flush();
edittxtbox->Clear();
}
<span style="color:Blue; else
{
MessageBox::Show(<span style="color:#A31515; "Connection not Established !",<span style="color:#A31515; "Server");
edittxtbox->Clear();
}
}
}

<span style="color:Green; // "send" button click function for edittxtbox for the text to be sent over the connection
<span style="color:Blue; private: <span style="color:Blue; void send_btn_Click(System::Object^ sender, System::EventArgs^ e)
{
<span style="color:Blue; if (connected && client != <span style="color:Blue; nullptr && !filemode)
{
textdisplay_txtbox->Text += <span style="color:#A31515; "Server : "+edittxtbox->Text+<span style="color:#A31515; "rn";
srwriter->Write(<span style="color:#A31515; "Server : "+edittxtbox->Text+<span style="color:#A31515; "rn");
srwriter->Flush();
edittxtbox->Clear();
}
<span style="color:Blue; else
{
MessageBox::Show(<span style="color:#A31515; "Connection not Established !",<span style="color:#A31515; "Server");
edittxtbox->Clear();
}
}

<span style="color:Green; // "clear" button click function for edittxtbox to clear the text when button pressed
<span style="color:Blue; private: System::Void clear_btn_Click(System::Object^ sender, System::EventArgs^ e)
{
edittxtbox->Clear();
}

<span style="color:Green; // link pressed method for the chat text to be saved in to a file in the local machine<br/>
                <span style="color:Blue; private: System::Void linkLabel1_LinkClicked(System::Object^ sender, System::Windows::Forms::LinkLabelLinkClickedEventArgs^ e)
{
<span style="color:Blue; try
{
String^ filename = <span style="color:#A31515; "ChatLog.txt";
<span style="color:Blue; int i=1;
<span style="color:Blue; while(File::Exists(filename))
{
filename = <span style="color:#A31515; "ChatLog"+<span style="color:#A31515; "("+i+<span style="color:#A31515; ")"+<span style="color:#A31515; ".txt";
i++;
}
FileStream^ fs = <span style="color:Blue; gcnew FileStream(filename, FileMode::CreateNew);
BinaryWriter^ w = <span style="color:Blue; gcnew BinaryWriter(fs);
w->Write(textdisplay_txtbox->Text->Replace(<span style="color:#A31515; "n",<span style="color:#A31515; "rn"));
w->Close();
fs->Close();
MessageBox::Show(<span style="color:#A31515; "Chat Log has been saved as "+filename,<span style="color:#A31515; "Server");
}
<span style="color:Blue; catch(Exception^ e)
{
MessageBox::Show(e->ToString(),<span style="color:#A31515; "Server");
}
}

<span style="color:Green; // function to enable the save as file link to save the display text in a file
<span style="color:Blue; private: System::Void textdisplay_txtbox_TextChanged(System::Object^ sender, System::EventArgs^ e)
{
<span style="color:Blue; if(textdisplay_txtbox->Text != <span style="color:#A31515; "")
linkLabel1->Enabled = <span style="color:Blue; true;
}

<span style="color:Green; // function which enables the port number text box on clicking the link
<span style="color:Blue; private: System::Void linkLabel2_LinkClicked(System::Object^ sender, System::Windows::Forms::LinkLabelLinkClickedEventArgs^ e)
{
<span style="color:Blue; if(!textBox1->Enabled)
{
textBox1->Enabled = <span style="color:Blue; true;
}
}

<span style="color:Green; // key pressed event handler to validate the port number in the text box
<span style="color:Blue; private: System::Void checkportnum(System::Object^ sender, KeyPressEventArgs^ e)
{
<span style="color:Blue; if(e->KeyChar == (<span style="color:Blue; char)13)
{
<span style="color:Blue; if(Int32::Parse(textBox1->Text) >= 1023 && Int32::Parse(textBox1->Text) <= 65535)
{
textBox1->Enabled = <span style="color:Blue; false;
MessageBox::Show(<span style="color:#A31515; "Please Connect Client with the same Port Number",<span style="color:#A31515; "Server");
}
<span style="color:Blue; else
{
MessageBox::Show(<span style="color:#A31515; "Enter Valid Port Number(Min : 1023,Max : 65535)",<span style="color:#A31515; "Server");
}
}
}

<span style="color:Green; // function which enables to send a file over the connection on clicking the link
<span style="color:Blue; private: System::Void sendfile_LinkClicked(
System::Object^ sender, System::Windows::Forms::LinkLabelLinkClickedEventArgs^ e)
{
<span style="color:Blue; try
{
<span style="color:Blue; if(connected && client != <span style="color:Blue; nullptr)
{
Stream^ fileStream;
sendfile->Enabled = <span style="color:Blue; false;
send_btn->Enabled = <span style="color:Blue; false;
OpenFileDialog^ filewindow = <span style="color:Blue; gcnew OpenFileDialog;
filewindow->InitialDirectory = "c:\";
filewindow->Filter = <span style="color:#A31515; "txt files (*.txt)|*.txt|All files (*.*)|*.*";
filewindow->FilterIndex = 2;
filewindow->RestoreDirectory = <span style="color:Blue; true;
<span style="color:Blue; if ( filewindow->ShowDialog() == System::Windows::Forms::DialogResult::OK )
{
<span style="color:Blue; if ( (fileStream = filewindow->OpenFile()) != <span style="color:Blue; nullptr )
{
String^ file_name = System::IO::Path::GetFileName(filewindow->FileName);
<span style="color:Blue; long filesize = filewindow->FileName->Length;
<span style="color:Green; //MessageBox::Show(file_name);
filemode = <span style="color:Blue; true;
srwriter->WriteLine(<span style="color:#A31515; "file");
srwriter->WriteLine(file_name);

BinaryReader^ r = <span style="color:Blue; gcnew BinaryReader(fileStream);
BinaryWriter^ w = <span style="color:Blue; gcnew BinaryWriter(dataStream);
w->Write(filesize);
<span style="color:Blue; array<Byte>^ data = <span style="color:Blue; gcnew <span style="color:Blue; array<Byte>(filesize);
<span style="color:Blue; int datasize;
<span style="color:Blue; do
{
datasize = r->Read(data, 0, filesize);
<span style="color:Blue; for(<span style="color:Blue; int i=0;i< data->Length;i++)
{
w->Write(data);
filesize -= 1;
}
} <span style="color:Blue; while (filesize > 0);
<span style="color:Green; //srwriter->Write("finishedrn");
fileStream->Close();
<span style="color:Green; //sendfile->Enabled = true;
filemode = <span style="color:Blue; false;
}
<span style="color:Blue; else
fileStream->Close();
}
sendfile->Enabled = <span style="color:Blue; true;
send_btn->Enabled = <span style="color:Blue; true;
}
}
<span style="color:Blue; catch(Exception^ e)
{
MessageBox::Show(e->ToString(),<span style="color:#A31515; "Server");
}

}
};
}


[/code]

It would be very nice of you if any one can help me in resolving this. If my post is not clear, please mention ur doubt so that I can explain it more clear. Thanks in advance. Waiting for your reply.
Regards,
pvnkmr

View the full article
 
Back
Top