📄 inetd_client.c
字号:
/****************************************************************************
Copyright(c) 2004 Analog Devices, Inc. All Rights Reserved.
This software is proprietary and confidential. By using this software you agree
to the terms of the associated Analog Devices License Agreement.
Description:
from the command line compile using 'cl inetd_client.c ws2_32.lib'
This program has to be used in conjunction with the inetd server application
running on the Blackfin. This client program uses windows sockets and connects
to the servers running on the blackfin ez-kits.
parameters:
<ip_addr> : IP address of the blackfin board, Printed during the start of
the inetd server application.
<serv_port> : Port number on which server is running. Three server are
running on the server application, So supported port numbers
are as below.
19 -- for chargen server.(outputs characters to the console)
9 -- for discard server (sends type chars to the server)
7 -- echo server. (sends and receives chars' prints them )
press ctrl+c to terminate the application
******************************************************************************/
#include <stdio.h>
#include <winsock.h>
#include <windows.h>
#define ECHO_SERVER_PORT 7
#define DISCARD_SERVER_PORT 9
#define CHAR_GEN_SERVER_PORT 19
#ifdef _DEBUG_
#define DEBUG_PRINT(_str,_val) (((_val ? (printf("%s\n",_str)) : 0)))
#else
#define DEBUG_PRINT(_str,_val)
#endif
#define BUFFER_LENGTH 1024
/* function prototypes */
void CharGen_Client(SOCKET sfd);
void Echo_Client(SOCKET sfd);
void Discard_Client(SOCKET sfd);
int CheckParms(int argc, char *argv[]);
void usage(void);
BOOL WINAPI keyboard_handler(DWORD type);
SOCKET g_sfd=0;
/**
* main program for the inetd client, calls appropriate function dependes
* on the server chosen.
**/
void main(int argc,char *argv[])
{
SOCKET socket_fd;
WSADATA wsdata;
DWORD ver_req;
struct sockaddr_in serv_addr;
int ret;
if(3 != argc) {
usage();
exit(100);
}
if(!CheckParms(argc,argv))
exit(101);
ver_req = MAKEWORD( 1, 1);
// initialize windows socket library
ret = WSAStartup(ver_req,&wsdata);
DEBUG_PRINT("WSAStartup failed",ret != 0);
// create a TCP Socket
if ((socket_fd = socket(AF_INET,SOCK_STREAM,0)) == INVALID_SOCKET)
{
DEBUG_PRINT("inetd_client: socket failed",socket_fd == INVALID_SOCKET);
exit(102);
}
memset((char *)&serv_addr,0,sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = inet_addr(argv[1]);
serv_addr.sin_port = htons(atoi(argv[2]));
// connect to the server.
//
if (connect(socket_fd,(struct sockaddr *) &serv_addr, sizeof(serv_addr)) <0)
{
DEBUG_PRINT("inetd_client: connect failed",1);
closesocket(socket_fd);
exit(103);
}
g_sfd = socket_fd;
// install control+c/break handler.
SetConsoleCtrlHandler(keyboard_handler,TRUE);
// switch based on port number
// 19 - chargen server
// 9 - discard server
// 7 - echo server
//
switch(atoi(argv[2]))
{
// chargen client
case 19:
printf("Chargen client connected to %s %d\n",argv[1],atoi(argv[2]));
CharGen_Client(socket_fd);
break;
// echo client
case 7:
printf("Echo client connected to %s %d\n",argv[1],atoi(argv[2]));
Echo_Client(socket_fd);
break;
// Disacard client
case 9:
printf("Discard client connected to %s %d\n",argv[1],atoi(argv[2]));
Discard_Client(socket_fd);
break;
default:
printf("Option unsupported \n");
break;
}
}
/**
*
* Charcter generator client. reads the characters generated by the
* chargen server.
*
**/
void CharGen_Client(SOCKET socket_fd)
{
char buffer[1024];
int num_recv_bytes=0;
// Recive the same string and print to the screen
//
while(1)
{
memset(buffer,0,sizeof(buffer));
num_recv_bytes = recv(socket_fd,buffer,BUFFER_LENGTH,0);
printf("%s",buffer);
}
// close the socket
closesocket(socket_fd);
printf("Chargen Client Exiting \n");
}
/**
*
* Echo server client.Sends the character typed by the user and reads
* back the echo'd output from the server.
*
**/
void Echo_Client(SOCKET socket_fd)
{
char c,t;
int num_recv_bytes=0;
// Recive the same string and print to the screen
//
while(1)
{
c = getc(stdin);
if(c == 0x3) break;
send(socket_fd,&c,1,MSG_DONTROUTE);
num_recv_bytes = recv(socket_fd,&t,1,0);
printf("%c",t);
fflush(stdout);
}
// close the socket
closesocket(socket_fd);
printf("Echo Client Exiting \n");
}
/**
*
* Discard client.Sends the character server discards it.
*
**/
void Discard_Client(SOCKET socket_fd)
{
char c;
// Recive the same string and print to the screen
//
while(1)
{
c = getc(stdin);
if(c == 0x3) break;
send(socket_fd,&c,1,MSG_DONTROUTE);
}
printf("Discard Client Exiting \n");
}
void usage(void)
{
printf("InetdClient <ip_addr> <serv_port>\n");
return;
}
int CheckParms(int argc, char *argv[])
{
int port = atoi(argv[2]);
if(argc >=3)
{
// check if the port is integer.
if(!atoi(argv[2])) {
printf("invalid port\n");
return 0;
}
// check for . in the ip address.
if(!strchr(argv[1],'.')){
printf("invalid ip address\n");
return 0;
}
if( (port != 19) && (port != 9) && (port != 7))
{
printf("Unsupported port for Inetd\n");
return 0;
}
}
return 1;
}
/**
* grabs control+c and control break events
**/
BOOL WINAPI keyboard_handler(DWORD type)
{
switch(type)
{
case CTRL_C_EVENT:
case CTRL_BREAK_EVENT:
if(g_sfd)
closesocket(g_sfd);
printf("Exiting the inetd_client application\n");
break;
case CTRL_SHUTDOWN_EVENT:
case CTRL_LOGOFF_EVENT:
break;
default:
break;
}
return(FALSE); // To call the default handler
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -