/*
* Include Files.
*/
/* #define RESOLVE_VIA_LOOKUP */
#pragma runopts(NOSPIE NOSTAE)
#define lim 119
#include <manifest.h>
#include <bsdtypes.h>
#include <in.h>
#include <socket.h>
#include <netdb.h>
#include <stdio.h>
/*
* Client Main.
*/
main(argc, argv)
int argc;
char **argv;
{
unsigned short port; /* port client will connect to */
struct sktmsg
{
short msglen;
short msgrsv;
char msgtrn??(8??);
char msgdat??(lim??);
} msgbuff;
struct datmsg
{
short datlen;
short datrsv;
char datdat??(lim??);
} datbuff;
char buf ??(lim??); /* send receive buffer */
struct hostent *hostnm; /* server host name information */
struct sockaddr_in server; /* server address */
int s; /* client socket */
int len; /* length for send */
/*
* Check Arguments Passed. Should be hostname and port.
*/
if (argc != 3)
{
printf("Invalid parameter count\n");
exit(1);
}
printf("Usage: %s program name\n",argv??(0??));
/*
* The host name is the first argument. Get the server address.
*/
printf("Usage: %s host name\n",argv??(1??));
hostnm = gethostbyname(argv[1]);
if (hostnm == (struct hostent *) 0)
{
printf("Gethostbyname failed\n");
exit(2);
}
/*
* The port is the second argument.
*/
printf("Usage: %s port name\n",argv??(2??));
port = (unsigned short) atoi(argv[2]);
/*
* Put the server information into the server structure.
* The port must be put into network byte order.
*/
server.sin_family = AF_INET;
server.sin_port = htons(port);
server.sin_addr.s_addr = *((unsigned long *)hostnm->h_addr);
/*
* Get a stream socket.
*/
if ((s = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
tcperror("Socket()");
exit(3);
}
/*
* Connect to the server.
*/
if (connect(s, (struct sockaddr *)&server, sizeof(server)) < 0)
{
tcperror("Connect()");
exit(4);
}
/*
* Put a message into the buffer.
*/
msgbuff.msgdat??(0??)='\0';
msgbuff.msgrsv = 0;
msgbuff.msglen = 20;
strncat(msgbuff.msgtrn,"*TRNREQ*",
lim-strlen(msgbuff.msgdat)-1);
strncat(msgbuff.msgdat,"DLSI101 ",
lim-strlen(msgbuff.msgdat)-1);
len=20;
if (send(s, (char *)&msgbuff, len, 0) < 0)
{
tcperror("Send()");
exit(5);
}
printf("\n");
printf(msgbuff.msgdat);
printf("send one complete\n");
/*
* Put a text message into the buffer.
*/
datbuff.datdat??(0??)='\0';
datbuff.datlen = 46;
datbuff.datrsv = 0;
strncat(datbuff.datdat,"THIS IS FIRST TEXT MESSAGE SEND TO SERVER ",
lim-strlen(datbuff.datdat)-1);
len=46;
if (send(s, (char *)&datbuff, len, 0) < 0)
{
tcperror("Send()");
exit(6);
}
printf("\n");
printf(datbuff.datdat);
printf("\n");
printf("send for first text message complete\n");
/*
* Put a text message into the buffer.
*/
datbuff.datdat??(0??)='\0';
datbuff.datlen = 47;
strncat(datbuff.datdat,"THIS IS 2ND TEXT MESSAGE SENDING TO SERVER",
lim-strlen(datbuff.datdat)-1);
len=47;
if (send(s, (char *)&datbuff, len, 0) < 0)
{
tcperror("Send()");
exit(7);
}
printf("\n");
printf(datbuff.datdat);
printf("\n");
printf("send for 2nd test message complete\n");
/*
* Put a end message into the buffer.
*/
datbuff.datdat??(0??)='\0';
datbuff.datlen = 4;
strncpy(datbuff.datdat," ",lim);
len=4;
if (send(s, (char *)&datbuff, len, 0) < 0)
{
tcperror("Send()");
exit(8);
}
printf("\n");
printf(datbuff.datdat);
printf("\n");
printf("send for end message complete\n");
/*
* The server sends back the same message. Receive it into the
* buffer.
*/
strncpy(datbuff.datdat," ",lim);
if (recv(s,(char *)&datbuff, lim, 0) < 0)
{
tcperror("Recv()");
exit(9);
}
printf("receive one text complete\n");
printf(datbuff.datdat);
printf("\n");
/*
* The server sends back the same message. Receive it into the
* buffer.
*/
strncpy(datbuff.datdat," ",lim);
if (recv(s,(char *)&datbuff, lim, 0) < 0)
{
tcperror("Recv()");
exit(10);
}
printf("receive two text complete\n");
printf(datbuff.datdat);
printf("\n");
/*
* The server sends eof message. Receive it into the
* buffer.
*/
strncpy(datbuff.datdat," ",lim);
if (recv(s,(char *)&datbuff, 4, 0) < 0)
{
tcperror("Recv()");
exit(11);
}
printf("receive eof complete\n");
printf("\n");
printf(datbuff.datdat);
printf("\n");
strncpy(datbuff.datdat," ",lim);
if (recv(s,(char *)&datbuff, 12, 0) < 0)
{
tcperror("Recv()");
exit(12);
}
printf("receive CSMOKY complete\n");
printf("\n");
printf(datbuff.datdat);
printf("\n");
/*
* Close the socket.
*/
close(s);
printf("Client Ended Successfully\n");
exit(0);
}
Figure 1. Sample C client to
drive IMS™ Listener