- 论坛徽章:
- 0
|
#ifndef MY_CTCPSERVER
#define MY_CTCPSERVER
#include sys/types.h>
#include sys/socket.h>
#include netinet/in.h>
#include unistd.h>
class CTcpServer
{
public:
CTcpServer();
int Bind(unsigned int IP,unsigned short bindPort );//默认为eth0的IP
int Bind(const char* IP,unsigned short bindPort);
int Listen(int listenNum);
int Accept();
int SendData(void *data, unsigned int length);
int RecvData(void *data, unsigned int length);
int Close();
private:
unsigned int get_ip();
int sock_fd;
int client_fd;
struct sockaddr_in server_addr;
struct sockaddr_in client_addr;
};
#endif
#include "CTcpServer.h"
#include stdlib.h>
#include string.h>
#include stdio.h>
#include errno.h>
#include assert.h>
#include arpa/inet.h>
#include net/if.h>
#include sys/ioctl.h>
CTcpServer::CTcpServer()
{
memset(&server_addr,0,sizeof(server_addr));
memset(&client_addr, 0, sizeof(client_addr));
sock_fd = -1;
client_fd = -1;
}
int CTcpServer::Bind(unsigned int IP,unsigned short bindPort)
{
server_addr.sin_family = AF_INET; /* 协议名 */
if(IP == 0 )
{
IP = get_ip();
if(IP == 0)
return -1;
}
server_addr.sin_addr.s_addr = htonl(IP); /* 默认eth0的IP*/
server_addr.sin_port = htons(bindPort); /* 端口号 */
if(sock_fd!=-1)
close(sock_fd);
if((sock_fd = socket(AF_INET, SOCK_STREAM, 0)) 0)
{
perror(strerror(errno));
return -1;
}
if(bind(sock_fd, (struct sockaddr*)&server_addr, sizeof(struct sockaddr_in)) 0 )
{
perror(strerror(errno));
return -1;
}
return 1;
}
int CTcpServer::Bind(const char* IP, unsigned short bindPort )
{
server_addr.sin_family = AF_INET; /* 协议名 */
unsigned int iIP;
if(IP == NULL)
{
iIP = get_ip();
if(iIP == 0)
return -1;
server_addr.sin_addr.s_addr = htonl(iIP);
}
else
server_addr.sin_addr.s_addr = inet_addr(IP); /* 默认eth0的IP*/
server_addr.sin_port = htons(bindPort); /* 端口号 */
if(sock_fd!=-1)
close(sock_fd);
if((sock_fd = socket(AF_INET, SOCK_STREAM, 0)) 0)
{
perror(strerror(errno));
return -1;
}
if(bind(sock_fd, (struct sockaddr*)&server_addr, sizeof(struct sockaddr_in)) 0 )
{
perror(strerror(errno));
return -1;
}
return 1;
}
unsigned int CTcpServer::get_ip()
{
int sock;
struct sockaddr_in sin;
struct ifreq ifr;
sock = socket(AF_INET, SOCK_DGRAM, 0);
if (sock == -1)
{
perror(strerror(errno));
return 0;
}
strncpy(ifr.ifr_name, "eth0", sizeof(ifr.ifr_name));
if (ioctl(sock, SIOCGIFADDR, &ifr) 0)
{
perror(strerror(errno));
return 0;
}
memcpy(&sin, &ifr.ifr_addr, sizeof(sin));
close(sock);
return sin.sin_addr.s_addr;
//memcpy(ip_addr,inet_ntoa(sin.sin_addr),strlen(inet_ntoa(sin.sin_addr)));
}
int CTcpServer::Listen(int listenNum)
{
if(listen(sock_fd, listenNum) 0 )
{
perror(strerror(errno));
return -1;
}
return 1;
}
int CTcpServer::Accept()
{
socklen_t client_len;
client_fd = accept(sock_fd, (struct sockaddr*)&client_addr, &client_len);
if(client_fd 0)
{
perror(strerror(errno));
return -1;
}
return 1;
}
int CTcpServer::SendData(void *data, unsigned int length)
{
int send_len = 0;
if((send_len = send(client_fd, data, length, 0)) 0 )
{
perror(strerror(errno));
return -1;
}
else
{
return send_len;
}
}
int CTcpServer::RecvData(void *data, unsigned int length)
{//接收数据即返回
int rev_len = 0;
if((rev_len = recv(client_fd, data, length, 0)) 0)
{
perror(strerror(errno));
return -1;
}
else
return rev_len;
}
int CTcpServer::Close()
{
if(sock_fd!=-1)
{
close(sock_fd);
sock_fd = -1;
}
return 1;
}
#ifdef DEBUG
int main()
{
unsigned char buffer[1024]={'\n'};
CTcpServer myserver;
// unsigned int ip = 0;
myserver.Bind("192.168.1.198",22082);
myserver.Listen(10);
myserver.Accept();
while(1)
{
myserver.RecvData(buffer,1024);
myserver.SendData((void*)"hello client",13);
printf("server:%s\n",buffer);
sleep(2);
}
}
#endif
g++ -o tcp_s CTcpServer.cpp -Wall -DDEBUG
本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u3/108358/showart_2126430.html |
|