![]() |
| | #1 |
| Registered User Join Date: Dec 2007 Location: France
Posts: 396
| Send data with class TcpClientObject.SendToSocket((char*)&DataObject, sizeof(DataObject)); but then the data sent is like a memory address of 7 numbers. I think its maybe sizeof(DataObject), because DataObject is a class and its size is not the same as the size of a, b, c? Thanks! Code: #include <windows.h>
#include <iostream>
#pragma comment(lib,"ws2_32.lib")
#pragma comment(lib,"user32.lib")
using namespace std;
class Data
{
public:
int a;
int b;
int c;
protected:
private:
};
class TcpClient
{
public:
void InitWinsock()
{
Result = WSAStartup(MAKEWORD(2, 2), &WSAdata);
if(Result != 0)
{printf("%s\n","Setting Up Winsock Client Failed!"); WSACleanup();}
else
printf("%s\n","Setting Up Winsock Client Success!");
}
void SetAddressInfo()
{
memset(&sins, 0, sizeof(sins));
ConnectSock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
sins.sin_family = AF_INET;
sins.sin_port = htons(80);
sins.sin_addr.s_addr = inet_addr("127.0.0.1");
}
void ConnectInfo()
{
if(connect(ConnectSock, (sockaddr*)&sins, sizeof(sins))== SOCKET_ERROR)
{printf("%s\n","Failed To Connect!"); WSACleanup();}
else printf("%s\n","Connected To Server!");
}
void SendToSocket(char* Data, int size)
{
while(1)
{
// printf("%s\n","Enter Something To Send!");
// cin >> Something;
Result = send(ConnectSock,Data,size,0);
if(Result == -1) {printf("%s\n","Failed To Send Data!"); WSACleanup(); break;}
else{printf("Data Sent! %i\n", Data); Sleep(3000);}
}
}
protected:
private:
WSADATA WSAdata;
SOCKET ConnectSock;
sockaddr_in sins;
int Result;
char Something[64];
};
/**********************************************************************************/
/******************************THE SERVER****************************************/
class TcpServer
{
public:
void InitWinsock()
{
Result = WSAStartup(MAKEWORD(2, 2), &WSAdata);
if(Result == -1)
{printf("%s\n","Setting Up Winsock Server Failed!"); WSACleanup();}
else
{printf("%s\n","Setting Up Winsock Server Success!");}
}
void SetAddressInfo()
{
memset(&sins, 0, sizeof(sins));
ListenSock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
sins.sin_family = AF_INET;
sins.sin_port = htons(80);
sins.sin_addr.s_addr = inet_addr("127.0.0.1");
}
void BindnListen()
{
if( bind( ListenSock, (struct sockaddr*)&sins, sizeof( struct sockaddr)) == SOCKET_ERROR ){
printf("%s\n","Failed To Bind To Address!");WSACleanup();}
else printf("%s\n","Binded To Address Success!");
if(listen(ListenSock, 1)==SOCKET_ERROR){
printf("%s\n","Failed To Listen On Address!"); WSACleanup();}
else printf("%s\n","Listening On Address!");
}
void WaitForConnections()
{
while(1)
{
printf("%s\n","Waiting For Connections!");
int clnt = sizeof(sins);
AcceptSock = accept(ListenSock ,(struct sockaddr*)&sins, &clnt);
if(AcceptSock == INVALID_SOCKET)
{
printf("%s\n","No Connection Made Yet!");
}
else
{
printf("%s\n","Connection Made!");
break;
}
}
}
void RecieveFromSocket()
{
while(1)
{
if(AcceptSock != INVALID_SOCKET)
{
recv(AcceptSock,(char*)&DataObject,sizeof(DataObject),0);
printf("%d\n",DataObject.a);
printf("%d\n",DataObject.b);
printf("%d\n",DataObject.c);
}
else
{
printf("%s\n","No Connection Yet!");
}
}
}
protected:
private:
WSADATA WSAdata;
SOCKET ListenSock;
SOCKET AcceptSock;
sockaddr_in sins;
int Result;
char RecvBuffer[512];
Data DataObject;
};
/**********************************THE SERVER END********************************/
int main(int argc, char* argv[])
{
Data DataObject;
DataObject.a = 50;
DataObject.b = 100;
DataObject.c = 200;
TcpClient TcpClientObject;
TcpClientObject.InitWinsock();
TcpClientObject.SetAddressInfo();
TcpServer TcpServerObject;
TcpServerObject.InitWinsock();
TcpServerObject.SetAddressInfo();
TcpServerObject.BindnListen();
TcpClientObject.ConnectInfo();
TcpServerObject.WaitForConnections();
TcpClientObject.SendToSocket((char*)&DataObject, sizeof(DataObject));
TcpServerObject.RecieveFromSocket();
WSACleanup();
return 0;
}
__________________ Using Code::Blocks,MingW with Windows. |
| Ducky is offline | |
| | #2 |
| Sweet Join Date: Aug 2002 Location: Tucson, Arizona
Posts: 1,678
| You are sending the memory address of the class, not the class itself. You would basically need to send the info to serialize to a class. Maybe name / value pairs or something like that. Code: SendToSocket("a=10&b=15&c=20", lengthOfString);
__________________ Woop? |
| prog-bman is offline | |
| | #3 |
| Registered User Join Date: Dec 2007 Location: France
Posts: 396
| Thank you prog-bman! I dont see right now how to solve this because if i send the info directly why would i need the class?
__________________ Using Code::Blocks,MingW with Windows. |
| Ducky is offline | |
| | #4 | |
| The superheterodyne. Join Date: Dec 2005 Location: Ireland
Posts: 2,205
| No... I think. Try taking the loop out of the send function. The receive function is never called in your code, cause of while(1). You'll either need to create two programs to test this (one client, one server) or create a separate thread to test it all either a client or server thread, whichever you want. My output (with a break on success of send in the send function): Quote:
__________________ I blag! | |
| twomers is offline | |
| | #5 |
| Registered User Join Date: Dec 2007 Location: France
Posts: 396
| Wow you're right its working! I just added the 2 break; where you suggested and its working without creating a thread or two separate programs. Its interesting though as to why would it print out the memory address and the 3 datas too when there are only 3 printf() in the RecieveFromSocket() function. Thank you twomers!
__________________ Using Code::Blocks,MingW with Windows. |
| Ducky is offline | |
| | #6 | |
| The superheterodyne. Join Date: Dec 2005 Location: Ireland
Posts: 2,205
| Not really. You're printf("Data Sent! %i\n", Data)-ing... Data is a char* type. If you were to dereference it and overload the operator<< method it would print the right values. If you do a Code: class Data
{
public:
int a;
int b;
int c;
void print(){printf( "%d, %d, %d\n", a, b, c ); };
protected:
private:
};
Code: printf("Data Sent! \n", Data); ((::Data*)Data)->print();
Quote:
__________________ I blag! | |
| twomers is offline | |
| | #7 |
| Registered User Join Date: Dec 2007 Location: France
Posts: 396
| Ah ok, i understand now, thank you!
__________________ Using Code::Blocks,MingW with Windows. |
| Ducky is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Screwy Linker Error - VC2005 | Tonto | C++ Programming | 5 | 06-19-2007 02:39 PM |
| Message class ** Need help befor 12am tonight** | TransformedBG | C++ Programming | 1 | 11-29-2006 11:03 PM |
| help on class coupling | andrea72 | C++ Programming | 3 | 07-05-2006 12:15 PM |
| binary tree token problem | OldSchool | C++ Programming | 13 | 05-28-2006 10:42 PM |
| Warnings, warnings, warnings? | spentdome | C Programming | 25 | 05-27-2002 06:49 PM |