• 全部
  • 问答

我写的红外通讯的服务端,怎么谁也连接不上呢?(有源码,高分求助)

oilinsea 2005-07-15 10:18:20
我自己写的一个红外的通讯程序,服务端源码如下:
#define _WIN32_WINNT 0x0500
#define WINVER 0x0500
#define WIN32_LEAN_AND_MEAN
#define UNICODE 1
#define _UNICODE 1

#include <WinSock2.h>
#include <STDIO.H> //wprintf etc
#include "af_irda.h"
#pragma comment ( lib, "WS2_32.Lib" )

void ErrOut()
{
LPVOID lpMsgBuf;
DWORD errCode=GetLastError();
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
errCode,
MAKELANGID(LANG_ENGLISH, SUBLANG_DEFAULT), // Default language
(LPTSTR) &lpMsgBuf,
0,
NULL);
printf("%sError Code=%d\n",(LPTSTR)lpMsgBuf,errCode);

LocalFree( lpMsgBuf );
}

int main()
{
WSADATA wsaData;
SOCKET ServerSock, // IR socket bound to the server
ClientSock; // IR socket bound to the client

SOCKADDR_IRDA address = {AF_IRDA, 0, 0, 0, 0, "IrDA:IrCOMM"};
// Specifies the server socket address
int index = 0, // Integer index
iReturn; // Return value of recv function
char szServerA[100]; // ASCII string

WSAStartup(MAKEWORD(1,1),&wsaData);

// Create a socket bound to the server.
if ((ServerSock = socket (AF_IRDA, SOCK_STREAM, 0)) == INVALID_SOCKET)
{
ErrOut();
return FALSE;
}

// Associate the server socket address with the server socket.
printf("\nbegin bind");
if (bind (ServerSock, (struct sockaddr *)&address, sizeof (address))
== SOCKET_ERROR)
{
ErrOut();
closesocket (ServerSock);
return FALSE;
}

// Establish a socket to listen for incoming connections.
printf("\nbegin listen");
if (listen (ServerSock, 5) == SOCKET_ERROR)
{
ErrOut();
closesocket (ServerSock);
return FALSE;
}

// Accept a connection on the socket.
printf("\nwait connect");
if ((ClientSock = accept (ServerSock, 0, 0)) == INVALID_SOCKET)
{
ErrOut();
closesocket (ServerSock);
return FALSE;
}

// Stop listening for connections from clients.
closesocket (ServerSock);

// Send a string from the server socket to the client socket.
printf("\nmessage \"To Client\" sent to client");
if (send (ClientSock, "To Client!", strlen ("To Client!") + 1, 0)
== SOCKET_ERROR)
{
ErrOut();
}

// Receive data from the client.
printf("receive data");
iReturn = recv (ClientSock, szServerA, sizeof (szServerA), 0);

// Check if there is any data received. If there is, display it.
if (iReturn == SOCKET_ERROR)
{
ErrOut();
}
else if (iReturn == 0)
{
printf("Finished receiving data\n");
}
else
{
printf("Received From Client:%s\n",szServerA);
}

// Close the client and server sockets.
closesocket (ClientSock);
WSACleanup();

return 0;
}
这段代码不知怎么搞的,总是停在accept,客户端怎么也连接不上。
渴求各位高手给以指点!
...全文
66 点赞 收藏 3
写回复
3 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
jenycheng 2005-07-15
你绑定的是哪个端口啊!?


不是 0 吧?
回复
oilinsea 2005-07-15
客户端源码如下:
//Windows 2000
#define _WIN32_WINNT 0x0500
#define WINVER 0x0500
#define WIN32_LEAN_AND_MEAN
#define UNICODE 1
#define _UNICODE 1

#include <WinSock2.h>
#include "af_irda.h"
#include <stdio.h>
#pragma comment ( lib, "WS2_32.Lib" )

void ErrOut()
{
LPVOID lpMsgBuf;
DWORD errCode=GetLastError();
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
errCode,
MAKELANGID(LANG_ENGLISH, SUBLANG_DEFAULT), // Default language
(LPTSTR) &lpMsgBuf,
0,
NULL);
printf("%sError Code=%d\n",(LPTSTR)lpMsgBuf,errCode);

LocalFree( lpMsgBuf );
}

#define NUMRETYR 5 // Maximum number of retries
int main()
{
WSADATA wsaData;
SOCKET sock; // Socket bound to the server
DEVICELIST devList; // Device list
SOCKADDR_IRDA address = {AF_IRDA, 0, 0, 0, 0, "IrDA:IrCOMM"};
// Specifies the server socket address
int iCount = 0, // Number of retries
index = 0, // Integer index
iReturn, // Return value of recv function
iDevListLen = sizeof (devList);
// Size of the device list
char szClientA[100]; // ASCII string

WSAStartup(MAKEWORD(1,1),&wsaData);
// Create a socket that is bound to the server.
if ((sock = socket (AF_IRDA, SOCK_STREAM, 0)) == INVALID_SOCKET)
{
ErrOut();
return FALSE;
}

// Initialize the number of devices to zero.
devList.numDevice = 0;
printf("\nsearch device");
while ( (devList.numDevice == 0) && (iCount <= NUMRETYR))
{
// Retrieve the socket option.
if (getsockopt (sock, SOL_IRLMP, IRLMP_ENUMDEVICES,
(char *)&devList, &iDevListLen) == SOCKET_ERROR)
{
ErrOut();
closesocket (sock);
return FALSE;
}

iCount++;

// Wait one second before retrying.
Sleep (1000);
}

printf("\ndevice num : %d",iCount);
if (iCount > NUMRETYR)
{
printf("Cannot locate server.\n");
closesocket (sock);
return FALSE;
}

// Get the server socket address.
for (index = 0; index <= 3; index++)
{
address.irdaDeviceID[index] = devList.Device[0].irdaDeviceID[index];
}

// Establish a connection to the socket.
printf("\nbegin connect");
if (connect (sock, (struct sockaddr *)&address,
sizeof (SOCKADDR_IRDA)) == SOCKET_ERROR)
{
ErrOut();
closesocket (sock);
return FALSE;
}

// Send a string from the client socket to the server socket.
printf("\nsend message :\"To Server\"");
if (send (sock, "To Server.", strlen ("To Server.") + 1, 0)
== SOCKET_ERROR)
{
ErrOut();
}

// Receive data from the server socket.
printf("\n receiving");
iReturn = recv (sock, szClientA, sizeof (szClientA), 0);

// Check if there is any data received. If there is, display it.
printf("\nreceived num: %d",iReturn);
if (iReturn == SOCKET_ERROR)
{
ErrOut();
}
else if (iReturn == 0)
{
//MessageBox (NULL, TEXT("Finished receiving data"), TEXT("Client"),MB_OK);
printf("Finished receiving data\n");
}
else
{
printf("Received From Server:%s\n",szClientA);
}

// Close the socket.
closesocket (sock);
WSACleanup();

return 0;
}
客户端在与服务端连接的时候总是失败。
回复
oilinsea 2005-07-15
绑定是红外的IrComm端口
SOCKADDR_IRDA address = {AF_IRDA, 0, 0, 0, 0, "IrDA:IrCOMM"};
回复
发帖
通信技术
创建于2007-08-27

4028

社区成员

通信技术相关讨论
申请成为版主
帖子事件
创建了帖子
2005-07-15 10:18
社区公告
暂无公告