1 Star 0 Fork 24

xul/socket

forked from eclipser/socket 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
ServerSocket.cpp 1.22 KB
一键复制 编辑 原始数据 按行查看 历史
Administrator 提交于 2014-02-26 21:28 . 封装serversocket
#include "ServerSocket.h"
#include <string.h>
ServerSocket::ServerSocket(unsigned int port, unsigned int backlog) {
bool ret = false;
#define KEY_MAC 12
m_Socket = new Socket;
// Assert( m_Socket ) ;
if (m_Socket == NULL)
throw 1;
// create socket
ret = m_Socket->create();
if (ret == false)
throw 1;
ret = m_Socket->setReuseAddr();
if (ret == false)
throw 1;
ret = m_Socket->bind(port);
if (ret == false)
throw 1;
ret = m_Socket->listen(backlog);
if (ret == false)
throw 1;
}
ServerSocket::~ServerSocket() {
if (m_Socket != NULL) {
m_Socket->close();
delete m_Socket;
m_Socket = NULL;
}
}
void ServerSocket::close() {
if (m_Socket) {
m_Socket->close();
}
}
bool ServerSocket::accept(Socket* socket) {
unsigned int addrlen = sizeof (SOCKADDR_IN);
socket->close();
socket->m_SocketID = m_Socket->accept((struct sockaddr *) (&(socket->m_SockAddr)), &addrlen);
if (socket->m_SocketID == INVALID_SOCKET)
return false;
socket->m_Port = ntohs(socket->m_SockAddr.sin_port);
strncpy(socket->m_Host, inet_ntoa(socket->m_SockAddr.sin_addr), IP_SIZE - 1);
return true;
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/xuliang5/socket.git
git@gitee.com:xuliang5/socket.git
xuliang5
socket
socket
master

搜索帮助