vendredi 14 août 2015

c/c++ socket sending data after disconnection

Im wirting a socket code with c/c++ and socket.h library and my server and clients are stream socket with tcp protocol. I've got a class User that shows the clients connected.

class User
{
private:
int sockfd;
int n;
thread * Thread;
public:
User ( int sockfd);
void Get_Message();
void Join();  
~User();
};
User::User ( int sockfd)
{
this->sockfd=sockfd;
Thread=new thread(&User::Get_Message,this);
}
void User::Get_Message()
{
while (1)
{
readmessage(sockfd);
}
}
void readmessage (int sock)
{
int n=0;
char buffer[256];
bzero(buffer,256);
n = read(sock,buffer,255);
if (n < 0) 
{
 error("ERROR reading from socket ");
}

cout<<"the message\t"<<buffer<<endl;
} 

now this code works and several clients can join my server and send message;

but when one of those clients dissconnect the server it keeps printing "the message:" and I don't know why and how to stop it... I'll be greatful if anybody helps me why and how to fix it.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire