?? mysocket.cpp
字號(hào):
#include "mySocket.h"
#include "..\myException\myException.h"
#include "..\myLog\myLog.h"
const int MSG_HEADER_LEN = 6;
mySocket::mySocket(int pNumber)
{
portNumber = pNumber;
blocking = 1;
try
{
if ( (socketId=socket(AF_INET,SOCK_STREAM,0)) == -1)
{
#ifdef WINDOWS_XP
int errorCode;
string errorMsg = "";
detectErrorOpenWinSocket(&errorCode,errorMsg);
myException openWinSocketException(errorCode,errorMsg);
throw openWinSocketException;
#endif
#ifdef UNIX
myException openUnixSocketException(0,"unix: error getting host by name");
throw openUnixSocketException;
#endif
}
}
catch(myException& excp)
{
excp.response();
exit(1);
}
/*
set the initial address of client that shall be communicated with to
any address as long as they are using the same port number.
The clientAddr structure is used in the future for storing the actual
address of client applications with which communication is going
to start
*/
clientAddr.sin_family = AF_INET;
clientAddr.sin_addr.s_addr = htonl(INADDR_ANY);
clientAddr.sin_port = htons(portNumber);
}
void mySocket::setDebug(int debugToggle)
{
try
{
if ( setsockopt(socketId,SOL_SOCKET,SO_DEBUG,(char *)&debugToggle,sizeof(debugToggle)) == -1 )
{
#ifdef WINDOWS_XP
int errorCode;
string errorMsg = "DEBUG option:";
detectErrorSetSocketOption(&errorCode,errorMsg);
myException socketOptionException(errorCode,errorMsg);
throw socketOptionException;
#endif
#ifdef UNIX
myException unixSocketOptionException(0,"unix: error getting host by name");
throw unixSocketOptionException;
#endif
}
}
catch(myException& excp)
{
excp.response();
exit(1);
}
}
void mySocket::setReuseAddr(int reuseToggle)
{
try
{
if ( setsockopt(socketId,SOL_SOCKET,SO_REUSEADDR,(char *)&reuseToggle,sizeof(reuseToggle)) == -1 )
{
#ifdef WINDOWS_XP
int errorCode;
string errorMsg = "REUSEADDR option:";
detectErrorSetSocketOption(&errorCode,errorMsg);
myException socketOptionException(errorCode,errorMsg);
throw socketOptionException;
#endif
#ifdef UNIX
myException unixSocketOptionException(0,"unix: error getting host by name");
throw unixSocketOptionException;
#endif
}
}
catch(myException& excp)
{
excp.response();
exit(1);
}
}
void mySocket::setKeepAlive(int aliveToggle)
{
try
{
if ( setsockopt(socketId,SOL_SOCKET,SO_KEEPALIVE,(char *)&aliveToggle,sizeof(aliveToggle)) == -1 )
{
#ifdef WINDOWS_XP
int errorCode;
string errorMsg = "ALIVE option:";
detectErrorSetSocketOption(&errorCode,errorMsg);
myException socketOptionException(errorCode,errorMsg);
throw socketOptionException;
#endif
#ifdef UNIX
myException unixSocketOptionException(0,"unix: error getting host by name");
throw unixSocketOptionException;
#endif
}
}
catch(myException& excp)
{
excp.response();
exit(1);
}
}
void mySocket::setLingerSeconds(int seconds)
{
struct linger lingerOption;
if ( seconds > 0 )
{
lingerOption.l_linger = seconds;
lingerOption.l_onoff = 1;
}
else lingerOption.l_onoff = 0;
try
{
if ( setsockopt(socketId,SOL_SOCKET,SO_LINGER,(char *)&lingerOption,sizeof(struct linger)) == -1 )
{
#ifdef WINDOWS_XP
int errorCode;
string errorMsg = "LINGER option:";
detectErrorSetSocketOption(&errorCode,errorMsg);
myException socketOptionException(errorCode,errorMsg);
throw socketOptionException;
#endif
#ifdef UNIX
myException unixSocketOptionException(0,"unix: error getting host by name");
throw unixSocketOptionException;
#endif
}
}
catch(myException& excp)
{
excp.response();
exit(1);
}
}
void mySocket::setLingerOnOff(bool lingerOn)
{
struct linger lingerOption;
if ( lingerOn ) lingerOption.l_onoff = 1;
else lingerOption.l_onoff = 0;
try
{
if ( setsockopt(socketId,SOL_SOCKET,SO_LINGER,(char *)&lingerOption,sizeof(struct linger)) == -1 )
{
#ifdef WINDOWS_XP
int errorCode;
string errorMsg = "LINGER option:";
detectErrorSetSocketOption(&errorCode,errorMsg);
myException socketOptionException(errorCode,errorMsg);
throw socketOptionException;
#endif
#ifdef UNIX
myException unixSocketOptionException(0,"unix: error getting host by name");
throw unixSocketOptionException;
#endif
}
}
catch(myException& excp)
{
excp.response();
exit(1);
}
}
void mySocket::setSendBufSize(int sendBufSize)
{
try
{
if ( setsockopt(socketId,SOL_SOCKET,SO_SNDBUF,(char *)&sendBufSize,sizeof(sendBufSize)) == -1 )
{
#ifdef WINDOWS_XP
int errorCode;
string errorMsg = "SENDBUFSIZE option:";
detectErrorSetSocketOption(&errorCode,errorMsg);
myException socketOptionException(errorCode,errorMsg);
throw socketOptionException;
#endif
#ifdef UNIX
myException unixSocketOptionException(0,"unix: error getting host by name");
throw unixSocketOptionException;
#endif
}
}
catch(myException& excp)
{
excp.response();
exit(1);
}
}
void mySocket::setReceiveBufSize(int receiveBufSize)
{
try
{
if ( setsockopt(socketId,SOL_SOCKET,SO_RCVBUF,(char *)&receiveBufSize,sizeof(receiveBufSize)) == -1 )
{
#ifdef WINDOWS_XP
int errorCode;
string errorMsg = "RCVBUF option:";
detectErrorSetSocketOption(&errorCode,errorMsg);
myException socketOptionException(errorCode,errorMsg);
throw socketOptionException;
#endif
#ifdef UNIX
myException unixSocketOptionException(0,"unix: error getting host by name");
throw unixSocketOptionException;
#endif
}
}
catch(myException& excp)
{
excp.response();
exit(1);
}
}
void mySocket::setSocketBlocking(int blockingToggle)
{
if (blockingToggle)
{
if (getSocketBlocking()) return;
else blocking = 1;
}
else
{
if (!getSocketBlocking()) return;
else blocking = 0;
}
try
{
#ifdef WINDOWS_XP
if (ioctlsocket(socketId,FIONBIO,(unsigned long *)&blocking) == -1)
{
int errorCode;
string errorMsg = "Blocking option: ";
detectErrorSetSocketOption(&errorCode,errorMsg);
myException socketOptionException(errorCode,errorMsg);
throw socketOptionException;
}
#endif
#ifdef UNIX
if (ioctl(socketId,FIONBIO,(char *)&blocking) == -1)
{
myException unixSocketOptionException(0,"unix: error getting host by name");
throw unixSocketOptionException;
}
#endif
}
catch(myException& excp)
{
excp.response();
exit(1);
}
}
int mySocket::getDebug()
{
int myOption;
int myOptionLen = sizeof(myOption);
try
{
if ( getsockopt(socketId,SOL_SOCKET,SO_DEBUG,(char *)&myOption,&myOptionLen) == -1 )
{
#ifdef WINDOWS_XP
int errorCode;
string errorMsg = "get DEBUG option: ";
detectErrorGetSocketOption(&errorCode,errorMsg);
myException socketOptionException(errorCode,errorMsg);
throw socketOptionException;
#endif
#ifdef UNIX
myException unixSocketOptionException(0,"unix: error getting host by name");
throw unixSocketOptionException;
#endif
}
}
catch(myException& excp)
{
excp.response();
exit(1);
}
return myOption;
}
int mySocket::getReuseAddr()
{
int myOption;
int myOptionLen = sizeof(myOption);
try
{
if ( getsockopt(socketId,SOL_SOCKET,SO_REUSEADDR,(char *)&myOption,&myOptionLen) == -1 )
{
#ifdef WINDOWS_XP
int errorCode;
string errorMsg = "get REUSEADDR option: ";
detectErrorGetSocketOption(&errorCode,errorMsg);
myException socketOptionException(errorCode,errorMsg);
throw socketOptionException;
#endif
#ifdef UNIX
myException unixSocketOptionException(0,"unix: error getting host by name");
throw unixSocketOptionException;
#endif
}
}
catch(myException& excp)
{
excp.response();
exit(1);
}
return myOption;
}
int mySocket::getKeepAlive()
{
int myOption;
int myOptionLen = sizeof(myOption);
try
{
if ( getsockopt(socketId,SOL_SOCKET,SO_KEEPALIVE,(char *)&myOption,&myOptionLen) == -1 )
{
#ifdef WINDOWS_XP
int errorCode;
string errorMsg = "get KEEPALIVE option: ";
detectErrorGetSocketOption(&errorCode,errorMsg);
myException socketOptionException(errorCode,errorMsg);
throw socketOptionException;
#endif
#ifdef UNIX
myException unixSocketOptionException(0,"unix: error getting host by name");
throw unixSocketOptionException;
#endif
}
}
catch(myException& excp)
{
excp.response();
exit(1);
}
return myOption;
}
int mySocket::getLingerSeconds()
{
struct linger lingerOption;
int myOptionLen = sizeof(struct linger);
try
{
if ( getsockopt(socketId,SOL_SOCKET,SO_LINGER,(char *)&lingerOption,&myOptionLen) == -1 )
{
#ifdef WINDOWS_XP
int errorCode;
string errorMsg = "get LINER option: ";
detectErrorGetSocketOption(&errorCode,errorMsg);
myException socketOptionException(errorCode,errorMsg);
throw socketOptionException;
#endif
#ifdef UNIX
myException unixSocketOptionException(0,"unix: error getting host by name");
throw unixSocketOptionException;
#endif
}
}
catch(myException& excp)
{
excp.response();
exit(1);
}
return lingerOption.l_linger;
}
bool mySocket::getLingerOnOff()
{
struct linger lingerOption;
int myOptionLen = sizeof(struct linger);
try
{
if ( getsockopt(socketId,SOL_SOCKET,SO_LINGER,(char *)&lingerOption,&myOptionLen) == -1 )
{
#ifdef WINDOWS_XP
int errorCode;
string errorMsg = "get LINER option: ";
detectErrorGetSocketOption(&errorCode,errorMsg);
myException socketOptionException(errorCode,errorMsg);
throw socketOptionException;
#endif
#ifdef UNIX
myException unixSocketOptionException(0,"unix: error getting host by name");
throw unixSocketOptionException;
#endif
}
}
catch(myException& excp)
{
excp.response();
exit(1);
}
if ( lingerOption.l_onoff == 1 ) return true;
else return false;
}
int mySocket::getSendBufSize()
{
int sendBuf;
int myOptionLen = sizeof(sendBuf);
try
{
if ( getsockopt(socketId,SOL_SOCKET,SO_SNDBUF,(char *)&sendBuf,&myOptionLen) == -1 )
{
#ifdef WINDOWS_XP
int errorCode;
string errorMsg = "get SNDBUF option: ";
detectErrorGetSocketOption(&errorCode,errorMsg);
myException socketOptionException(errorCode,errorMsg);
throw socketOptionException;
#endif
#ifdef UNIX
myException unixSocketOptionException(0,"unix: error getting host by name");
throw unixSocketOptionException;
#endif
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -