当前位置: 技术问答>linux和unix
帮我看看UDP的问题
来源: 互联网 发布时间:2015-06-02
本文导语: 下面代码是从widnows下移植过来的, windows下执行很好.但是linux下就是无法接收到数据?高手帮我 看看,有没有什么问题? // 返回如果为0,表示没有数据 int CUdp::ReceiveData(char* buf, int* readLen, unsigned int* fromIp, int* fr...
下面代码是从widnows下移植过来的, windows下执行很好.但是linux下就是无法接收到数据?高手帮我 看看,有没有什么问题?
// 返回如果为0,表示没有数据
int CUdp::ReceiveData(char* buf, int* readLen, unsigned int* fromIp, int* fromPort, int waitTime)
{
FD_ZERO(fds);
FD_SET(s,fds);
timeOut.tv_sec = waitTime /1000;
timeOut.tv_usec = waitTime % 1000;
unsigned int addrLen = sizeof(remoteAddr);
if (select(0,fds,NULL,NULL,&timeOut)>0 ) // 检查是否有数据
*readLen = recvfrom(s, buf, 2048, 0,(SOCKADDR *)&remoteAddr,&addrLen);
else
return 0;
printf ("recv ok");
if ( fromIp != NULL)
*fromIp = ntohl(remoteAddr.sin_addr.s_addr);
if ( fromPort != NULL)
*fromPort = (int)ntohs(remoteAddr.sin_port);
return 1;
}
// 返回如果为0,表示没有数据
int CUdp::ReceiveData(char* buf, int* readLen, unsigned int* fromIp, int* fromPort, int waitTime)
{
FD_ZERO(fds);
FD_SET(s,fds);
timeOut.tv_sec = waitTime /1000;
timeOut.tv_usec = waitTime % 1000;
unsigned int addrLen = sizeof(remoteAddr);
if (select(0,fds,NULL,NULL,&timeOut)>0 ) // 检查是否有数据
*readLen = recvfrom(s, buf, 2048, 0,(SOCKADDR *)&remoteAddr,&addrLen);
else
return 0;
printf ("recv ok");
if ( fromIp != NULL)
*fromIp = ntohl(remoteAddr.sin_addr.s_addr);
if ( fromPort != NULL)
*fromPort = (int)ntohs(remoteAddr.sin_port);
return 1;
}
|
/*--
Copyright (c) 2003 Shenzhen Surfilter Network Technology Co.Ltd
File Name:
bsocket.h
Version:
1.0
Abstract:
Declarations/definitions for BSD socket operation
Author:
Gang He
Created on:
2003-11-12
Modified History:
Modified Person:
--*/
#ifndef _HEGANG_BSOCKET_H_
#define _HEGANG_BSOCKET_H_
#include
#include
#ifdef __cplusplus
extern "C" {
#endif
/* Define compatible data types */
typedef int SOCKET;
typedef unsigned short PORT;
#define INVALID_SOCKET 0
#define SOCKET_RECV_TIMEOUT -2
/* Define the maximum length the listen queue */
#define MAX_LISTEN_NUM 5
/*-
Name:
SOCKET CreateSock(int nDomain, int nType, int nProtocol)
Description:
Create a socket.
Input Parameters:
nDomain: the protocol family
nType: socket type
nProtocol: a particular protocol
Output Parameters:
nothing
Return Value:
a socket descriptor, otherwise -1 is returned if an error occurred.
-*/
SOCKET CreateSock(int nDomain, int nType, int nProtocol);
/*-
Name:
int BindSock(SOCKET nSock, PORT wPort)
Description:
Bind a socket.
Input Parameters:
nSock: socket descriptor
wPort: port to be binded
Output Parameters:
nothing
Return Value:
0, otherwise -1 is returned if an error occurred.
-*/
int BindSock(SOCKET nSock, PORT wPort);
/*-
Name:
int ListenSock(SOCKET nSock, PORT wPort)
Description:
Bind and listen a socket.
Input Parameters:
nSock: socket descriptor
wPort: port to be binded
Output Parameters:
nothing
Return Value:
0, otherwise -1 is returned if an error occurred.
-*/
int ListenSock(SOCKET nSock, PORT wPort);
/*-
Name:
SOCKET AcceptSock(SOCKET nSock, char* szIpAddr)
Description:
Accept a socket.
Input Parameters:
nSock: socket descriptor
Output Parameters:
szIpAddr: peer IP address, this buffer must be larger or equal to 16 bytes,
otherwise, set szIpAddr NULL.
Return Value:
a socket descriptor, otherwise -1 is returned if an error occurred.
-*/
SOCKET AcceptSock(SOCKET nSock, char* szIpAddr);
/*-
Name:
int ConnectSock(SOCKET nSock, char* szIpAddr, PORT wPort)
Description:
Connect a socket.
Input Parameters:
nSock: socket descriptor
szIpAddr: peer IP address
wPort: peer port
Output Parameters:
nothing
Return Value:
0, otherwise -1 is returned if an error occurred.
-*/
int ConnectSock(SOCKET nSock, const char* szIpAddr, PORT wPort);
/*-
Name:
int SendSock(SOCKET nSock, const void* pBuf, size_t nBufSize)
Description:
Transmit a message by socket.
Input Parameters:
nSock: socket descriptor
pBuf: a message buffer
nBufSize: a message buffer size
Output Parameters:
nothing
Return Value:
the number of characters sent, otherwise -1 is returned if an error occurred.
-*/
int SendSock(SOCKET nSock, const void* pBuf, size_t nBufSize);
/*-
Name:
int SendToSock(SOCKET nSock, const char* szIpAddr, PORT wPort, const void* pBuf, size_t nBufSize)
Description:
Transmit a message by socket.
Input Parameters:
nSock: socket descriptor
szIpAddr: peer ip address
wPort: peer port
pBuf: a message buffer
nBufSize: a message buffer size
Output Parameters:
nothing
Return Value:
the number of characters sent, otherwise -1 is returned if an error occurred.
-*/
int SendToSock(SOCKET nSock, const char* szIpAddr, PORT wPort, const void* pBuf, size_t nBufSize);
/*-
Name:
int RecvSock(SOCKET nSock, void* pBuf, size_t nBufSize, int nSec)
Description:
Receive a message from a socket
Input Parameters:
nSock: socket descriptor
nSec: overtime seconds
Output Parameters:
pBuf: a message buffer
nBufSize: a message buffer size
Return Value:
the number of characters received, otherwise,
-1 is returned if an error occurred,
-2 is returned if timeout.
-*/
int RecvSock(SOCKET nSock, void* pBuf, size_t nBufSize, int nSec);
/*-
Name:
int RecvFromSock(SOCKET nSock, char* szIpAddr, PORT* pwPort, void* pBuf, size_t nBufSize)
Description:
Receive a message from a socket
Input Parameters:
nSock: socket descriptor
Output Parameters:
szIpAddr: peer ip address
pwPort: peer port
pBuf: a message buffer
nBufSize: a message buffer size
Return Value:
the number of characters received, otherwise,
-1 is returned if an error occurred,
-*/
int RecvFromSock(SOCKET nSock, char* szIpAddr, PORT* pwPort, void* pBuf, size_t nBufSize);
/*-
Name:
void CloseSock(SOCKET* pSock)
Description:
Close a socket
Input Parameters:
pSock: socket pointer
Output Parameters:
nothing
Return Value:
nothing
-*/
void CloseSock(SOCKET* pSock);
#ifdef __cplusplus
}
#endif
#endif
Copyright (c) 2003 Shenzhen Surfilter Network Technology Co.Ltd
File Name:
bsocket.h
Version:
1.0
Abstract:
Declarations/definitions for BSD socket operation
Author:
Gang He
Created on:
2003-11-12
Modified History:
Modified Person:
--*/
#ifndef _HEGANG_BSOCKET_H_
#define _HEGANG_BSOCKET_H_
#include
#include
#ifdef __cplusplus
extern "C" {
#endif
/* Define compatible data types */
typedef int SOCKET;
typedef unsigned short PORT;
#define INVALID_SOCKET 0
#define SOCKET_RECV_TIMEOUT -2
/* Define the maximum length the listen queue */
#define MAX_LISTEN_NUM 5
/*-
Name:
SOCKET CreateSock(int nDomain, int nType, int nProtocol)
Description:
Create a socket.
Input Parameters:
nDomain: the protocol family
nType: socket type
nProtocol: a particular protocol
Output Parameters:
nothing
Return Value:
a socket descriptor, otherwise -1 is returned if an error occurred.
-*/
SOCKET CreateSock(int nDomain, int nType, int nProtocol);
/*-
Name:
int BindSock(SOCKET nSock, PORT wPort)
Description:
Bind a socket.
Input Parameters:
nSock: socket descriptor
wPort: port to be binded
Output Parameters:
nothing
Return Value:
0, otherwise -1 is returned if an error occurred.
-*/
int BindSock(SOCKET nSock, PORT wPort);
/*-
Name:
int ListenSock(SOCKET nSock, PORT wPort)
Description:
Bind and listen a socket.
Input Parameters:
nSock: socket descriptor
wPort: port to be binded
Output Parameters:
nothing
Return Value:
0, otherwise -1 is returned if an error occurred.
-*/
int ListenSock(SOCKET nSock, PORT wPort);
/*-
Name:
SOCKET AcceptSock(SOCKET nSock, char* szIpAddr)
Description:
Accept a socket.
Input Parameters:
nSock: socket descriptor
Output Parameters:
szIpAddr: peer IP address, this buffer must be larger or equal to 16 bytes,
otherwise, set szIpAddr NULL.
Return Value:
a socket descriptor, otherwise -1 is returned if an error occurred.
-*/
SOCKET AcceptSock(SOCKET nSock, char* szIpAddr);
/*-
Name:
int ConnectSock(SOCKET nSock, char* szIpAddr, PORT wPort)
Description:
Connect a socket.
Input Parameters:
nSock: socket descriptor
szIpAddr: peer IP address
wPort: peer port
Output Parameters:
nothing
Return Value:
0, otherwise -1 is returned if an error occurred.
-*/
int ConnectSock(SOCKET nSock, const char* szIpAddr, PORT wPort);
/*-
Name:
int SendSock(SOCKET nSock, const void* pBuf, size_t nBufSize)
Description:
Transmit a message by socket.
Input Parameters:
nSock: socket descriptor
pBuf: a message buffer
nBufSize: a message buffer size
Output Parameters:
nothing
Return Value:
the number of characters sent, otherwise -1 is returned if an error occurred.
-*/
int SendSock(SOCKET nSock, const void* pBuf, size_t nBufSize);
/*-
Name:
int SendToSock(SOCKET nSock, const char* szIpAddr, PORT wPort, const void* pBuf, size_t nBufSize)
Description:
Transmit a message by socket.
Input Parameters:
nSock: socket descriptor
szIpAddr: peer ip address
wPort: peer port
pBuf: a message buffer
nBufSize: a message buffer size
Output Parameters:
nothing
Return Value:
the number of characters sent, otherwise -1 is returned if an error occurred.
-*/
int SendToSock(SOCKET nSock, const char* szIpAddr, PORT wPort, const void* pBuf, size_t nBufSize);
/*-
Name:
int RecvSock(SOCKET nSock, void* pBuf, size_t nBufSize, int nSec)
Description:
Receive a message from a socket
Input Parameters:
nSock: socket descriptor
nSec: overtime seconds
Output Parameters:
pBuf: a message buffer
nBufSize: a message buffer size
Return Value:
the number of characters received, otherwise,
-1 is returned if an error occurred,
-2 is returned if timeout.
-*/
int RecvSock(SOCKET nSock, void* pBuf, size_t nBufSize, int nSec);
/*-
Name:
int RecvFromSock(SOCKET nSock, char* szIpAddr, PORT* pwPort, void* pBuf, size_t nBufSize)
Description:
Receive a message from a socket
Input Parameters:
nSock: socket descriptor
Output Parameters:
szIpAddr: peer ip address
pwPort: peer port
pBuf: a message buffer
nBufSize: a message buffer size
Return Value:
the number of characters received, otherwise,
-1 is returned if an error occurred,
-*/
int RecvFromSock(SOCKET nSock, char* szIpAddr, PORT* pwPort, void* pBuf, size_t nBufSize);
/*-
Name:
void CloseSock(SOCKET* pSock)
Description:
Close a socket
Input Parameters:
pSock: socket pointer
Output Parameters:
nothing
Return Value:
nothing
-*/
void CloseSock(SOCKET* pSock);
#ifdef __cplusplus
}
#endif
#endif
|
select(0,fds,NULL,NULL,&timeOut)
~~~~ s+1 不是 0 的了
~~~~ s+1 不是 0 的了
|
if (select(0,fds,NULL,NULL,&timeOut)>0 ) // 检查是否有数据
0-------------------------absolutely not "0" here
0-------------------------absolutely not "0" here