windows_socket_易忘函数

#define INVALID_SOCKET (SOCKET)(~0)
#define SOCKET_ERROR -1
/*
socket();创建函数失败返回INVALID_SOCKET,其他socket函数失败返回SOCKET_ERROR;
*/
inet_ntoa();
inet_addr();
/*
实现网络地址和“.”间隔的字符串地址之间的转换:
unsigned long ulAddr = inet_addr("192.168.1.250");
char* pAddr = inet_ntoa(*(in_addr*)(&ulAddr));
*/
ntohs();
/*
将端口从网路字节顺序转换为主机字节顺序;
*/
nRead = recv();
/*
该函数返回值为0时,说明此时客户端关闭了连接;
错误处理:
if (SOCKET_ERROR == nReadLen)
{
int nErrCode = WSAGetLastError(); //获取错误代码
if(WSAENOTCONN == nErrCode)
{
//the socket is not connected
}else if(WSAESHUTDOWN == nErrCode)
{
//the socket has been shut down
}else if(WSAETIMEDOUT == nErrCode)
{
//the connection has been dropped
}else if(WSAECONNRESET == nErrCode)
{
//the virtual circuit was reset by the remote side
}else
{
//....
}
}
*/
nSend = send();
/*
错误处理
if(SOCKET_ERROR == nSend)
{
int nErrCode = WSAGetLastError(); //获取错误代码
if(WSAENOTCONN == nErrCode)
{
//the socket is not connected
}else if(WSAESHUTSOEN == nErrCode)
{
//the socket has been shut down
}else if(WSAETIMEDOUT == nErrCode)
{
//the connection has been dropped
}else
{
//.....
}
}
*/
bind();
listen();
accept();
/*
绑定、监听、接受连接失败,要记得关闭套接字
*/
服务器端,退出时,要先关闭监听套接字,再关闭连接客户端套接字;
gethostname();
//获的主机的名字
gethostbyname();
/*以gethostname()返回的主机名字为参数调用gethostbyname(),返回hostent结构指针,
其中保存了主机的地址。另外,获得hostent结构的方法:
使用inet_addr()函数的返回值作为参数调用gethostbyaddr()函数;
*/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值