home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
OS/2 Shareware BBS: 10 Tools
/
10-Tools.zip
/
tolkit45.zip
/
os2tk45
/
samples
/
tcpiptk
/
echosamp
/
echoclnt
/
echoc1st.c
< prev
next >
Wrap
Text File
|
1999-05-11
|
8KB
|
247 lines
/********************************************************copyrite.xic********/
/* */
/* Licensed Materials - Property of IBM */
/* IBM TCP/IP for OS/2 */
/* (C) Copyright IBM Corporation. 1996. */
/* */
/* All rights reserved. */
/* */
/* US Government Users Restricted Rights - */
/* Use, duplication or disclosure restricted by GSA ADP Schedule */
/* Contract with IBM Corp. */
/* */
/*--------------------------------------------------------------------------*/
/* */
/* DISCLAIMER OF WARRANTIES. The following [enclosed] code is */
/* sample code created by IBM Corporation. This sample code is not */
/* part of any standard or IBM product and is provided to you solely */
/* for the purpose of assisting you in the development of your */
/* applications. The code is provided "AS IS", without */
/* warranty of any kind. IBM shall not be liable for any damages */
/* arising out of your use of the sample code, even if they have been */
/* advised of the possibility of such damages. */
/* */
/*--------------------------------------------------------------------------*/
/*****************************************************************************
*
* ECHO client
* -----------
*
* Written by Andre Asselin
*
*
* Description
* -----------
*
* This program implements the client side of the Echo protocol (RFC 862). The
* Echo protocol is very simple: whatever the client sends to the server, the
* server will send right back to the client. The complete RFC is reproduced
* below:
*
Network Working Group J. Postel
Request for Comments: 862 ISI
May 1983
Echo Protocol
This RFC specifies a standard for the ARPA Internet community. Hosts on
the ARPA Internet that choose to implement an Echo Protocol are expected
to adopt and implement this standard.
A very useful debugging and measurement tool is an echo service. An
echo service simply sends back to the originating source any data it
receives.
TCP Based Echo Service
One echo service is defined as a connection based application on TCP.
A server listens for TCP connections on TCP port 7. Once a
connection is established any data received is sent back. This
continues until the calling user terminates the connection.
UDP Based Echo Service
Another echo service is defined as a datagram based application on
UDP. A server listens for UDP datagrams on UDP port 7. When a
datagram is received, the data from it is sent back in an answering
datagram.
*
******************************************************************************/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <netdb.h>
#include <arpa\inet.h>
void TCPClient(struct sockaddr_in *server);
void UDPClient(struct sockaddr_in *server);
void main(int argc, char **argv) {
struct sockaddr_in server;
struct hostent *hp;
// Initialize TCP/IP
if (sock_init() != 0) {
printf("INET.SYS probably is not running");
exit(1);
}
// Check to make sure the user passed the right number of arguments
if (argc != 3 || argc > 1 && argv[1][0] == '?') {
printf("Usage:\n"
"──echoclnt──┬─ TCP ─┬─<host>──\n"
" └─ UDP ─┘\n"
"\n"
"TCP Specifies to use TCP as the connection protocol.\n"
"UDP Specifies to use UDP as the connection protocol.\n"
"<host> Specifies the remote host on which the command is to be issued.\n");
exit(1);
}
// Set the server address from argument 2 -- we accept both domain names
// and IP addresses
server.sin_family = AF_INET;
server.sin_addr.s_addr = inet_addr(argv[2]);
if (server.sin_addr.s_addr == -1) {
// It wasn't a dotted decimal address -- assume it's a domain name,
// and use gethostbyname() to convert it
hp = gethostbyname(argv[2]);
if (hp) {
server.sin_family = hp->h_addrtype;
memcpy(&server.sin_addr, hp->h_addr, sizeof(server.sin_addr));
} else {
printf("Unknown host %s\n", argv[2]);
exit(1);
}
}
// Call the correct client routine
if (stricmp(argv[1], "TCP") == 0) {
TCPClient(&server);
} else if (stricmp(argv[1], "UDP") == 0) {
UDPClient(&server);
} else {
printf("You must specify either 'tcp' or 'udp' as the connection protocol.\n");
exit(1);
}
}
void TCPClient(struct sockaddr_in *server) {
char buf[100];
struct servent *echoprot;
int sock, len, rc;
// Open a TCP socket
sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock == -1) {
psock_errno("socket()");
exit(1);
}
// Get the port for the echo protocol out of the etc\services file
echoprot = getservbyname("echo", "tcp");
if (echoprot == NULL) {
printf("The echo/tcp protocol is not listed in the etc/services file\n");
exit(1);
}
server->sin_port = echoprot->s_port;
// Connect to the server
rc = connect(sock, (struct sockaddr *)server, sizeof(*server));
if (rc == -1) {
psock_errno("connect()");
exit(1);
}
while (1) {
if (gets(buf) == NULL) {
printf("gets() returned NULL\n");
exit(1);
}
rc = send(sock, buf, strlen(buf), 0);
if (rc == -1) {
psock_errno("send()");
exit(1);
}
len = recv(sock, buf, sizeof(buf), 0);
if (len == -1) {
psock_errno("recv()");
exit(1);
}
buf[len] = '\0';
printf("%s\n", buf);
}
}
void UDPClient(struct sockaddr_in *server) {
struct sockaddr_in response;
char buf[100];
struct servent *echoprot;
int sock, len, rc, respsize;
// Open a UDP socket
sock = socket(AF_INET, SOCK_DGRAM, 0);
if (sock == -1) {
psock_errno("socket()");
exit(1);
}
// Get the port for the echo protocol out of the etc\services file
echoprot = getservbyname("echo", "udp");
if (echoprot == NULL) {
printf("The echo/udp protocol is not listed in the etc/services file\n");
exit(1);
}
server->sin_port = echoprot->s_port;
while (1) {
if (gets(buf) == NULL) {
printf("gets() returned NULL\n");
exit(1);
}
rc = sendto(sock, buf, strlen(buf), 0, (struct sockaddr *)server, sizeof(*server));
if (rc == -1) {
psock_errno("send()");
exit(1);
}
respsize = sizeof(response);
len = recvfrom(sock, buf, sizeof(buf), 0, (struct sockaddr *)&response, &respsize);
if (len == -1) {
psock_errno("recv()");
exit(1);
}
buf[len] = '\0';
printf("%s\n", buf);
}
}