/*
Socket.cpp
Copyright (C) 2002-2004 Ren谷 Nyffenegger
This source code is provided 'as-is', without any express or implied
warranty. In no event will the author be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this source code must not be misrepresented; you must not
claim that you wrote the original source code. If you use this source code
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original source code.
3. This notice may not be removed or altered from any source distribution.
Ren谷 Nyffenegger rene.nyffenegger@adp-gmbh.ch
*/
#include "Socket.h"
#include <iostream>
using namespace std;
int Socket::nofSockets_= 0;
void Socket::Start() {
if (!nofSockets_) {
WSADATA info;
if (WSAStartup(MAKEWORD(2,0), &info)) {
throw "Could not start WSA";
}
}
++nofSockets_;
}
void Socket::End() {
WSACleanup();
}
Socket::Socket() : s_(0) {
Start();
// UDP: use SOCK_DGRAM instead of SOCK_STREAM
s_ = socket(AF_INET,SOCK_STREAM,0);
if (s_ == INVALID_SOCKET) {
throw "INVALID_SOCKET";
}
refCounter_ = new int(1);
}
Socket::Socket(SOCKET s) : s_(s) {
Start();
refCounter_ = new int(1);
};
Socket::~Socket() {
if (! --(*refCounter_)) {
Close();
delete refCounter_;
}
--nofSockets_;
if (!nofSockets_) End();
}
Socket::Socket(const Socket& o) {
refCounter_=o.refCounter_;
(*refCounter_)++;
s_ =o.s_;
nofSockets_++;
}
Socket& Socket::operator=(Socket& o) {
(*o.refCounter_)++;
refCounter_=o.refCounter_;
s_ =o.s_;
nofSockets_++;
return *this;
}
void Socket::Close() {
closesocket(s_);
}
std::string Socket::ReceiveBytes() {
std::string ret;
char buf[1024];
while (1) {
u_long arg = 0;
if (ioctlsocket(s_, FIONREAD, &arg) != 0)
break;
if (arg == 0)
break;
if (arg > 1024) arg = 1024;
int rv = recv (s_, buf, arg, 0);
if (rv <= 0) break;
std::string t;
t.assign (buf, rv);
ret += t;
}
return ret;
}
std::string Socket::ReceiveLine() {
std::string ret;
while (1) {
char r;
switch(recv(s_, &r, 1, 0)) {
case 0: // not connected anymore;
// ... but last line sent
// might not end in \n,
// so return ret anyway.
return ret;
case -1:
return "";
// if (errno == EAGAIN) {
// return ret;
// } else {
// // not connected anymore
// return "";
// }
}
ret += r;
if (r == '\n') return ret;
}
}
void Socket::SendLine(std::string s) {
s += '\n';
send(s_,s.c_str(),s.length(),0);
}
void Socket::SendBytes(const std::string& s) {
send(s_,s.c_str(),s.length(),0);
}
SocketServer::SocketServer(int port, int connections, TypeSocket type) {
sockaddr_in sa;
memset(&sa, 0, sizeof(sa));
sa.sin_family = PF_INET;
sa.sin_port = htons(port);
s_ = socket(AF_INET, SOCK_STREAM, 0);
if (s_ == INVALID_SOCKET) {
throw "INVALID_SOCKET";
}
if(type==NonBlockingSocket) {
u_long arg = 1;
ioctlsocket(s_, FIONBIO, &arg);
}
/* bind the socket to the internet address */
if (bind(s_, (sockaddr *)&sa, sizeof(sockaddr_in)) == SOCKET_ERROR) {
closesocket(s_);
throw "INVALID_SOCKET";
}
listen(s_, connections);
}
Socket* SocketServer::Accept() {
SOCKET new_sock = accept(s_, 0, 0);
if (new_sock == INVALID_SOCKET) {
int rc = WSAGetLastError();
if(rc==WSAEWOULDBLOCK) {
return 0; // non-blocking call, no request pending
}
else {
throw "Invalid Socket";
}
}
Socket* r = new Socket(new_sock);
return r;
}
SocketClient::SocketClient(const std::string& host, int port) : Socket() {
std::string error;
hostent *he;
if ((he = gethostbyname(host.c_str())) == 0) {
error = strerror(errno);
throw error;
}
sockaddr_in addr;
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
addr.sin_addr = *((in_addr *)he->h_addr);
memset(&(addr.sin_zero), 0, 8);
if (::connect(s_, (sockaddr *) &addr, sizeof(sockaddr))) {
error = strerror(WSAGetLastError());
throw error;
}
}
SocketSelect::SocketSelect(Socket const * const s1, Socket const * const s2, TypeSocket type) {
FD_ZERO(&fds_);
FD_SET(const_cast<Socket*>(s1)->s_,&fds_);
if(s2) {
FD_SET(const_cast<Socket*>(s2)->s_,&fds_);
}
TIMEVAL tval;
tval.tv_sec = 0;
tval.tv_usec = 1;
TIMEVAL *ptval;
if(type==NonBlockingSocket) {
ptval = &tval;
}
else {
ptval = 0;
}
if (select (0, &fds_, (fd_set*) 0, (fd_set*) 0, ptval) == SOCKET_ERROR)
throw "Error in select";
}
bool SocketSelect::Readable(Socket const* const s) {
if (FD_ISSET(s->s_,&fds_)) return true;
return false;
}
VC模拟发送数据包-百度关键词查找
4星 · 超过85%的资源 需积分: 10 118 浏览量
2009-09-06
16:35:56
上传
评论
收藏 60KB RAR 举报
汪宁宇
- 粉丝: 749
- 资源: 42
最新资源
- unity读取excel工具 使用3.5即可
- Matplotlib 是一个 Python 的绘图库 Matplotlib 绘图指南与功能介绍.docx
- 基于TMS320F2808设计光伏并网发电模拟器AD09设计硬件(原理图 PCB)+CCS软件源码 +说明文档.zip
- 在微电网中采用粒子群算法在matlab环境下进行了仿真,最后给出了微网各微源的最优机组组合方式和最佳电能交易计划 (注释完全,可
- 在matlab中通过yalmip平台调用cplex求解器,可用于求解MILP问题,适合于综合能源系统优化求解(注释完全,可直接运
- MT8852B labview 驱动,MT8852B labview 代码,蓝牙测试仪MT8852B代码,蓝牙低功耗测试
- 导照片2.0(只适合索尼微单)
- KMP算法python代码实现与使用场景.docx
- 基于MATLABCPLEX 的机组最优组合,成功求解表格化,图示化的机组组合结果(注释完全,可直接运行)(文档加Matlab源码
- postgresql-9.5.3-1-windows-x64
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈