1、包含头文件
2、NETCLASEE定义
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/stat.h>
ypedef struct netmessage
{
int successFlag;
int clientSocket;
int serverSocket;
struct sockaddr_in clientAddr;
struct sockaddr_in serverAddr;
} NETMS;
typedef class netclass
{
public:
void NetInitS(int port);
void NetAceptS();
void NetInitC();
void NetConnectC(char ipaddress[] , int port);
int NetSend(char *buff, int bufflen);
int NetRecv(char *buff, int bufflen);
void NetClose();
protected:
NETMS netms;
}NETC;
void NETC::NetInitS(int port)
{
if((netms.serverSocket=socket(AF_INET, SOCK_STREAM,0))<0)
{
perror("tcp socket error");
netms.successFlag = 0;
}
else netms.successFlag = 1;
if(netms.successFlag == 1)
{
int reuseflag = 1;
if((setsockopt(netms.serverSocket, SOL_SOCKET, SO_REUSEADDR,
(char*)&reuseflag, sizeof(reuseflag)))==-1)
{
perror("tcp setsockopt error");
netms.successFlag = 0;
}
else netms.successFlag = 1;
}
if(netms.successFlag == 1)
{
bzero(&netms.serverAddr, sizeof(netms.serverAddr));
netms.serverAddr.sin_family = AF_INET;
netms.serverAddr.sin_port = htons(port);
netms.serverAddr.sin_addr.s_addr = INADDR_ANY;
if(bind(netms.serverSocket,
(struct sockaddr*) &netms.serverAddr,
sizeof(netms.serverAddr))==-1)
{
perror("tcp bind error");
netms.successFlag = 0;
}
else netms.successFlag = 1;
}
if(netms.successFlag == 1)
{
if(listen(netms.serverSocket, 30)<0)
{
perror("tcp listen error");
netms.successFlag = 0;
}
else netms.successFlag = 1;
}
}
void NETC::NetAceptS()
{
socklen_t addr_len = sizeof(struct sockaddr);
if((netms.clientSocket=accept(netms.serverSocket,
(struct sockaddr*)&(netms.clientAddr), &addr_len))<0)
{
perror("tcp accept error");
netms.successFlag = 0;
}
else
{
printf("server:connection client ip is %s, client port is %d.\n", inet_ntoa(netms.clientAddr.sin_addr), ntohs(netms.clientAddr.sin_port));
netms.successFlag = 1;
}
}
void NETC::NetInitC()
{
if((netms.clientSocket=socket(AF_INET, SOCK_STREAM, 0))<0)
{
perror("tcp socket error");
netms.successFlag = 0;
}
else netms.successFlag = 1;
if(netms.successFlag == 1)
{
int reuseflag = 1;
if(setsockopt(netms.clientSocket, SOL_SOCKET, SO_REUSEADDR, (char*)&reuseflag, sizeof(reuseflag))==-1)
{
perror("tcp setsockopt error");
netms.successFlag = 0;
}
else netms.successFlag = 1;
}
}
void NETC::NetConnectC(char ipaddress[], int port)
{
netms.serverAddr.sin_family=AF_INET;
netms.serverAddr.sin_port = htons(port); netms.serverAddr.sin_addr.s_addr=inet_addr(ipaddress);
if(connect(netms.clientSocket,
(struct sockaddr*)&netms.serverAddr,
sizeof(struct sockaddr))<0)
{
perror("tcp connect error");
netms.successFlag = 0;
}
else
{
printf("client:connection from server ip is %s, server port is %d.\n", inet_ntoa(netms.serverAddr.sin_addr), ntohs(netms.serverAddr.sin_port));
netms.successFlag = 1;
}
}
int NETC::NetSend(char *buff, int bufflen)
{
return send(netms.clientSocket, buff, bufflen, 0);
}
int NETC::NetRecv(char *buff, int bufflen)
{
return recv(netms.clientSocket, buff , bufflen, 0);
}
void NETC::NetClose()
{
close(netms.clientSocket);
close(netms.serverSocket);
netms.clientSocket = 0;
netms.serverSocket = 0;
netms.successFlag = 0;
}