linux下C++SocketTCP编程---客户端

NetClient.h

#ifndef NETCLIENT_H_
#define NETCLIENT_H_
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <sys/socket.h>
#include <resolv.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <iostream>
using namespace std;
class NetClient {
public:
	NetClient();
	virtual ~NetClient();
	void RevHello(char* str,int para[]);
	void SendHi();
	void CreateConnect();
	void CheckConnect();
	void MyTest();
protected:
	void ConnectServer();
	bool CheckBuff(char* str,int num);
	void HandlePacket();
	void DividePacket(char* str);
	void HandlePacketHead(char** str,int param[]);
	void SendPacket(string strInfo);
private:
#define MAXBUF 1024
	int sockfd;
	struct sockaddr_in serverAddr;
	bool connectFlag;
};
#endif /* NETCLIENT_H_ */


NetClient.cpp

#include "NetClient.h"

#define defPort 7788   //ssh服务的端口号:22
char defIP[]="127.0.0.1";
NetClient::NetClient()
{
}
NetClient::~NetClient()
{
	close(sockfd);
}
void NetClient::CreateConnect()
{
	if((sockfd=socket(AF_INET,SOCK_STREAM,0))==0)
	{
		printf("create socket errno:%d, errerInfo:%s\n",errno,strerror(errno));
		exit(1);
	}
	else
		printf("socket created sucessfully\n");

	bzero(&serverAddr,sizeof(serverAddr));
	serverAddr.sin_family=AF_INET;
	serverAddr.sin_port=htons(defPort);
	serverAddr.sin_addr.s_addr=inet_addr(defIP);

	connectFlag=false;
	int b_on = 1;
	ioctl (sockfd, FIONBIO, &b_on);

	//设置socket属性为非阻塞方式
//	if(fcntl(sockfd,F_SETFL,O_NONBLOCK)==-1)
//		printf("fcntl errno:%d, errerInfo:%s\n",errno,strerror(errno));
}
void NetClient::CheckConnect()
{
	if(connectFlag)
		HandlePacket();
	else
		ConnectServer();
}
void NetClient::ConnectServer()
{
	if(connect(sockfd,(struct sockaddr*)&serverAddr,sizeof(serverAddr))!=0)
	{
		connectFlag=false;
		printf("connect erron:%d, errerInfo:%s\n",errno,strerror(errno));
	}
	else
	{
		connectFlag=true;
		printf("client connect to server successfully\n\n\n");
		int b_on = 1;
		ioctl (sockfd, FIONBIO, &b_on);
	}
}
void NetClient::HandlePacket()
{
	char buffer[MAXBUF];
	bzero(buffer,MAXBUF);
	int ret=recv(sockfd,buffer,sizeof(buffer),0);
	if(ret!=-1)
	{
		if(CheckBuff(buffer,ret))
			DividePacket(buffer);
		else
			cout<<"\""<<buffer<<"\""<<" has illegal data!!!"<<endl;
	}
	else
		cout<<"recv erron="<<errno<<"  errorInfo:"<<strerror(errno)<<endl;
}
bool NetClient::CheckBuff(char* str,int num)
{
	//合法字符只有空格(32),_ (95)小写字母(97~122)和数字(48~57)
	for(int i=32;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 NetClient::DividePacket(char* str)
{
	char* strHead[10];
	int paraList[10][10];
	for(int i=0;i<10;i++)
	{
		strHead[i]=NULL;
		for(int j=0;j<10;j++)
			paraList[i][j]='|';
	}
	char* subStr=strtok(str," ");
	int i=0,j=0;
	while(subStr!=NULL)
	{
		if(*subStr>=48 && *subStr<=57)
		{
			paraList[i-1][j]=atoi(subStr);
			j++;
		}
		else
		{
			strHead[i]=subStr;
			i++;
			j=0;
		}
		subStr=strtok(NULL," ");
	}
	for(int i=0;i<10;i++)
	{
		if(strHead[i])
			HandlePacketHead(strHead+i,paraList[i]);
	}
}
void NetClient::HandlePacketHead(char** str,int param[])
{
	if(strcmp(*str,"hello")==0)
	{
		RevHello(*str,param);
	}
	else if(strcmp(*str,"women")==0)
	{
		cout<<"hellotoo"<<param[0]<<endl;
		SendPacket("thankyou 9809");
	}
	else
	{
		cout<<"no matched Info: "<<*str<<"    para: "<<*param<<endl;
	}
}
void NetClient::SendPacket(string strInfo)
{
	char buffer[MAXBUF];
	bzero(buffer,MAXBUF);
	strcpy(buffer,strInfo.data());
	int len=send(sockfd,buffer,strlen(buffer),0);
	if(len==-1)
		printf("send errno=%d,errerInfo: %s\n",errno,strerror(errno));
	else
		printf("send successful: '%s' ,InfoLen=%d\n",buffer,len);
}
void NetClient::RevHello(char* str,int para[])
{
	cout<<"rev "<<str<<" ";
	for(int i=0;i<10;i++)
	{
		if(para[i]!='|')
			cout<<para[i]<<" ";
		else
			cout<<" -!-"<<para[i];
	}
	cout<<endl;
	cout<<"I rev info"<<endl;
}
void NetClient::SendHi()
{
	SendPacket("hello 1989 7812 hex 85");
}

void NetClient::MyTest()
{
	//初始化服务器端的ip和端口信息
	bzero(&serverAddr,sizeof(serverAddr));
	serverAddr.sin_family=AF_INET;
	serverAddr.sin_port=htons(defPort);

	//本地IP地址 转换成 网络ip
	//serverAddr.sin_addr.s_addr=inet_addr(defIP);
    serverAddr.sin_addr.s_addr=htonl(INADDR_ANY);
	cout<<"internet IP: "<<serverAddr.sin_addr.s_addr<<endl;
	cout<<"internet port: "<<serverAddr.sin_port<<"  ntohs"<<ntohs(serverAddr.sin_port)<<endl;
	if(inet_aton(defIP,(struct in_addr*)&serverAddr.sin_addr.s_addr)==0)
		printf("inet_aton errno:%d, errerInfo:%s\n",errno,strerror(errno));

	cout<<"123internet IP: "<<serverAddr.sin_addr.s_addr<<endl;
	cout<<"IP: "<<inet_ntoa(serverAddr.sin_addr)<<endl;
}
int main()
{
	NetClient NC;
	//NC.MyTest();
	NC.CreateConnect();
	while(1)
	{
		sleep(2);
		NC.CheckConnect();
		sleep(2);
		NC.SendHi();
	}
	return 0;
}



你可能感兴趣的:(socket)