E klasa koju sam ti poslala je smjestena u h file, evo saljem ti cpp file:
Code:
#include "stdafx.h"
#include "Client.h"
//#include "Form1.h"
using namespace Socket_multithread;
Client::Client()
{
CallBackReceiveFunction=new AsyncCallback(this, ReceiveComplete);
CallBackSendFunction=new AsyncCallback(this, SendComplete);
}
void Client::ConnectClient(String *ip, Int16 i)
{
try
{
if(objNetworkStream==NULL) // provjerava da li je objNetworkStream vec kreiran
{
String * IP=ip;
Int16 portTemp=i;
objTcpClient=new TcpClient(IP, portTemp); // konektuje klijenta kroz konstruktor bez pozivanja Connect
objNetworkStream=objTcpClient->GetStream(); // uzima Network Stream za slanje i primanje poruka
ClientReceive(); //zapocinje asihroni prijem podataka
}
}
catch(Exception * e)
{
MessageBox::Show(e->Message, S"Greska");
}
}
void Client::ClientReceive()
{
if(objNetworkStream->CanRead && objNetworkStream!=NULL)
{
objNetworkStream->BeginRead(ReadBuffer, 0, ReadBuffer->Length, CallBackReceiveFunction, NULL);
}
else
MessageBox::Show(S"Greska pri prijemu, nema poruka");
}
void Client::ReceiveComplete(IAsyncResult *ar)
{
if(objNetworkStream->CanRead)
{
CompleteReceiveMessage=S"";
numberOfBytesRead=objNetworkStream->EndRead(ar);
CompleteReceiveMessage=Encoding::ASCII->GetString(ReadBuffer,0,numberOfBytesRead);
//MessageBox::Show(CompleteReceiveMessage);
int ind = form->txtKonverzacija->Items->Add(CompleteReceiveMessage);
form->txtKonverzacija->TopIndex = ind;
if(numberOfBytesRead!=0)
ClientReceive();
else
MessageBox::Show(S"Server se diskonektovao");
}
}
void Client::SendComplete(IAsyncResult * ar)
{
objNetworkStream->EndWrite(ar);
}
Pokazivac na objekat ove klase koristi u sljedecoj klasi:
Code:
#pragma once
//#include "ServerSocket.h"
namespace Socket_multithread
{
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
/// <summary>
/// Summary for Form1
///
/// WARNING: If you change the name of this class, you will need to change the
/// 'Resource File Name' property for the managed resource compiler tool
/// associated with all .resx files this class depends on. Otherwise,
/// the designers will not be able to interact properly with localized
/// resources associated with this form.
/// </summary>
//__gc class ServerSocket;
__gc class Client;
__gc class ServerSocket;
public __gc class Form1 : public System::Windows::Forms::Form
{
private: Client *cc;
private: ServerSocket * serSocket;
public:
Form1(void)
{
InitializeComponent();
}
protected:
void Dispose(Boolean disposing)
{
if (disposing && components)
{
components->Dispose();
}
__super::Dispose(disposing);
}
int port;
String * ipadresa;
// Socket_multithread::ServerSocket* serSoc;
bool serverStart;
bool clientStart;
private: System::Windows::Forms::Label * label1;
private: System::Windows::Forms::TextBox * txtPort;
private: System::Windows::Forms::TextBox * txtPoruka;
private: System::Windows::Forms::Button * btnListen;
private: System::Windows::Forms::Button * btnDisconnect;
private: System::Windows::Forms::Button * btnSend;
private: System::Windows::Forms::GroupBox * groupBox1;
private: System::Windows::Forms::RadioButton * rbtnServer;
private: System::Windows::Forms::RadioButton * rbtnClient;
private: System::Windows::Forms::Label * lbServer;
private: System::Windows::Forms::TextBox * txtServer;
private: System::Windows::Forms::Button * btnExit;
public: System::Windows::Forms::ListBox * txtKonverzacija;
private:
/// <summary>
/// Required designer variable.
/// </summary>
System::ComponentModel::Container * components;
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
void InitializeComponent(void);
private: System::Void rbtnServer_CheckedChanged_1(System::Object * sender, System::EventArgs * e);
private: System::Void rbtnClient_CheckedChanged(System::Object * sender, System::EventArgs * e);
private: System::Void btnListen_Click(System::Object * sender, System::EventArgs * e);
};
}