PDA

View Full Version : Problem in socket.



Niamita
28th July 2011, 08:50
Hi all
i am working on socket programming but i am not understanding the reason behind the problem. I have three classes light_frame.cpp, home_profile.cpp and socket.cpp
In socket.cpp class i implement the socket and with remaining two classes i am connecting that socket but it is not working correctly, with first class the output is desire but in second class, it is sending nothing to server.
Code for the three classes are:

Light_frame.cpp

Light_frame::Light_frame(QWidget *parent) :
QFrame(parent)
{
QSignalMapper *signalMapper = new QSignalMapper(this);
signalMapper->setMapping(button_1st, 1);
signalMapper->setMapping(button_2nd, 2);
signalMapper->setMapping(button_3rd, 3);
signalMapper->setMapping(button_down_arrow, 4);
signalMapper->setMapping(button_up_arrow, 5);


connect(button_1st, SIGNAL(clicked()),
signalMapper, SLOT (map()));
connect(button_2nd, SIGNAL(clicked()),
signalMapper, SLOT (map()));
connect(button_3rd, SIGNAL(clicked()),
signalMapper, SLOT (map()));
connect(button_down_arrow, SIGNAL(clicked()),
signalMapper, SLOT (map()));
connect(button_up_arrow, SIGNAL(clicked()),
signalMapper, SLOT (map()));
connect(signalMapper, SIGNAL(mapped(int)),
this, SLOT(open(int)));

Socket *socket_new = new Socket(this);

QSignalMapper *signalMapper_socket = new QSignalMapper(this);
signalMapper_socket->setMapping(button_1st, 4);
signalMapper_socket->setMapping(button_2nd, 5);
signalMapper_socket->setMapping(button_3rd, 6);
signalMapper_socket->setMapping(button_on, 7);
signalMapper_socket->setMapping(button_off, 8);
signalMapper_socket->setMapping(button_down_arrow, 9);
signalMapper_socket->setMapping(button_up_arrow, 10);

connect(button_1st, SIGNAL(clicked()),
signalMapper_socket, SLOT (map()));

connect(button_2nd, SIGNAL(clicked(bool)),
signalMapper_socket, SLOT (map()));

connect(button_3rd, SIGNAL(clicked()),
signalMapper_socket, SLOT (map()));

connect(button_on, SIGNAL(clicked()),
signalMapper_socket, SLOT (map()));

connect(button_off, SIGNAL(clicked()),
signalMapper_socket, SLOT (map()));

connect(button_down_arrow, SIGNAL(clicked()),
signalMapper_socket, SLOT (map()));

connect(button_up_arrow, SIGNAL(clicked()),
signalMapper_socket, SLOT (map()));

connect(signalMapper_socket, SIGNAL(mapped(int)),
socket_new, SLOT(sendRequest(int)));
}

home_profile2.cpp

Home_Profile2::Home_Profile2(QWidget *parent) :
QFrame(parent)
{
Socket *socket_new = new Socket(this);

QSignalMapper *signalMapper = new QSignalMapper(this);
signalMapper->setMapping(button_out, 1);
signalMapper->setMapping(button_maid, 2);
signalMapper->setMapping(button_holiday, 3);

connect(button_out, SIGNAL(clicked()),
signalMapper, SLOT (map()));

connect(button_maid, SIGNAL(clicked()),
signalMapper, SLOT (map()));

connect(button_holiday, SIGNAL(clicked()),
signalMapper, SLOT (map()));

connect(signalMapper, SIGNAL(mapped(int)),
socket_new, SLOT(sendRequest(int)));
}socket.cpp

Socket::Socket(QObject * parent)
: QTcpSocket(parent)
{
socket = new QTcpSocket(this);
socket->connectToHost("10.10.1.110", 1222);
connect(socket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(error()));
}

void Socket::sendRequest(int id)
{
make_packet *make = new make_packet();
uint8_t temp_var;
uint8_t arr[LENGTH];

switch(id){

case 1:
{
temp_var = make->packet_formation(Home_Profile, Out_Action);
break;
}
case 2:
{
temp_var = make->packet_formation(Home_Profile, Maid_Action);
break;
}
case 3:
{
temp_var = make->packet_formation(Home_Profile, Holiday_Action);
break;
}
case 4:
{
temp_var = make->packet_formation(Light_Id, SET_25_PERCENT);
break;
}
case 5:
{
temp_var = make->packet_formation(Light_Id, SET_50_PERCENT);
break;
}
case 6:
{
temp_var = make->packet_formation(Light_Id, SET_75_PERCENT);
break;
}
case 7:
{
temp_var = make->packet_formation(Light_Id, ON);
break;
}
case 8:
{
temp_var = make->packet_formation(Light_Id, OFF);
break;
}
case 9:
{
temp_var = make->packet_formation(Light_Id, DECREMENT);
break;
}
case 10:
{
temp_var = make->packet_formation(Light_Id, INCREMENT);
break;
}
}

for(int i=0; i<temp_var; i++)
{
arr[i] = make->packet_frame[i];
}
QByteArray block;
QDataStream out(&block, QIODevice::WriteOnly);
out.setVersion(QDataStream::Qt_4_5);
out << uint16_t(temp_var); // number of elements
for (int i=0; i<temp_var; ++i)
out << arr[i];
socket->write(block.toHex());

}

Help me.