Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2987920
  • 博文数量: 523
  • 博客积分: 11908
  • 博客等级: 上将
  • 技术积分: 5475
  • 用 户 组: 普通用户
  • 注册时间: 2009-04-03 15:50
文章分类

全部博文(523)

文章存档

2019年(3)

2013年(4)

2012年(71)

2011年(78)

2010年(57)

2009年(310)

分类: LINUX

2009-09-15 09:19:33


#ifndef        SERVERSOCKET__H
#define        SERVERSOCKET__H
 
#include "Common.h"
#define        SOCKERRO        -1
#define        SOCKSUCC        0
 
class ServerSocket
{
    private:
        int sin_size;
         int sockfd,new_fd;
         unsigned short port;
         struct sockaddr_in my_addr,their_addr,get_addr;
  
     public:
         ServerSocket();
         virtual ~ServerSocket();
         
         //创建套接字,参数为SOCK_DGRAM或SOCK_STREAM

         int Socket(int type);
         
         //设置监听端口

         void SetPort(unsigned short in_port);
         
         //绑定端口

         bool Bind();
         
         //设置返回端口

         void SetRemotePort(unsigned short in_port);
         
         //取得返回端口

         unsigned short GetRemotePort();
         
         //设置返回地址

         bool SetRemoteAddrees(const char *addrees);
         
         //取得返回地址

         void GetRemoteAddrees(char *addrees);
         
         //连接

         bool Connect();
         
         //监听

         void Listen();
         
         //获取新连接

         void Accept();
         
         //发送函数

         int Send(const char *buf,int len);
         int Sendto(const char *buf,int len);
         
         //接收函数

         int Recv(char *buf,int len);
         int Recvfrom(char *buf,int len);
         
         //关闭套接字

         void Close();
         
};
 
#endif 



# #include "ServerSocket.h"
#
# /*
# * 服务端Socket类
# */

#
# ServerSocket::ServerSocket(){
#
# }
#
# ServerSocket::~ServerSocket(){
#
# Close();
#
# }
#
# int ServerSocket::Socket(int type){
#
# sockfd = socket(AF_INET, type, 0);
# return sockfd;
#
# }
#
# void ServerSocket::SetPort(unsigned short in_port){
#
# my_addr.sin_family = AF_INET;
# port = in_port;
# my_addr.sin_port = htons(port);
# my_addr.sin_addr.s_addr = INADDR_ANY;
#
# }
#
# void ServerSocket::SetRemotePort(unsigned short in_port){
#
# their_addr.sin_port=htons(in_port);
#
# }
#
# unsigned short ServerSocket::GetRemotePort(){
#
# return ntohs(get_addr.sin_port);
#
# }
#
# bool ServerSocket::Bind(){
#
# if(bind(sockfd,(struct sockaddr *)&my_addr,sizeof(struct sockaddr))==-1)
# return false;
#
# return true;
#
# }
#
# int ServerSocket::Recvfrom(char *buf,int len){
#
# int numbytes;
# socklen_t addr_len = sizeof(get_addr);
# numbytes = recvfrom(sockfd,buf,len,0,(struct sockaddr *)&get_addr,&addr_len);
# if(numbytes == -1)
# {
# return false;
# }
# buf[numbytes] = '\0';
#
# return numbytes;
#
# }
#
# int ServerSocket::Sendto(const char *buf,int len){
#
# return sendto(sockfd,buf,len,0,(struct sockaddr *)&their_addr,sizeof(struct sockaddr));
#
# }
#
# bool ServerSocket::SetRemoteAddrees(const char* addrees){
#
#
# struct hostent *he;
# if((he = gethostbyname(addrees)) == NULL)
# return false;
#
# their_addr.sin_family = AF_INET;
# their_addr.sin_port = htons(port);
# their_addr.sin_addr = *((struct in_addr *)he->h_addr);
#
# return true;
#
# }
#
# void ServerSocket::GetRemoteAddrees(char *addrees){
#
# strcpy(addrees,inet_ntoa(get_addr.sin_addr));
# SetRemoteAddrees(addrees);
# }
#
# void ServerSocket::Close(){
#
# close(sockfd);
# close(new_fd);
#
# }
#
# void ServerSocket::Listen(){
#
# listen(sockfd,10);
#
# }
#
# void ServerSocket::Accept(){
#
# socklen_t size = sizeof(struct sockaddr_in);
# new_fd = accept(sockfd,(struct sockaddr *)&get_addr,&size);
#
# }
#
# int ServerSocket::Recv(char *buf,int len){
#
# return recv(new_fd,buf,len,0);
#
# }
#
# int ServerSocket::Send(const char *buf, int len){
#
# return send(sockfd,buf,len,0);
#
# }
#
# bool ServerSocket::Connect(){
#
# if(connect(sockfd,(struct sockaddr *)&their_addr, sizeof(struct sockaddr))==-1)
# return false;
#
# return true;
#
# }

阅读(1660) | 评论(0) | 转发(0) |
0

上一篇:socket与TCP/IP

下一篇:轻量级Web Server

给主人留下些什么吧!~~