📄 helloworld.c
字号:
/*
* Copyright 2006 by Texas Instruments Incorporated.
* All rights reserved. Property of Texas Instruments Incorporated.
* Restricted rights to use, duplicate or disclose this code are
* granted through contract.
*
* @(#) TCP/IP_Network_Developers_Kit 1.91.00.08 08-22-2006 (ndk-a08)
*/
//---------------------------------------------------------------------------
// UDP Client Program
//---------------------------------------------------------------------------
// HELLOWORLD.C
//
// Test the crude echo by sending to port 7 and waiting
// for a reply.
//
// Author: Michael Denio
// Magdalena Iovescu
//---------------------------------------------------------------------------
#include <windows.h>
#include <stdio.h>
#include <time.h>
#include <winsock.h>
#define TESTSIZE 15
int main( int argc, char *argv[] )
{
WORD wVersionRequested;
WSADATA wsaData;
SOCKET s;
struct sockaddr_in sin;
unsigned char *pBuf = "Hello World!\n";
int tmp1,tmp2,tmp3,tmp4;
int idle;
fd_set readfds;
struct timeval timeout;
time_t ts,tn;
wVersionRequested = MAKEWORD(1, 1);
tmp1 = WSAStartup(wVersionRequested, &wsaData);
if (tmp1 != 0)
{
printf("\r\nUnable to initialize WinSock for host info");
exit(0);
}
if( (argc != 2) ||
sscanf(argv[1],"%03d.%03d.%03d.%03d",&tmp1,&tmp2,&tmp3,&tmp4)!=4 ||
(tmp1 < 0 || tmp1 > 255 || tmp2 < 0 || tmp2 > 255 ||
tmp3 < 0 || tmp3 > 255 || tmp4 < 0 || tmp4 > 255) )
{
printf("\nUsage: HELLOWORLD <x.x.x.x>\n");
goto leave;
}
tmp1 |= tmp2 << 8;
tmp1 |= tmp3 << 16;
tmp1 |= tmp4 << 24;
printf("\nTesting DSP UDP echo server at %d.%d.%d.%d\n",
tmp1&255, (tmp1>>8)&255, (tmp1>>16)&255, (tmp1>>24)&255 );
s = socket( AF_INET, SOCK_DGRAM, 0 );
if( s < 0 )
{
printf("failed socket (%d)\n",WSAGetLastError());
goto leave;
}
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = 0;
sin.sin_port = htons(1234);
if ( bind( s, &sin, sizeof(sin) ) < 0 )
{
printf("failed bind (%d)\n",WSAGetLastError());
goto leave;
}
idle = 0;
timeout.tv_sec = 0;
timeout.tv_usec = 0;
sin.sin_addr.s_addr = tmp1;
sin.sin_port = htons(7);
ts = time(0);
// Send the string to the DSP
if( sendto( s, pBuf, strlen(pBuf), 0, &sin, sizeof(sin) ) < 0 )
{
printf("send failed (%d)\n",WSAGetLastError());
goto leave;
}
// Now try and receive a packet
idle = 0;
do
{
FD_ZERO( &readfds );
FD_SET( s, &readfds );
if( !select( 0, &readfds, 0, 0, &timeout ) )
{
if( ++idle == 50000 )
{
printf("Receive timeout\n");
goto leave;
}
}
else
break;
} while(1);
tmp1 = recv( s, pBuf, TESTSIZE, 0 );
// Print the data echoed back
if (tmp1 > 0)
printf("\nReceived back %d bytes.\nData Echoed back: %s\n",tmp1, pBuf);
tn = time(0) - ts;
printf("Exiting test. Time = %d seconds",tn);
leave:
if( pBuf )
free( pBuf );
if( s >= 0 )
closesocket( s );
WSACleanup();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -