/////////////////////////////////////////////////////////////////////
// Class Creator Version 2.0.000 Copyrigth (C) Poul A. Costinsky 1994
///////////////////////////////////////////////////////////////////
// Implementation File RawSocket.cpp
// class CWizRawSocket
//
// 23/07/1996 14:54 Author: Poul, Hadas & Oren
///////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include <RawSocket.h>
#ifdef _UNICODE
#include <winnls.h>
#endif
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char BASED_CODE THIS_FILE[] = __FILE__;
#endif
//*****************************************************************
void CWizSyncSocket::Init(SOCKET h)
{
m_hSocket = h;
}
//*****************************************************************
void CWizSyncSocket::Close()
{
ASSERT(m_hSocket != INVALID_SOCKET);
::closesocket(m_hSocket);
m_hSocket = INVALID_SOCKET;
}
//*****************************************************************
int CWizSyncSocket::SetIntOption(int level, int optname, int val)
{
return ::setsockopt (m_hSocket, level, optname, (char *)&val, sizeof(val));
}
//*****************************************************************
BOOL CWizSyncSocket::InitializeSocket(int nPort)
{
// Socket must be created with socket()
ASSERT(m_hSocket != INVALID_SOCKET);
// Make up address
SOCKADDR_IN SockAddr;
memset(&SockAddr,0, sizeof(SockAddr));
SockAddr.sin_family = AF_INET;
SockAddr.sin_addr.s_addr = INADDR_ANY;
SockAddr.sin_port = ::htons(nPort);
// Bind to the address and port
int r = ::bind(m_hSocket, (SOCKADDR*)&SockAddr, sizeof(SockAddr));
if (r == 0)
{
SetIntOption (SOL_SOCKET, SO_DONTLINGER,1);
SetIntOption (SOL_SOCKET, SO_KEEPALIVE,1);
// establishes a socket to listen for incoming connection
// so Accept can be called
r = ::listen (m_hSocket, 5);
if (r == 0)
return TRUE;
}
return FALSE;
}
//*****************************************************************
BOOL CWizSyncSocket::Create(int nPort)
{
// creates a socket
m_hSocket = ::socket(PF_INET, SOCK_STREAM, 0);
if (m_hSocket == INVALID_SOCKET)
return FALSE;
// Bind to the port
if (!InitializeSocket(nPort))
{
Close();
return FALSE;
}
return TRUE;
}
//*****************************************************************
// Create an invalid socket
CWizSyncSocket::CWizSyncSocket(SOCKET h /* = INVALID_SOCKET */)
{
Init(h);
}
//*****************************************************************
// Create a listening socket
CWizSyncSocket::CWizSyncSocket(int nPort)
{
Init(INVALID_SOCKET);
if(!Create(nPort))
throw XCannotCreate();
}
//*****************************************************************
CWizSyncSocket::~CWizSyncSocket()
{
if (m_hSocket != INVALID_SOCKET)
Close();
}
//*****************************************************************
// Waits for pending connections on the port,
// creates a new socket with the same properties as this
// and returns a handle to the new socket
SOCKET CWizSyncSocket::Accept()
{
SOCKET h = ::accept (m_hSocket, NULL, NULL);
return h;
}
//*****************************************************************
// Cerates a socket and establishes a connection to a peer
// on lpszHostAddress:nHostPort
BOOL CWizSyncSocket::Connect(LPCTSTR lpszHostAddress, UINT nHostPort )
{
ASSERT(lpszHostAddress != NULL);
// Create ? socket
if (m_hSocket == INVALID_SOCKET)
{
m_hSocket = ::socket(PF_INET, SOCK_STREAM, 0);
if (m_hSocket == INVALID_SOCKET)
return FALSE;
}
// Fill address machinery of sockets.
SOCKADDR_IN sockAddr;
memset(&sockAddr,0,sizeof(sockAddr));
#ifdef _UNICODE
char buff[500];
WideCharToMultiByte(CP_ACP, 0, lpszHostAddress, -1, buff, 500, NULL, NULL);
LPSTR lpszAscii = buff;
#else
LPSTR lpszAscii = (LPSTR)lpszHostAddress;
#endif
sockAddr.sin_family = AF_INET;
sockAddr.sin_addr.s_addr = inet_addr(lpszAscii);
if (sockAddr.sin_addr.s_addr == INADDR_NONE)
{
LPHOSTENT lphost;
lphost = gethostbyname(lpszAscii);
if (lphost != NULL)
sockAddr.sin_addr.s_addr = ((LPIN_ADDR)lphost->h_addr)->s_addr;
else
{
WSASetLastError(WSAEINVAL);
return FALSE;
}
}
sockAddr.sin_port = htons((u_short)nHostPort);
// connects to peer
int r = ::connect(m_hSocket, (SOCKADDR*)&sockAddr, sizeof(sockAddr));
if (r != SOCKET_ERROR)
{
ASSERT(r == 0);
return TRUE;
}
int e = ::WSAGetLastError();
ASSERT(e != WSAEWOULDBLOCK);
return FALSE;
}
//*****************************************************************
// read raw data
int CWizReadWriteSocket::Read(void *pData, int nLen)
{
char* pcData = (char* )pData;
int n = nLen;
// if data size is bigger then network buffer
// handle it nice
do
{
int r1 = ::recv (m_hSocket, pcData, n, 0);
if (r1 == SOCKET_ERROR)
{
int e = WSAGetLastError();
ASSERT(e != WSAEWOULDBLOCK);
return 0;
}
else if (r1 == 0)
return 0;
else if (r1 < 0)
{
ASSERT(0);
return 0;
}
pcData += r1;
n -= r1;
} while (n > 0);
ASSERT(n == 0);
return nLen;
}
//*****************************************************************
// write raw data
int CWizReadWriteSocket::Write(const void *pData, int nLen)
{
const char* pcData = (const char* )pData;
int n = nLen;
// if data size is bigger then network buffer
// handle it nice
do
{
int r1 = ::send (m_hSocket, pcData, n, 0);
if (r1 == SOCKET_ERROR)
{
int e = WSAGetLastError();
ASSERT(e != WSAEWOULDBLOCK);
return 0;
}
else if (r1 == 0)
return 0;
else if (r1 < 0)
{
ASSERT(0);
return 0;
}
pcData += r1;
n -= r1;
} while (n > 0);
ASSERT(n == 0);
return nLen;
}
//*****************************************************************
// Reads UNICODE string from socket.
// Converts string from ANSI to UNICODE.
int CWizReadWriteSocket::ReadString (WCHAR* lpszString, int nMaxLen)
{
// read string length
u_long nt_Len;
if (Read(&nt_Len, sizeof(nt_Len)) < sizeof(nt_Len))
return 0;
int Len = ntohl(nt_Len);
if (Len == 0 || Len >= nMaxLen)
return 0;
static const int BUFF_SIZE = 2000;
if (Len >= BUFF_SIZE)
return 0;
char buff[BUFF_SIZE];
// Read ANSI string to the buffer
if (Read(buff, Len) < Len)
return 0;
buff[Len] = 0;
// Convert ANSI string to the UNICODE
VERIFY(::MultiByteToWideChar(CP_ACP, 0, buff, Len, lpszString, nMaxLen*sizeof(lpszString[0])));
return Len;
}
//*****************************************************************
// Reads ANSI string from socket.
int CWizReadWriteSocket::ReadString (char* lpszString, int nMaxLen)
{
// read string length
u_long nt_Len;
if (Read(&nt_Len, sizeof(nt_Len)) < sizeof(nt_Len))
return 0;
int Len = ntohl(nt_Len);
if (Len == 0 || Len >= nMaxLen)
return 0;
// Read ANSI string
if (Read(lpszString, Len) < Len)
return 0;
lpszString[Len] = 0;
return Len;
}
//*****************************************************************
inline int Length(const char* p)
{
return strlen(p);
}
//*****************************************************************
inline int Length(const WCHAR* p)
{
return wcslen(p);
}
//*****************************************************************
// Writes UNICODE string to socket,
// converts UNICODE string to ANSI.
BOOL CWizReadWriteSocket::WriteString (const WCHAR* lpszString, int nLen /* = -1*/)
{
if (nLen < 0)
nLen = Length(lpszString);
static const int BUFF_SIZE = 2000;
if (nLen >= BUFF_SIZE*sizeof(lpszString) + sizeof(u_long))
return FALSE;
char buff[BUFF_SIZE];
u_long nt_Len = htonl(nLen);
int nSize = sizeof(nt_Len);
memcpy(buff, &nt_Len, nSize);
// To make one call less, the length of the string
// copied to the buffer before the string itself
// and the buffer sent once.
char* pt
没有合适的资源?快使用搜索试试~ 我知道了~
多线程实现客户-服务器结构的一个例子
共32个文件
h:11个
cpp:10个
dsp:2个
2星 需积分: 10 12 下载量 73 浏览量
2008-11-26
14:25:41
上传
评论
收藏 36KB ZIP 举报
温馨提示
一个比较好的 网络编程代码,在工作中我曾今使用过,希望能给学习和工作的朋友带来价值
资源推荐
资源详情
资源评论
收起资源包目录
RawSocketServerExample.ZIP (32个子文件)
cpp
RawSocketServerExample
StdAfx.h 961B
RawSocketServerWorker.cpp 6KB
res
SocketServer.rc2 404B
SocketServer.ico 1KB
CRITSECT.CPP 1KB
SocketServer.rc 5KB
SocketServer.opt 58KB
SocketServer.cpp 2KB
RawSocket.h 6KB
RawSocket.cpp 11KB
SocketClient
StdAfx.h 642B
SC.dsp 5KB
SC.plg 10KB
main.cpp 2KB
StdAfx.cpp 210B
SC.mak 8KB
SocketServer.plg 4KB
ThreadDispatcher.cpp 6KB
SocketServer.h 945B
SocketServer.mak 16KB
ClosingDialog.h 908B
RawSocketServerWorker.h 2KB
resource.h 734B
ClosingDialog.cpp 1KB
SocketServerDlg.h 2KB
SocketServer.dsp 5KB
ThreadDispatcher.h 5KB
SocketServerDlg.cpp 4KB
StdAfx.cpp 210B
SocketServer.dsw 739B
names.h 354B
CRITSECT.H 2KB
共 32 条
- 1
资源评论
- 思变2013-07-18编译时有错误,不太好。
IT_SKY_888
- 粉丝: 0
- 资源: 10
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功