linux下C++网络编程
各为大侠,。。我刚刚毕业。。到公司上班有己天了。。。公司要我些一个多线程的测试程序
要求是:单客某一户端发送消息给服务端时。服务端把这个客户发送来的信息 转发给其他的客户,,发送这信息的客户不发
主要代码如下:
#include "Server.h"
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <iostream>
#include <string.h>
using namespace std;
//------------------------------------------------------------------
Server::Server() //初始化互斥事件,信号量
{
pthread_mutex_init(&mutex,NULL);
sem_init(&sem_r,0,10);
sem_init(&sem_s,0,0);
}
//------------------------------------------------------------------
void Server::Start()
{
pthread_create(&pth_s,NULL,SendMessageThread,this);
Initialize();
}
//------------------------------------------------------------------
void* Server::SendMessageThread(void* param)
{
Server* server = (Server *)param;
server->SendMessage(server);
return NULL;
}
//------------------------------------------------------------------
void Server::SendMessage(Server* server)
{
while(1)
{
sem_wait(&sem_s);
int list_len = list.GetLength();
int tNewfd,tReceivefd;
Data data_nod;
//pthread_mutex_lock(&mutex);
//// pData = queue.OutQueue();
data_nod=Message_nod.MessageOut();
//int queue_len = queue.Queuelength();
//pthread_mutex_unlock(&mutex);
tReceivefd =data_nod.fd;
//cout << "Received fd = " << tReceivefd << endl;
pthread_mutex_lock(&mutex);
for(int i = 1; i <= list_len; i++) //判断是不是本机如果不是就发送
{
list.GetNodeData(i,tNewfd);
if(tNewfd != tReceivefd)
{
write(tNewfd,data_nod.Msge,sizeof(data_nod.Msge));
cout << "Send to client successful! fd = " << tNewfd << endl;;
}
//}
}
//// delete pData;
pthread_mutex_unlock(&mutex);
sem_post(&sem_r);
}
}
//------------------------------------------------------------------
void Server::Initialize() //套结字初始化
{
sock_fd = socket(AF_INET,SOCK_STREAM,0);
if(sock_fd < 0)
{
perror("socket fail!" );
exit(-1);
}
servaddr.sin_family = AF_INET;
servaddr.sin_port = ntohs(atoi(PORT));
servaddr.sin_addr.s_addr=htonl(INADDR_ANY);
bzero(&(servaddr.sin_zero), 8);
if (bind(sock_fd, (struct sockaddr*) &servaddr,sizeof(struct sockaddr)) < 0)
{
perror("bind fail! ");
exit(-1);
}
if(listen(sock_fd,MAX_Num) < 0)
{
perror("listen fail!" );
exit(-1);
}
cout << "listenning......" << endl;
socklen_t sin_size = sizeof(desetaddr);
while(1)
{
new_fd = accept(sock_fd,(struct sockaddr *)&desetaddr,&sin_size);
if(new_fd < 0)
{
perror("accept fail!" );
exit(-1);
}
cout << "\nA client has connected to me "
<< inet_ntoa(desetaddr.sin_addr)
<< ":" << ntohs(desetaddr.sin_port)
<< endl;
pthread_mutex_lock(&mutex);
list.InsertNode(new_fd);
pthread_mutex_unlock(&mutex);
pthread_create(&pth_r,NULL,ReadMessageThread,this); //每当一个用户连接时就创建一个读数据的线程
}
}
//------------------------------------------------------------------
void* Server::ReadMessageThread(void* param)
{
Server* server = (Server *)param;
server->ReadMessage(server);
return NULL;
}
//------------------------------------------------------------------
void Server::ReadMessage(Server* server)
{
int fd = server->new_fd;
char buf[MSG_SIZE];
int len;
while(1)
{
sem_wait(&sem_r);
if ((len = read(fd, buf, MSG_SIZE)) == -1)
{
perror("read fail!");
pthread_exit(NULL);
}
else if (len == 0)
{
cout << "Current client has disconnected to me" << endl;
list.DeleteNode(fd);
pthread_exit(NULL);
}
buf[len] = '\0';
Data date;
date.fd=fd;
strcpy(date.Msge,buf);
cout << "\nRECEIVE: " << buf
<< " receive fd = " << fd << endl;
//pthread_mutex_lock(&mutex);
Message_nod.MessageInsert(date); //把客户端发过来的信息存入Message里面
//pthread_mutex_unlock(&mutex);
//delete data;
sem_post(&sem_s);
}
}
//------------------------------------------------------------------
Server::~Server()
{
//close(sock_fd);
//pthread_join(pth_r,NULL);
// pthread_join(pth_s,NULL);
}
void Server::GetPort_Max_Num(char *str,char * Max)
{
strcpy(str,PORT);
MAX_Num=atoi(Max);
}
//---------------------------------------------------------------------
//int Server::charT0Int(char *str)
//{
//}
#include <iostream>
#include "Server.h"
int main(int argc,char *argv[])
{
char Pt[10],Max[3];
Server *server = new Server();
cout<<"please input Port!"<<endl;
cin>>Pt;
cout<<"please input Max nuber!"<<endl;
cin>>Max;
server->GetPort_Max_Num(Pt,Max);
server->Initialize();
return 0;
}
上面有一个list 是存fd的
message 书存信息的
我可以肯定客户段没问题。。我测试过了。。