如何通过 irDA 套接字建立连接?
how to establish connection through irDA sockets?
在学习网络编程时,我们从 irDA 编程开始,所以我有两个 irDA 适配器 "Kingsun KS-959 USB Infrared Adapter" 和两台旧计算机 运行 windows xp sp2.
我在两台机器上都正确安装了适配器。
我想在 irDA 中创建 Server/Client。这是 irServer irClient 的示例:
// irServer:
#define BACKLOG 2
WORD wVersion = MAKEWORD(2, 2);
WSADATA wSaData;
if(WSAStartup(wVersion, &wSaData))
return -1;
int irSockServ = socket(AF_IRDA, SOCK_STREAM, 0);
if(SOCKET_ERROR == irSockServ)
DieWithError(WSAGetLastError());
SOCKADDR_IRDA sa_irda_serv = {AF_IRDA, 0, 0, 0, 0,
"irDA:irCOMM"};
if(SOCKET_ERROR == bind(irSockServ, (SOCKADDR*)&sa_irda_serv, sizeof(sa_irda_serv)))
DieWithError(WSAGetLastError());
if(SOCKET_ERROR == listen(irSockServ, BACKLOG))
DieWithError(WSAGetLastError());
SOCKADDR_IRDA sa_irda_clnt;
int sa_irda_clnt_len = sizeof(sa_irda_clnt);
int irSockClnt = accept(irSockServ, (SOCKADDR*)&sa_irda_clnt, &sa_irda_clnt_len);
if(SOCKET_ERROR == irSockClnt)
DieWithError(WSAGetLastError());
else
cout << "Client! Connected" << std::endl;
这里是 irClient 的示例:
WORD wVersion = MAKEWORD(2, 2);
WSADATA wSaData;
if(WSAStartup(wVersion, &wSaData))
return -1;
int irSockClnt = socket(AF_IRDA, SOCK_STREAM, 0);
if(SOCKET_ERROR == irSockClnt)
DieWithError(WSAGetLastError());
DEVICELIST DevLst;
int DevLstLen = sizeof(DEVICELIST);
while(1)
{
Sleep(1000);
DevLst.numDevice = 0;
// enumerating in range irDA devices
if(SOCKET_ERROR == getsockopt(irSockClnt, SOL_IRLMP,
IRLMP_ENUMDEVICES, (char*)&DevLst, &DevLstLen))
DieWithError(WSAGetLastError());
if(DevLst.numDevice)
{
cout << "Device Name: " << DevLst.Device[0].irdaDeviceName << endl;
SOCKADDR_IRDA sa_irda_serv;
sa_irda_serv.irdaAddressFamily = AF_IRDA;
strcpy(sa_irda_serv.irdaServiceName, "irDA:irCOMM");
memcpy(&sa_irda_serv.irdaDeviceID[0], &DevLst.Device[0].irdaDeviceID[0], 4);
// the problem is here it still stuck and won't connect!??
if(SOCKET_ERROR == connect(irSockClnt, (SOCKADDR*)&sa_irda_serv, sizeof(sa_irda_serv)))
DieWithError(WSAGetLastError());
}
}
我在发出连接之前一切正常,所以 connect
仍然卡住,因此没有建立连接。
感谢大王的帮助
在服务器实现中,将accept()
置于while(1)
循环中。
您无需在 while(1)
循环中等待 客户端 实现中的 connect()
。
服务器
while(1) {
...
accept(...);
Sleep(Number_of_miliseconds);//You are using win32 imp, for POSIX sleep() this will be in seconds
...
}
在学习网络编程时,我们从 irDA 编程开始,所以我有两个 irDA 适配器 "Kingsun KS-959 USB Infrared Adapter" 和两台旧计算机 运行 windows xp sp2.
我在两台机器上都正确安装了适配器。
我想在 irDA 中创建 Server/Client。这是 irServer irClient 的示例:
// irServer:
#define BACKLOG 2
WORD wVersion = MAKEWORD(2, 2);
WSADATA wSaData;
if(WSAStartup(wVersion, &wSaData))
return -1;
int irSockServ = socket(AF_IRDA, SOCK_STREAM, 0);
if(SOCKET_ERROR == irSockServ)
DieWithError(WSAGetLastError());
SOCKADDR_IRDA sa_irda_serv = {AF_IRDA, 0, 0, 0, 0,
"irDA:irCOMM"};
if(SOCKET_ERROR == bind(irSockServ, (SOCKADDR*)&sa_irda_serv, sizeof(sa_irda_serv)))
DieWithError(WSAGetLastError());
if(SOCKET_ERROR == listen(irSockServ, BACKLOG))
DieWithError(WSAGetLastError());
SOCKADDR_IRDA sa_irda_clnt;
int sa_irda_clnt_len = sizeof(sa_irda_clnt);
int irSockClnt = accept(irSockServ, (SOCKADDR*)&sa_irda_clnt, &sa_irda_clnt_len);
if(SOCKET_ERROR == irSockClnt)
DieWithError(WSAGetLastError());
else
cout << "Client! Connected" << std::endl;
这里是 irClient 的示例:
WORD wVersion = MAKEWORD(2, 2);
WSADATA wSaData;
if(WSAStartup(wVersion, &wSaData))
return -1;
int irSockClnt = socket(AF_IRDA, SOCK_STREAM, 0);
if(SOCKET_ERROR == irSockClnt)
DieWithError(WSAGetLastError());
DEVICELIST DevLst;
int DevLstLen = sizeof(DEVICELIST);
while(1)
{
Sleep(1000);
DevLst.numDevice = 0;
// enumerating in range irDA devices
if(SOCKET_ERROR == getsockopt(irSockClnt, SOL_IRLMP,
IRLMP_ENUMDEVICES, (char*)&DevLst, &DevLstLen))
DieWithError(WSAGetLastError());
if(DevLst.numDevice)
{
cout << "Device Name: " << DevLst.Device[0].irdaDeviceName << endl;
SOCKADDR_IRDA sa_irda_serv;
sa_irda_serv.irdaAddressFamily = AF_IRDA;
strcpy(sa_irda_serv.irdaServiceName, "irDA:irCOMM");
memcpy(&sa_irda_serv.irdaDeviceID[0], &DevLst.Device[0].irdaDeviceID[0], 4);
// the problem is here it still stuck and won't connect!??
if(SOCKET_ERROR == connect(irSockClnt, (SOCKADDR*)&sa_irda_serv, sizeof(sa_irda_serv)))
DieWithError(WSAGetLastError());
}
}
我在发出连接之前一切正常,所以 connect
仍然卡住,因此没有建立连接。
感谢大王的帮助
在服务器实现中,将accept()
置于while(1)
循环中。
您无需在 while(1)
循环中等待 客户端 实现中的 connect()
。
服务器
while(1) {
...
accept(...);
Sleep(Number_of_miliseconds);//You are using win32 imp, for POSIX sleep() this will be in seconds
...
}