Just look at the c++11 multi-threading, write a chat room try
Add c++11 and multi-threaded libraries when compiling
g++-wall-std=c++0x-pthread-o Server Server.cpp
Both the server and client are Q exits
? 1. [Code]server.cpp
#include <iostream>
#include <thread>
#include <arpa/inet.h>
#include <cstring>
#include <vector>
#include <algorithm>
#include <unistd.h>
using namespace Std;
Vector<int> Cli_vec;
int LISTENFD, port;
const char *IP = "127.0.0.1";
void Sendmsg (char* buf, int connfd) {
cout<< "Now send ..." <<endl;
char *data = BUF;
int Len;
for (auto I=cli_vec.begin (); I!=cli_vec.end (); ++i) {
if (*i = = CONNFD) continue;
if ((Len=send (*i, data, strlen (data), 0)) = =-1) {
cout<< "Send Error" <<endl;
}else{
cout<< "Send:" <<data<<endl;
}
}
}
void recvmsg (int connfd) {
int Len;
Char buf[100];
while (1) {
memset (buf, 0, sizeof (BUF));
cout<< "now receive ..." <<connfd<<endl;
if (len = recv (CONNFD, buf,sizeof (BUF), 0)) = = = 1) {
cout<< "Falid to receive" <<endl;
}
if (0 = = len) {
cout<<connfd<< "has exit the chatting" <<endl;
Auto pos = Find (Cli_vec.begin (), Cli_vec.end (), CONNFD);
if (pos! = Cli_vec.end ()) {
Cli_vec.erase (POS);
}
Break
}
Sendmsg (BUF,CONNFD);
}
}
void Waitquit () {
Char buf[100];
while (1) {
cin>>buf;
if (0 = = strcmp (buf, "Q")) {
for (auto I=cli_vec.begin (); I!=cli_vec.end (); i++) {
Close (*i);
}
Cli_vec.clear ();
Close (LISTENFD);
Break
}
}
Exit (0);
}
int main ()
{
if ((Listenfd=socket (af_inet,sock_stream,0)) = =-1) {
cout<< "Socket Error" <<endl;
return-1;
}
cout<< "Please enter the port number:";
cin>>port;
struct sockaddr_in serv_addr,cli_addr;
memset (&serv_addr, 0, sizeof (SERV_ADDR));
serv_addr.sin_family = af_inet;
Serv_addr.sin_port = htons (port);
SERV_ADDR.SIN_ADDR.S_ADDR = inet_addr (IP);
if (Bind (LISTENFD, (struct sockaddr*) &serv_addr, sizeof (serv_addr)) = =-1) {
cout<< "Bind error" <<endl;
return-1;
}http://www.enterdesk.com/special/dushen/?
if (Listen (LISTENFD, 10) = =-1) {
cout<< "Listen error" <<endl;
Return-1, God of Gamblers
}
Std::thread (Waitquit). Detach ();
cout<< "Server is running ..." <<endl;
while (1) {
size_t Cli_len = sizeof (CLI_ADDR);
memset (&cli_addr, 0, Cli_len);
int CONNFD = Accept (LISTENFD, (struct sockaddr*) &cli_addr, &cli_len);
if (CONNFD = =-1) {
cout<< "Accept Error" <<endl;
return-1;
}
cout<<connfd<< "has enter the chatting" <<endl;
Cli_vec.push_back (CONNFD);
Std::thread (Recvmsg, CONNFD). Detach ();
}
return 0;
}
2. [Code]client.cpp
#include <iostream>
#include <arpa/inet.h>/Socket related ...
#include <unistd.h>//close ()
#include <thread>
#include <string>
#include <cstring>
using namespace Std;
int sockfd;
struct sockaddr_in serv_addr;
void sendmsg (const string &msg) {
if ( -1 = = Send (SOCKFD, Msg.c_str (), Msg.size (), 0))
cout<< "Failed to send" <<endl;
}
void RMsg () {
int Len;
Char recv_buf[1024];
while (1) {
memset (recv_buf, 0, sizeof (RECV_BUF));
Len = recv (sockfd, Recv_buf, sizeof (RECV_BUF), 0);
if ( -1 = = len) {
cout<< "Failed to Recv" <<endl;
}
if (0 = = len) {
Close (SOCKFD);
cout<< "Disconnect from server!" <<endl;
Break
}
cout<< "Receive message:" <<recv_buf<<endl;
}
}
int main ()
{
string ip = "127.0.0.1";
int port;
cout<< "Please input server port:";
cin>>port;
SOCKFD = socket (af_inet, sock_stream, 0);
if ( -1 = = SOCKFD) {
cout<< "Failed to SOCKFD" <<endl;
return-1;
}
memset (&serv_addr, 0, sizeof (SERV_ADDR));
serv_addr.sin_family = af_inet;
Serv_addr.sin_port = htons (port);
SERV_ADDR.SIN_ADDR.S_ADDR = inet_addr (Ip.c_str ());
int stat = connect (sockfd, struct sockaddr *) &serv_addr,sizeof (SERV_ADDR));
if ( -1 = = stat) {
cout<< "Failed to connect" <<endl;
return-1;
}
Std::thread (RMSG). Detach ();
while (1)
{
String send_msg;
cin>>send_msg;
if (send_msg = = "Q")
{
cout<< "End client! "<<endl;
Break
}
Else
Sendmsg (SEND_MSG);
}
Close (SOCKFD);
return 0;
}
?
C++11 multithreaded chat room under Linux