📄 function_tcp.c
字号:
/*--------------------------------------------------------
A series of functions used for the TCP/IP operation is
listed here one by one. The function was not available,
created by whf on Dec.11,2006.
---------------------------------------------------------*/
#include "Function_TCP.h"
#include "MyDefine.h"
int socket( int domain, int type, int protocol )
{
return 1;
}
int bind( int s, PSA pName, int len )
{
return 1;
}
int connect( int s, PSA pName, int len )
{
return 1;
}
int listen( int s, int maxcon )
{
return 1;
}
int accept( int s, PSA pName, int *plen )
{
return 1;
}
int recv( int s, void *pbuf, int size, int flags )
{
return 1;
}
#define SHUT_RD 0
#define SHUT_WR 1
#define SHUT_RDWR 2
int shutdown( int s, int how )
{
switch(how):
case 0:
{
close_RD(s);
break;
}
case 1:
{
close_WR(s);
break;
}
case 2:
{
close_RDWR(s);
break;
}
default:
{
printf("Error");
return -1;
}
return 0;
}
/*----------------------------------------------------------------------
This Connnect function realize the connection function with the output
IP address. Connect( IPN IPAddr);
----------------------------------------------------------------------*/
static void Connect( IPN IPAddr )
{
SOCKET s;
struct sockaddr_in sin1;
int tmp;
struct timeval timeout;
fd_set obits;
int cnt;
// Create connected socket
s = socket(AF_INET, SOCK_STREAMNC, IPPROTO_TCP);
if( s < 0 )
{
//ConPrintf("failed socket create (%d)\n",fdError());
goto leave;
}
// Prepare address for connect
bzero( &sin1, sizeof(struct sockaddr_in) );
sin1.sin_family = AF_INET;
sin1.sin_len = sizeof( sin1 );
sin1.sin_addr.s_addr = IPAddr;
sin1.sin_port = htons(7);
// Setup for non-blocking
tmp = 0;
setsockopt( s, SOL_SOCKET, SO_BLOCKING, &tmp, sizeof( tmp ) );
// Connect socket
if( connect( s, (PSA) &sin1, sizeof(sin1) ) < 0 )
{
tmp = fdError();
if( tmp != EINPROGRESS )
{
//ConPrintf("Connect returned error %d\n",tmp);
goto leave;
}
//ConPrintf("Connect returned EINPROGRESS\n");
//ConPrintf("Setting fdSelect() timeout to 8 seconds\n");
timeout.tv_sec = 8;
timeout.tv_usec = 0;
FD_ZERO(&obits);
FD_SET(s,&obits);
cnt = fdSelect( s, 0, &obits, 0, &timeout );
if( !cnt )
;//ConPrintf("Connection timeout!\n");
else
{
//ConPrintf("Socket reports writable\n");
if( connect( s, (PSA) &sin1, sizeof(sin1) ) < 0 )
{
tmp = fdError();
if( tmp == EISCONN )
{
//ConPrintf("Connect returned EISCONN\n");
goto leave;
}
else if( tmp == ECONNREFUSED )
{
//ConPrintf("Connect returned ECONNREFUSED\n");
goto leave;
}
//ConPrintf("Connect returned error %d\n",tmp);
}
}
//ConPrintf("Connect failed\n");
}
if( !tmp )
//ConPrintf("Connect returned success (IP address was local?)\n");
leave:
if( s >= 0 )
fdClose( s );
//ConPrintf("== End Non-Blocking Connect Test ==\n\n");
}
int send( int s, void *pbuf, int size, int flags )
{
return 1;
}
/*----------------------------------------------------------------------
send() function to realize the task of sending the information to
the specific address here. Test sending on a TCP socket
----------------------------------------------------------------------*/
#define SEND_ITER 25000
static int Send(int s, void *pbuf, int size, int flags, IPN IPAddr )
{
//SOCKET s;
struct sockaddr_in sin1;
Uint32 test,j;
char *pBuf = 0;
struct timeval timeout;
Uint32 startS, startMS;
Uint32 endS, endMS;
ConPrintf("\n== Start TCP Send Test ==\n");
// Create test socket
s = socket(AF_INET, SOCK_STREAMNC, IPPROTO_TCP);
if( s < 0 )
{
ConPrintf("failed socket create (%d)\n",fdError());
goto leave;
}
// Prepare address for connect
bzero( &sin1, sizeof(struct sockaddr_in) );
sin1.sin_family = AF_INET;
sin1.sin_len = sizeof( sin1 );
sin1.sin_addr.s_addr = IPAddr;
sin1.sin_port = htons(1001);
// Configure our timeout to be 5 seconds
timeout.tv_sec = 5;
timeout.tv_usec = 0;
setsockopt( s, SOL_SOCKET, SO_SNDTIMEO, &timeout, sizeof( timeout ) );
setsockopt( s, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof( timeout ) );
// Connect socket
if ( connect( s, (PSA)&sin1, sizeof(sin1) ) < 0 )
{
//ConPrintf("failed connect (%d)\n",fdError());
goto leave;
}
// Allocate a working buffer
if( !(pBuf = mmBulkAlloc( 8192 )) )
{
//ConPrintf("failed temp buffer allocation\n");
goto leave;
}
startS = llTimerGetTime( &startMS );
// Start Test
test = 8192;
for( j=0; j<SEND_ITER; j++ )
{
// Send the buffer
if( send( s, pBuf, (int)test, 0 ) < 0 )
{
//ConPrintf("send failed (%d)\n",fdError());
break;
}
}
if( j == SEND_ITER )
{
endS = llTimerGetTime( &endMS );
endS -= startS;
endS *= 1000;
endMS += endS;
endMS -= startMS;
endS = (endMS+50)/100;
//ConPrintf("Passed in %d ms, %d bytes/sec\n",endMS,(j*test*10)/endS);
}
return 1;
leave:
if( pBuf )
{
mmBulkFree( pBuf );
return -1;
}
if( s >= 0 )
{
return -1;
fdClose( s );
}
//ConPrintf("== End TCP Send Test ==\n\n");
return 1;
}
// Pipe Oriented Functions
int pipe( int *pfd1, int *pfd2 )
{
int *p;
*p=*pfd2;
*pfd2=*pfd1;
return 1;
}
void closesocket(int x)
{
return;
}
unsigned long inet_ntoa(struct in_addr in)
{
unsigned long p;
p=168100001001;
return p;
}
/*-----------------------Standby Here for Further use--------------------
// File Descriptor Functions
int fdOpenSession( Handle hTask );
void fdCloseSession( Handle hTask );
int fdSelect( int width, fd_set *readfds, fd_set *writefds,
fd_set *exceptfds, struct timeval *timeout );
void fdSelectAbort( Handle hTask );
int fdClose( int fd );
Handle fdGetFileHandle( int fd );
int fdError();
int fdTransfer( Handle hSrcTask, int srcfd, Handle hDstTask, int *pdstfd );
int fdShare( Handle hSrcTask, int srcfd,
Handle hDstTask, int dstfdpref, int *pdstfd );
int fdShareAll( Handle hSrcTask, Handle hDstTask );
// File Handle Functions
void FileHandleClose( Handle h );
int FileHandleGetFd( Handle h );
// Socket Oriented Functions
int accept( int s, PSA pName, int *plen );
int bind( int s, PSA pName, int len );
int connect( int s, PSA pName, int len );
int getpeername( int s, PSA pName, int *plen );
int getsockname( int s, PSA pName, int *plen );
int getsockopt( int s, int level, int op, void *pbuf, int *pbufsize );
int listen( int s, int maxcon );
int recv( int s, void *pbuf, int size, int flags );
int recvfrom( int s, void *pbuf, int size, int flags, PSA pName, int *plen );
int recvnc( int s, void **ppbuf, int flags, Handle *pHandle );
int recvncfrom( int s, void **ppbuf, int flags,
PSA pName, int *plen, Handle *pHandle );
void recvncfree( Handle Handle );
int send( int s, void *pbuf, int size, int flags );
int sendto( int s, void *pbuf, int size, int flags, PSA pName, int len );
int setsockopt( int s, int level, int op, void *pbuf, int bufsize );
int shutdown( int s, int how );
int socket( int domain, int type, int protocol );
int socketpair( int domain, int type, int protocol, int s[2] );
// Pipe Oriented Functions
int pipe( int *pfd1, int *pfd2 );
--------------------------------------------------------------------------------*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -