linux下C++SocketTCP编程---服务器端

NetServer.h

#ifndef NETSERVER_H_
#define NETSERVER_H_

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/ioctl.h>
#include <iostream>
using namespace std;
class NetServer {
public:
	NetServer();
	virtual ~NetServer();
	void CreateConnect();
	void CheckConnect();
	void RevHello(char* str,int para[]);
	void SendHelloToo();
protected:
	void AcceptConnect();
	void HandlePacket();
	bool CheckBuff(char* str,int num);
	void DividePacket(char* str);
	void HandlePacketHead(char** str,int param[]);
	void SendPacket(string str);
private:
#define MAXBUF 1024
	char buff[MAXBUF];
	int sockfd,clientfd;
	sockaddr_in clientAddr;
	socklen_t len;
	bool connectFlag;
};
#endif /* NETSERVER_H_ */


 

NetServer.cpp

#include "NetServer.h"

#define defPort 7788
NetServer::NetServer()
{
}
NetServer::~NetServer()
{
	close(sockfd);
}
void NetServer::CreateConnect()
{
	sockaddr_in serverAddr;
	connectFlag=false;
	if((sockfd=socket(PF_INET,SOCK_STREAM,0))==-1)
	{
		printf("socket created failly,errno=%d,errorInfo: %s\n",errno,strerror(errno));
		exit(1);
	}
	else
		printf("socket created successfully\n");

	bzero(&serverAddr,sizeof(serverAddr));
	serverAddr.sin_family=PF_INET;
	serverAddr.sin_port=htons(defPort);
	serverAddr.sin_addr.s_addr=htonl(INADDR_ANY);

	if(bind(sockfd,(sockaddr*)&serverAddr,sizeof(sockaddr))==-1)
	{
		printf("bind failly,errno=%d,errorInfo: %s\n",errno,strerror(errno));
		exit(1);
	}
	else
		printf("bind successfully\n");
	if(listen(sockfd,10)==-1)
	{
		printf("listen failly,errno=%d,errorInfo: %s\n",errno,strerror(errno));
		exit(1);
	}
	else
		printf("listen successfully\n");

	len=sizeof(struct sockaddr);
	int b_on = 1;
	ioctl (sockfd, FIONBIO, &b_on);
}
void NetServer::CheckConnect()
{
	if(connectFlag)
		HandlePacket();
	else
		AcceptConnect();
}
void NetServer::AcceptConnect()
{
	clientfd=accept(sockfd,(sockaddr*)&clientAddr,&len);
	if(clientfd==-1)
	{
		connectFlag=false;
		printf("accept failly,errno=%d,errorInfo: %s\n",errno,strerror(errno));
	}
	else
	{
		connectFlag=true;
		printf("server:got connection from %s,port %d,socket %d\n",inet_ntoa(clientAddr.sin_addr),ntohs(clientAddr.sin_port), clientfd);
		long b_on = 1;
		ioctl (clientfd, FIONBIO, &b_on);
	}
}
void NetServer::HandlePacket()
{
	char buff[MAXBUF];
	bzero(buff,MAXBUF);
	int res=recv(clientfd,buff,MAXBUF,0);
	if(res!=-1)
	{
		if(CheckBuff(buff,res))
			DividePacket(buff);
		else
			cout<<"\""<<buff<<"\""<<"  has illegal data!!!"<<endl;
	}
	else
		printf("recv failly,errno=%d,errorInfo: %s\n",errno,strerror(errno));
}
bool NetServer::CheckBuff(char* str,int num)
{
	//合法字符只有空格(32),_ (95)小写字母(97~122)和数字(48~57)
	for(int i=0;i<num;i++)
	{
		if(str[i]==32||str[i]==95)
			continue;
		else if(str[i]>=97&&str[i]<=122)
			continue;
		else if(str[i]>=48&&str[i]<=57)
			continue;
		else
			return false;
	}
	return true;
}
void NetServer::DividePacket(char* paraStr)
{
	char* strPtr[10];
	int paraList[10][10];
	for(int i=0;i<10;i++)
	{
		strPtr[i]=NULL;
		for(int j=0;j<10;j++)
		{
			paraList[i][j]='|';
		}
	}
	char* subStr=strtok(paraStr," ");
	int i=0,j=0;
	while(subStr!=NULL)
	{
		if(*subStr>=48 && *subStr<=57)
		{
			paraList[i-1][j]=atoi(subStr);
			cout<<"subdata: "<<subStr<<" paraList["<<i<<"]["<<j<<"]="<<paraList[i-1][j]<<endl;
			j++;
		}
		else
		{
			strPtr[i]=subStr;
			i++;
			j=0;
		}
		subStr=strtok(NULL," ");
	}
	for(int i=0;i<10;i++)
	{
		if(strPtr[i])
		{
			HandlePacketHead(strPtr+i,paraList[i]);
		}
	}
}
void NetServer::HandlePacketHead(char** str,int param[])
{
	if(strcmp(*str,"hello")==0)
	{
		RevHello(*str,param);
	}
	else if(strcmp(*str,"thankyou")==0)
	{
		cout<<"thankyou---------"<<param[0]<<endl;
	}
	else
	{
		cout<<"not useful info: "<<*str<<" para: "<<*param<<endl;
	}
}
void NetServer::RevHello(char* str,int para[])
{
	cout<<"receive:   "<<str<<" ";
	for(int i=0;i<10;i++)
	{
		if(para[i]!='|')// '|'==124
			cout<<para[i]<<" ";
//		else
//			cout<<"!!!"<<para[i]<<" ";
	}
	cout<<endl;
	cout<<"\ntwo para="<<para[1]<<endl;
	if(para[0]==1989)
		SendHelloToo();
}
void NetServer::SendPacket(string str)
{
	char buff[MAXBUF];
	bzero(buff,MAXBUF);
	strcpy(buff,str.data());
	int len=send(clientfd,buff,strlen(buff),0);
	if(len==-1)
		printf("send failly,errno=%d,errorInfo:%s",errno,strerror(errno));
	else
		printf("send successfully,InfoLen=%d,Info: '%s'.\n",len,buff);
}
void NetServer::SendHelloToo()
{
	SendPacket("women 789");
}
int main()
{
	NetServer NS;
	NS.CreateConnect();
	while(1)
	{
		NS.CheckConnect();
		sleep(2);
	}
	return 0;
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值