📄 noncopytcptester.c
字号:
ReportStr[Report/256], Report&0xFF );
//
// Example of adding to the DHCP configuration space
//
// When using the DHCP client, th client has full control over access
// to the first 256 entries in the CFGTAG_SYSINFO space.
//
// Note that the DHCP client will erase all CFGTAG_SYSINFO tags except
// CFGITEM_DHCP_HOSTNAME. If the application needs to keep manual
// entries in the DHCP tag range, then the code to maintain them should
// be placed here.
//
// Here, we want to manually add a DNS server to the configuration, but
// we can only do it once DHCP has finished its programming.
//
if( Item == CFGITEM_SERVICE_DHCPCLIENT &&
Status == CIS_SRV_STATUS_ENABLED &&
(Report == (NETTOOLS_STAT_RUNNING|DHCPCODE_IPADD) ||
Report == (NETTOOLS_STAT_RUNNING|DHCPCODE_IPRENEW)) )
{
IPN IPTmp;
// Manually add the DNS server when specified
IPTmp = inet_addr(DNSServer);
if( IPTmp )
CfgAddEntry( 0, CFGTAG_SYSINFO, CFGITEM_DHCP_DOMAINNAMESERVER,
0, sizeof(IPTmp), (UINT8 *)&IPTmp, 0 );
}
}
//
// TCP Send Client
// (SOCK_STREAM @ port 1001)
//
void sendclient()
{
char *IPAddrSend = TESTEE_IPADDR_STRING;
SOCKET stcp = INVALID_SOCKET;
struct sockaddr_in sin1;
int count, i, j;
char *pBuf = "Hello World\n";
UINT32 bufferSize;
IPN IPAddr;
TaskSleep(100);
printf(" Sending Task started\n");
for (i=0 ; i < sizeof(frameSize)/sizeof(frameSize[0]); i++) {
for (j=0; j < sizeof(frameRate)/sizeof(frameRate[0]); j++) {
printf (" Sending %d frames of %d at ", BENCH_FRAMES,frameSize[i]);
if (frameRate[j] == 0) {
printf ("maximum frame rate\n");
}
else {
printf ("%d ms frame rate\n", frameRate[j]);
}
// Allocate the file environment for this task
fdOpenSession( TaskSelf() );
// Create the main TCP listen socket
stcp = socket(AF_INET, SOCK_STREAM, 0);
if( stcp == INVALID_SOCKET ) {
printf("Fail socket, %d\n", fdError());
goto leave;
}
// Set Port = 1001, IP address = IPAddrSend
IPAddr = inet_addr(IPAddrSend);
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);
// Connect socket
if ( connect( stcp, (PSA) &sin1, sizeof(sin1) ) < 0 ) {
printf("Fail connect, %d\n", fdError());
goto leave;
}
bufferSize = frameSize[i];
// Allocate a working buffer of bufferSize
if( !(pBuf = mmBulkAlloc( bufferSize )) ) {
printf("failed temp buffer allocation\n");
goto leave;
}
*(int *)pBuf = bufferSize;
if( send( stcp, pBuf, sizeof(int), 0 ) < 0 ) {
printf("send failed (%d)\n",fdError());
goto leave;
}
for( count =0; count < BENCH_FRAMES; count++ ) {
if( send( stcp, pBuf, (int)bufferSize, 0 ) < 0 ) {
printf("send failed (%d)\n",fdError());
break;
}
if (frameRate[j] > 0) {
TaskSleep(frameRate[j]);
}
}
leave:
if( pBuf ) {
mmBulkFree( pBuf );
}
// We only get here on an error - close the sockets
if( stcp != INVALID_SOCKET ) {
fdClose( stcp );
}
TaskSleep(1000);
}
}
printf(" Send Task terminated\n");
//Create the Receice Task
TaskCreate( recvclient, "RecvClient", OS_TASKPRINORM, 0x1400, 0, 0, 0 );
// This task is killed by the system - here, we block
TaskBlock( TaskSelf() );
}
//
// TCP Receive Client
// (SOCK_STREAM @ port 1000)
//
void recvclient()
{
char *IPAddrRecv = TESTEE_IPADDR_STRING;
SOCKET stcp = INVALID_SOCKET;
struct sockaddr_in sin1;
int count, i, j, k, trial, skipoutput,tmp;
char *pBuf = 0;
UINT32 bufferSize;
IPN IPAddr;
printf(" Receive Task started\n");
for (i=0 ; i < sizeof(frameSize)/sizeof(frameSize[0]); i++) {
for (j=0; j < sizeof(frameRate)/sizeof(frameRate[0]); j++) {
/* Send data size requested */
printf(" Requesting %d bytes frames at ",frameSize[i]);
if (frameRate[j] == 0) {
printf ("maximum frame rate...\n");
}
else {
printf ("%d ms frame rate...\n", frameRate[j]);
}
// Allocate the file environment for this task
fdOpenSession( TaskSelf() );
// Create the main TCP socket
stcp = socket(AF_INET, SOCK_STREAM, 0);
if( stcp == INVALID_SOCKET ) {
goto leave;
}
// Set Port = 1000, IP address = IPAddrRecv
IPAddr = inet_addr(IPAddrRecv);
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(1000);
// Connect socket
if ( connect( stcp, (PSA) &sin1, sizeof(sin1) ) < 0 ) {
printf("failed to connect\n");
goto leave;
}
bufferSize = frameSize[i];
// Allocate a working buffer
if( !(pBuf = mmBulkAlloc( bufferSize )) ) {
printf(" ...failed temp buffer allocation\n");
goto leave;
}
trial = 0;
skipoutput = BENCH_FRAMES;
/* Send data size requested */
*(int *)pBuf = bufferSize;
if( send( stcp, pBuf, sizeof(int), 0 ) < 0 ) {
printf(" ...send failed (%d)\n",fdError());
goto leave;
}
// Loop through the number of frames
for (count =0; count < BENCH_FRAMES; count++) {
trial++;
skipoutput--;
// Try and receive the test pattern
if( !skipoutput ) {
printf(" ...received %d frames\n", trial);
}
k = 0;
while( k < (int)bufferSize ) {
tmp = recv( stcp, pBuf+k, bufferSize-k, 0 );
if( tmp < 0 ) {
printf("recv failed (%d)\n",fdError());
break;
}
if( tmp == 0 ) {
printf("recv failed - no data\n");
break;
}
k += tmp;
}
if (frameRate[j] > 0) {
TaskSleep(frameRate[j]);
}
// Verify reception size
if( k != bufferSize ) {
printf("received %d (not %d) bytes\n",k,bufferSize);
break;
}
}
leave:
if( pBuf ) {
mmBulkFree( pBuf );
}
// We only get here on an error - close the sockets
if( stcp != INVALID_SOCKET ) {
fdClose( stcp );
}
TaskSleep(1000);
}
}
printf(" Receive Task terminated\n");
// This task is killed by the system - here, we block
TaskBlock( TaskSelf() );
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -