PDA

View Full Version : Socket Prog



Vivek1982
12th November 2013, 10:45
Dear All,

I have developed a SERVER_CLIENT Application, Where Server will receives data from client and present it on the Server Application. I have done with the 1-server and 1-client. If i want to increase the Client Nos. I'm not able to do it.. When same Client application in compiled second time as second client. The data at server end is not processed or to be clear. Data was not sent from Secone client only. How this can be addressed,
1.either we need to create mulitple sockets.. how this can be done having one-Serverapplication.
2. or Can we create a Server Application where it can accept mutilple "ports" (e.g server->listen(QHostAddress::Any,1234, server->listen(QHostAddress::Any,1235);)) connections ?:) in the server application while checking for Newconnections.

toufic.dbouk
12th November 2013, 11:41
One approach you could try is every time you get an incoming connection from the server you set a socket and the streams for that user, ( you can make a class user to handle all this ).
So now when a client connects an instance of the class will be created specifically to that user handling his requests.
You will still have one Server socket to listen and you create a client-socket for every user.
That's a general approach. You can find several other ways to solve the issue.

Good Luck.

anda_skoa
12th November 2013, 11:55
1.either we need to create mulitple sockets.. how this can be done having one-Serverapplication.

This is done automatically. Each incoming connection gets its own QTcpSocket instance.



2. or Can we create a Server Application where it can accept mutilple "ports" (e.g server->listen(QHostAddress::Any,1234, server->listen(QHostAddress::Any,1235);)) connections ?:) in the server application while checking for Newconnections.

No need for multiple listening ports. All connections to a QTcpServer result in new QTcpSocket objects being created, one for each incoming connection/client.

Cheers,
_

Vivek1982
12th November 2013, 13:16
Thanks for all your replies. I will give an attempt to create a multiple Sockets for adding new clients into my server application. I hope I may example(Qt inbuilt) for this to unsderstand, how coding/ creating multiple socket can be done.

anda_skoa
12th November 2013, 16:38
Just to be sure you understood this correctly: you don't need to create multiple sockets, they are created for you. This is what the QTcpServer ultimately does.

Cheers,
_

Vivek1982
13th November 2013, 06:44
Hi. I have tried upto my level how to create a Server to handle many clients, but my non of the debug msgs are working. Im presenting my simple Server client code.plz support me.

Teamstatus.h

class TeamStatus : public QMainWindow {
Q_OBJECT
public:
TeamStatus(QWidget *parent = 0);
~TeamStatus();
void listen();
void socketdisc();
protected:
void changeEvent(QEvent *e);
void incomingConnection(int socketfd);

private:
Ui::TeamStatus *ui1;
QTcpServer *server;
QSet <QTcpSocket*> sockets;
public slots:
void read_socket();

Teamstatus.cpp


TeamStatus::TeamStatus(QWidget *parent) :
QMainWindow(parent),
ui1(new Ui::TeamStatus)
{
ui1->setupUi(this);
server=new QTcpServer(this);
TeamStatus::~TeamStatus()
{

delete ui1;
}
void TeamStatus::listen()


{
QHostAddress hAddr;
hAddr.setAddress("192.168.64.55");

server->listen(QHostAddress::Any,1234);
qDebug("Listening to new connection");
qDebug("listening for new socket")
}

void TeamStatus::incomingConnection(int socketfd)
{{

socket=server->nextPendingConnection();
if((socket->state()==QTcpSocket::ConnectedState))
{
QTcpSocket *socket = new QTcpSocket;
socket->setSocketDescriptor(socketfd);

qDebug("new connection1 established");

}
{
}
qDebug("new connection1 established");

}

read_socket();


}



void TeamStatus::read_socket()
{
QTcpSocket *socket=new QTcpSocket;
socket=(QTcpSocket*) sender();

qDebug("after readingy);
QByteArray buffer= socket->readLine();
... check for the buffer, do something

void TeamStatus::write_socket()
{
"How to write the data or send packet to single client or for all clients"
}



Client.h


class QTcpSocket;

namespace Ui {
class TCPClient;
}

class TCPClient : public QWidget {
Q_OBJECT
public:
TCPClient(QWidget *parent = 0);
~TCPClient();

protected:
void changeEvent(QEvent *e);

private:
Ui::TCPClient *ui;
QLineEdit *le,*le1;
QTextEdit *te,*te1;
QPushButton *pb_connect, *pb_disconnect;
QPushButton *pb_send,*pb_send1;
QTcpSocket *socket;
QString CHAT;

private slots:
void connecttoserver();
void on_connected();
void write();
void write1();
void DisconnectServer();

};

#endif // TCPCLIENT_H




Client.cpp


TCPClient::TCPClient(QWidget *parent) :
QWidget(parent),
ui(new Ui::TCPClient)
{
ui->setupUi(this);

le= new QLineEdit;
le=ui->lineEdit;
le->setDisabled(true);

le1=new QLineEdit;
le1=ui->lineEdit_2;
le1->setDisabled(true);

te=new QTextEdit;
te=ui->textEdit;
te->setDisabled(true);

te1=new QTextEdit;
te1=ui->textEdit_2;
te1->setDisabled(true);

pb_connect=new QPushButton;
pb_connect=ui->pushButton;
pb_connect->setText("CONNECT");
pb_connect->setEnabled(true);

pb_send=new QPushButton;
pb_send=ui->pushButton_2;
pb_send->setText("Send");
pb_send->setEnabled(false);

pb_send1=new QPushButton;
pb_send1=ui->pushButton_3;
pb_send1->setText("SEND");
pb_send1->setEnabled(false);

pb_disconnect=new QPushButton;
pb_disconnect=ui->pushButton_4;
pb_disconnect->setEnabled(false);
pb_disconnect->setText("DISCONNECT");



socket=new QTcpSocket(this);

connect(pb_connect,SIGNAL(clicked()),this,SLOT(con necttoserver()));
connect(pb_send,SIGNAL(clicked()),this,SLOT(write( )));
connect(pb_send1,SIGNAL(clicked()),this,SLOT(write 1()));
connect(pb_disconnect,SIGNAL(clicked()),this,SLOT( DisconnectServer()));
connect(socket,SIGNAL(connected()),this,SLOT(on_co nnected()));
connect(socket,SIGNAL(readyRead()),this,SLOT(read1 ()));

}

TCPClient::~TCPClient()
{
delete ui;
}

void TCPClient::changeEvent(QEvent *e)
{
QWidget::changeEvent(e);
switch (e->type()) {
case QEvent::LanguageChange:
ui->retranslateUi(this);
break;
default:
break;
}
}

void TCPClient::connecttoserver()
{
QHostAddress hAddr;
hAddr.setAddress("192.168.64.55");
socket->connectToHost(hAddr,1234);

}

void TCPClient::on_connected()
{
pb_connect->setEnabled(false);
pb_send->setEnabled(true);
pb_send1->setEnabled(true);
qDebug("Connection established");
pb_disconnect->setEnabled(true);
le->setEnabled(true);
le1->setEnabled(true);
te->setEnabled(true);
te1->setEnabled(true);
}

void TCPClient::write()
{
QString Data="",Data1=this->le->text();
CHAT="1";
Data.append(CHAT);
Data.append(Data1);
socket->write(Data.toUtf8().constData());
qDebug("Client Sent 1 to Server");

}
void TCPClient::write1()
{
QString Data="",Data1=this->le->text();
CHAT="2";
Data.append(CHAT);
Data.append(Data1);
socket->write(Data.toUtf8().constData());
qDebug("Client Sent 2 to server");

// socket->flush();

}

toufic.dbouk
13th November 2013, 09:31
Where are you using your listen() method ?
Check if isListening returns true.
And check if the newConnection signal is fired when a client connects , to see if you are actually connecting to your server.
Its not easy to read a code from iphone lol my post might be useless.


Good Luck.

Vivek1982
13th November 2013, 09:47
Im using Listen() in one of the child window called Team Status. While compiling Im getting Debug msgs of Listen function. Im not gettin debug msg of New client or Connection accepted msg on the Application output window. earlier When i created one server and one client it works fine. when initialise for two or more clients in the server by


void TeamStatus::incomingConnection(int socketfd) // Public slot in the teamstatus.H file
{{
QTcpSocket *socket = new QTcpSocket;

socket=server->nextPendingConnection();
if((socket->state()==QTcpSocket::ConnectedState))
{
socket->setSocketDescriptor(socketfd);

qDebug("new connection1 established");

}


client


connect(pb_connect,SIGNAL(clicked()),this,SLOT(con necttoserver()));
connect(socket,SIGNAL(connected()),this,SLOT(on_co nnected()));
void TCPClient::connecttoserver()
{
QHostAddress hAddr;
hAddr.setAddress("192.168.64.55");
socket->connectToHost(hAddr,1234);

void TCPClient::on_connected(int descriptor) //public slot of client.h

{
socket=new QTcpSocket(this);
socket->setSocketDescriptor(descriptor);


connect(pb_send,SIGNAL(clicked()),this,SLOT(write( )));
connect(pb_send1,SIGNAL(clicked()),this,SLOT(write 1()));
connect(pb_disconnect,SIGNAL(clicked()),this,SLOT( DisconnectServer()));

connect(socket,SIGNAL(readyRead()),this,SLOT(read1 ()));
}

void TCPClient::write()
{
QString Data="",Data1=this->le->text();
CHAT="1";
Data.append(CHAT);
Data.append(Data1);
socket->write(Data.toUtf8().constData());
qDebug("Client Sent 1 to Server");

}
}

If can't later also u can look at the code then reply..
Thanks

toufic.dbouk
13th November 2013, 10:10
Is the newConnection signal getting fired ?
Connect it to a slot where you accept the connection.

anda_skoa
13th November 2013, 11:22
Connect it to a slot where you accept the connection.
Exactly.

You can't just create a QTcpServer object and expect that it magically knows how to invoke the function in which you call nextPendingConnection().

Programming can look like magic but it isn't :)

Cheers,
_

stampede
13th November 2013, 12:20
Btw. you have a memory leak here:


QTcpSocket *socket = new QTcpSocket; // this object will never be deleted
socket=server->nextPendingConnection();// because you overwrite the only pointer to it here

Vivek1982
13th November 2013, 13:26
ya.. I have given a condition If already socket is accepted/connected. process the debug like new connection established


socket=server->nextPendingConnection();
if((socket->state()==QTcpSocket::ConnectedState))

I feel somewhere I'm going wrong when in creating a socket descriptor while assigning for connecting with many clients.
or using QSet in teamstatus.h for creating more sockets.

anda_skoa
13th November 2013, 13:55
There is no need to create a socket descriptor, QTcpServer::nextPendingConnection() creates a fully constructed QTcpSocket object.

Cheers,
_

toufic.dbouk
13th November 2013, 14:51
Connect the newConnection signal to a newConnection Slot and accept the connection or use your code :


QTcpSocket *socket = new QTcpSocket;
socket=server->nextPendingConnection();
if((socket->state()==QTcpSocket::ConnectedState))

Good Luck.

Vivek1982
14th November 2013, 09:09
Hi..

I'm trying built application handling multiple clients by one server Qt Application. I have tried all suggestion to change the code etc.plz have a look on my unsderstanding of this concept of Multiple client in one server. how I'm approaching/trying may be wrong plz correct me.
Actually Compiling server application and client on same PC. When Server has initiated, first Client will work fine till second client will join, after that first client data is not accepted by server/not reading the first client data.It will stay on to the second client only.
.SERVER.cpp


SERVER::SEVER(QWidget *parent) :
QMainWindow(parent),
ui1(new Ui::SERVER)
{
ui1->setupUi(this);
server=new QTcpServer(this);
connect(team_but,SIGNAL(clicked()),this,SLOT(liste n()));
connect(server,SIGNAL(newConnection()),this,SLOT(o n_newconn()));

void Server::listen()


{
QHostAddress hAddr;
hAddr.setAddress("192.168.64.55");
server->listen(QHostAddress::Any,1234);
qDebug("Listening to new connection");
}

void Server::on_newconn()
{
QTcpSocket *socket= new QTcpSocket;

socket=server->nextPendingConnection();
if((socket->state()==QTcpSocket::ConnectedState))
{
qDebug("new connection1 established");
}
connect(socket,SIGNAL(readyRead()),this,SLOT(read_ socket()));
}

void Server::read_socket()
{
{
QByteArray buffer= socket->readLine();

if(buffer.startsWith("1"))
{
QByteArray buf=buffer.mid(1);
qDebug()<<buf;
}
}
else
{
//something to do
}}

SERVER.H



private:
Ui::Server *ui1;
QTcpServer *server;
QTcpSocket *socket;
QPushbutton *team_but

private slots:
void listen();
void on_newconn();
void read_socket();
}

CLIENT.cpp


Client::Client(QWidget *parent) :
QWidget(parent),
ui(new Ui::Client)
{
ui->setupUi(this);

socket=new QTcpSocket(this);
connect(pb_connect,SIGNAL(clicked()),this,SLOT(con necttoserver()));
connect(socket,SIGNAL(connected()),this,SLOT(on_co nnected()));
}
void Client::connecttoserver()
{
QHostAddress hAddr;
hAddr.setAddress("192.168.64.55");
socket->connectToHost("192.168.64.55",1234);
// socket->connectToHost("192.168.64.52",1234);
}
void Client::on_connected()
{
qDebug("Connected");
}

void Client::write()
{
//Data is lineedit text
socket->write(Data.toUtf8().constData());
}



Client.H


private:
Ui::TCPClient *ui;
QTcpSocket *socket;

private slots:
void connecttoserver();
void write();
void on_connected();


i hope Compiling second client on same system(PC) is not issue here.

anda_skoa
14th November 2013, 11:11
First, get rid of


QTcpSocket *socket= new QTcpSocket;

in Server::on_newconn(). It needlessly creates a QTcpSocket and then loses the pointer to it -> leak



QTcpSocket *socket = server->nextPendingConnection();


in Server::read_socket() you first have to get the socket that emitted the signal, you currently assume that only the last connected client will send data


QTcpSocket *socket = qobject_cast<QTcpSocket*>( sender() );


Alternatively create a "client handler" object for each client's socket, i.e. a class that handles one client connection.

Cheers,
_

wysota
14th November 2013, 13:23
I think one of your issues is that you expect that if you write something on the server, it should be received by all clients, is that correct? For that you need to iterate over all client sockets and write the same data to all client sockets (once you sort out your problems with establishing proper connections).

Vivek1982
14th November 2013, 14:48
Ya.. exactly.. my requirement is many clients will connect to server. Send data/Character as data on socket->write. After server receives a query from client, In general It will the word to all clients.Let me try now, How others can be addresses first, later I will plan for server writing to client, Now I need rectify, server accepting many client issue.



Alternatively create a "client handler" object for each client's socket, i.e. a class that handles one client connection.
Cheers,

this has to be done at server alternatively right

toufic.dbouk
14th November 2013, 18:06
Yes it should be done on the server side of course. If you recall to my first post i mentioned that you should make a class user that handles the client connection including streams and sockets.

Good Luck.

Vivek1982
15th November 2013, 05:18
Dear All,

I thank you all and Qt Forum.Finally I got the output, Server is able to listen for many clients, based on the data sent. Server is executing small functions, based on the data from client. As earlier discussed. Now I can connect two or more clients. Actually, My complete Application is , get the data from all connected clients. Based on that data sent from each client. Server will send a data/msg to all connected clients.

Here,Server code is having one socket. while it has to send data for all clients. if I call directly Write(), by QPushbutton activity Is that okay. Or in the write function I need to set some parameter to address all Or in my client application i need to change?




connect(pb_clicked,SIGNAL(clicked()),this,SLOT(wri te());
void Server::write()
{
QString data=this->le->text();
qDebug()<<data;
socket->write(data.toUtf8().constData());
qDebug("server is sending msgs to client");

}

Client.cpp


connect(pb_clicked,SIGNAL(clicked()),this,SLOT(con nectoserver())
connect(socket,SIGNAL(connected()),this,SLOT(on_co nnected())
connect(socket,SIGNAL(readyRead()),this,SLOT(read( )));
void TcpClient::connectoserver()
{
//connect to server
}
void TcpClient::on_connected()
{ qDebug("Connected to server");
}
void TcpClient::readsocket()
{
}
void TcpClient::read()
{ qDebug("start reading the socket");
QByteArray buffer= socket->readLine();
qDebug("reading the socket ");
te->setText(buffer);
}

toufic.dbouk
15th November 2013, 09:03
Here,Server code is having one socket. while it has to send data for all clients.
Do you mean that you have one socket sending data to several clients from the server side? And you want to call write() to send data for all clients by THAT SAME socket ?

Vivek1982
15th November 2013, 10:07
Not exactly at the same time, two or more clients will send data to server. Server will analyse and process the data send by all the clients. Later Server will send data to all connected clients. After receiving the data from server, All the clients update the info/data sent by the server in all Client Application.

Vivek1982
15th November 2013, 13:22
After searching in this Qt Forum, I made attempt to write data on server by using below mentioned code, but it was not working.

void Server::writetoallclients()
{
QString Data="2allclients";
foreach(QtcpSocket *socket,socket)
{
socket->write(Data.toUtf8().constData());
qDebug("data sent to all Clients");
}
}



void Client::read()
{
qDebug("start reading the socket");
QByteArray buffer= socket->readLine();
qDebug("data rxcd from server ");

Vivek1982
15th November 2013, 17:04
Dear all.. Waiting for reply...application is at final stage..I can compete soon

anda_skoa
15th November 2013, 18:25
If you want to send to all clients, you need to keep all incoming sockets, e.g. in a list, and when sending you iterate over that container and call write on all sockets.

Cheers,
_

Vivek1982
15th November 2013, 20:12
Thanks for the reply.... I will give my attempt. . Any alternate idea to get this done by seeing my earlier posts is most welcome to all

toufic.dbouk
15th November 2013, 21:12
I dont think there is any other idea to do that because to send to one client you have to send the data from server through the connected socket. Hence to send data to all clients you have to send the data through all connected sockets. Iterating over the sockets is a good idea as posted above by anda_skoa.
You can give it an attempt save all sockets in a container of some type and add sockets to it through nextPendingConnection since it will return the connected socket and then make a method for example sendToAll() where you iterate over the items and send the message one by one.

Good Luck.

Vivek1982
16th November 2013, 04:52
Ok... I hope this may work by QList<>.
In my new connection slot I need to try like
QList<QTcpsocket*,sockets>=new QTcpsocket.
But after declaring as aQList.we have to set socket descriptor or not required? Then we can try to sendtoall().

Okay now this may work for data to all the clients.... As mentioned above post if I need to send data for specific client.not suddenly at the same time to respective client who has sent data to server...later also...how this process will happen?

Vivek1982
16th November 2013, 08:43
As suggested I have used QList<> in server application to address all or send data to all clients. Now, all clients are able to send/read data.but, all clients all getting data twice.Any problem in my write function of Server


void Server::on_newconn()
{
QTcpSocket *socket = server->nextPendingConnection();
if((socket->state()==QTcpSocket::ConnectedState))
sockets.append(socket);
connect(socket,SIGNAL(readyRead()),this,SLOT(read_ socket()));
}
void Server::write()
{
QString data=this->le->text();
foreach(QTcpSocket* socket,sockets)
{
socket->write(CHAT.toUtf8().constData());
}
qDebug("server is sending msgs to all clients");

}




void TCPClient::read1()
{

QByteArray buffer= socket->readLine();
te->setText(buffer);

}

anda_skoa
16th November 2013, 09:59
Thanks for the reply.... I will give my attempt. . Any alternate idea to get this done by seeing my earlier posts is most welcome to all

Well, alternatively to iterating over a container one could make a client handler object for each socket that has a slot for sending and have the chat text source emit a matching signal when text should be sent.

Basically this is the same as iterating, just done internally by the signal/slot system.

Cheers,
_

Vivek1982
16th November 2013, 11:12
I have got output where many clients can be connected to one server, two way data can be sent means from server its multi cast to all connected clients...but in client now data is coming twice in line edits.how it can be solved
In post 29...in write() its data not CHAT

toufic.dbouk
16th November 2013, 11:34
How many connected clients do you have?
Did you check how many times is the for loop iterating?

Vivek1982
16th November 2013, 13:39
No...wat ever is the clients no, but out put is coming twice in lineedit s...I checked based on no of client connected.if no of clients is one or two or four...still text wat I send in server line edit...its appearing twice in Clint application

toufic.dbouk
16th November 2013, 23:16
Check what's in the QByteArray buffer at the client side, is the message also saved twice there ?

Vivek1982
17th November 2013, 08:39
Ya.. I checked fully,,, in my server side data of linedit is sent only once across all the clients, but where as in the client side QByteArray buffer, data is coming twice.
E.G: Server: lineedit=HI
Client:textedit=HIHI


void TCPClient::read1()
{

QByteArray buffer= socket->readLine();
qDebug()<<buffer;
te->setText(buffer);
}




void Sever::on_pushButton_clicked()
{
QString data=this->le->text();
qDebug()<<data;
foreach(QTcpSocket* socket,sockets)
{
socket->write(data.toUtf8().constData());
qDebug()<<data;
}
qDebug()<<data;
qDebug("server is sending msgs to clients");

}

Vivek1982
17th November 2013, 17:24
Any problem in my coding.....I'm checking for solutions all the time but no reply...why my client application receiving twice

toufic.dbouk
17th November 2013, 20:51
Attach the whole project as a zip in order to investigate the problem.

Vivek1982
18th November 2013, 12:57
Hi.. I have attached the complete project, how to address?.. data saving twice problem is resolved some how. plz go through the attachment. Plz correct me if i have gone wrong me in coding. Initially I request to read Application Text file once, before compiling code to understand the Project.

anda_skoa
18th November 2013, 14:05
Well, there are a couple of bugs but the network stuff seems to work for me.
Any string sent by the server is only received once by each client.

Couple of bugs:
* SLOT((on_conn())) has one pair of () to much, should be SLOT(on_conn())
* you are creating invisible widgets and then throw away their pointers. waste of resources
* send buttons in client main window are connected cross-wise, i.e. the left button sends the right line edits contents

Cheers,
_

Vivek1982
18th November 2013, 14:30
Plz can I get solution fixed for this... Or atleast line level solution where I need to work to get solution....I'm getting more confused with couple of bugs...please

Vivek1982
18th November 2013, 16:38
Okay I will remove that one extra bracket at slot new connection, regarding string if once it is received byeach client is okay to process the data, wasting of pointers resource while sending data for that I will remove const.data while sending in socket and cross connection at client application also I will change it then I will will reply about the result wat I will get

anda_skoa
18th November 2013, 17:17
The "wasting resources" refers to code like this


te1=new QTextEdit;
te1=ui->textEdit;


Here you create a QTextEdit and immediately lose its pointer by assigning another text edit's pointer to the variable.

Just


te1=ui->textEdit;

or replace all usages of


te1->

with


ui->textEdit->


Cheers,
_

Vivek1982
18th November 2013, 19:51
Okay sure I'm going to replace all the things which is specified here...but ultimately my program logic or purpose is not having problem I hope....I have a small query here as you mentioned server sends only once data to all connected clients where as many clients will send data to server ,there is any problem in my logic all the time calling write () function from server to client or clients/clients to server at a, time

anda_skoa
19th November 2013, 09:44
I am afraid I don't understand your question.

Cheers,
_

Vivek1982
19th November 2013, 11:24
Thanks alot for the inputs, I resolved all bugs as you have mentioned in your last reply. I'm working on it now. once if this done. I will try to address that server will send data to each client specifically.Let me finish this first. Thanks for all

toufic.dbouk
19th November 2013, 14:08
I will try to address that server will send data to each client specifically.Let me finish this first.
Do you mean that want to send data from server to one specific client ?
Good work on making your project work so far.

Good Luck.

Vivek1982
19th November 2013, 17:35
Thanks Alot...let me finish first and then I will approach for specific client communication from server.. I need a support for that...always I wait for a reply
from forum to analyse or understand whether I'm doing right or wrong... I'm very happy that forum has given me lot of support to develop application....

patrik08
20th November 2013, 08:39
I found a nice code for the goal waht you need on

Creating a multithreading QTcpServer

http://www.qt-coding.com/2013/10/09/creating-a-multithreading-qtcpserver-part-iii-and-last/

from a spanish men..




git clone https://github.com/francescmm/qt-coding.git

anda_skoa
20th November 2013, 11:15
I would really, strongly, advise against going into multithreading at this stage. It is completely unnessary and makes things a lot more complicated.

Cheers,
_

Vivek1982
20th November 2013, 18:24
Hi...can I make my small project as a package for Demo...how this can be done...A System where Qt is not installed..in that PC if we want run any qt application..what are the prerequisite procedure...

anda_skoa
21st November 2013, 09:31
http://qt-project.org/doc/qt-4.8/deployment-windows.html

Cheers,
_

Vivek1982
25th November 2013, 12:15
Dear All,

I need to send data to the server, which may have headers, based on the header type. Data will be sent to respective class or lineedit or text edit. But in my client application the entire data is sent across the socket. But, where as in server side I need to check the headers then handle or processs the data.


Void Client:: Write()
{
{
QByteArray DATA="001";
QString linedata=ui->lineedit->text();
DATA.append(linedata);
socket.write(DATA.toUf8.constdata());
}
{QByteArray DATA="002";
QString linedata=ui->lineedit_1->text();
DATA.append(linedata);
socket.write(DATA.toUf8.constdata());
}
}




Void Server::read()
{
{
QTcpSocket *socket = qobject_cast<QTcpSocket*>( sender() );
{
QByteArray buffer= socket->readLine();

{
if(buffer.startsWith("001"))
QByteArray buf=buffer;
qDebug()<<buf;
buff=buffer.mid(3)
Lineedit->setText(buff)
qDebug()<<buf;
}
{
if(buffer.startsWith("002"))
QByteArray buf=buffer;
buff=buffer.mid(3)
Lineedit->setText(buff)
}



Here the problem is when socket is read at server side. In QDebug im getting complete data like "001hello002hi"

in the Lineedit of server complete line is printed (hello002hi) instead of giving only "hello" and actually by checking with "002" next lineedit_2 as "hi" msg to lineedit_2. How can i address this.

toufic.dbouk
25th November 2013, 14:09
Thats how TCP works.
You will have to define your own protocol or send sufficient info with the packets to be able to analyse it before processing it.


Good Luck.

anda_skoa
25th November 2013, 19:36
Here the problem is when socket is read at server side. In QDebug im getting complete data like "001hello002hi"

You are not sending a new line but you are trying to read lines.

Cheers,
_

Vivek1982
26th November 2013, 04:26
How to insert new line please. Or Can i define the length of the data or packet to be processed every time.regarding new line I had appended "/n" at last of the data. but in the output also /n printed.I server Im calling readLine. while reading can i limit the reaqding size.. If possible, plz let me know

anda_skoa
26th November 2013, 21:11
\n is a valid newline character, but it seems you already found that out yourself.

Also, as toufic.dbouk already suggested (and now also discovered by yourself?) using more protocol protocol, i.e. send size information alongside the channel information that you already have.

Cheers,
_

Vivek1982
27th November 2013, 09:37
okay.. I resolved the problem by storing data in buffer and some classes to check the data in the buffer, if data is valid it will updated on the GUI.No problem in this, its working now. I need a suggestion on my new Query.. Can we insert a image on image something like my first image is GIS MAP (screen shot of google maps after zoomin it to my location). Now there is anyway option or idea to insert a pushpin on the Image at the background. similarly like google map, inserting red pushpin for any location. How to insert pushpin image on map image(screen shot of google map).

anda_skoa
27th November 2013, 19:22
If you have a new question, create a new thread.

Cheers,
_