#include
#include
#include
#include
#include
#include
#include
#include
#define PORT 5524
#define PROTOCOL "tcp"
#define HOST_NAME "nw0546"
int Connect()
{
int SocketNumber; /* Socket descripter */
struct hostent *HostEntry; /* host entry */
struct sockaddr_in sin; /* Socket Entry */
/* Get Host Entry by Host-Name */
if((struct hostent *)NULL == (HostEntry = gethostbyname(HOST_NAME)))
{
printf("\7No Hosts [%s].\n",HOST_NAME);
exit(1);
}
/* Get Socket */
if(0 > (SocketNumber = (socket(PF_INET,SOCK_STREAM,0))))
{
printf("\7Cannot get Socket.\n");
exit(1);
}
bzero((char *)(&sin), sizeof(sin));
bcopy(HostEntry->h_addr,&sin.sin_addr,HostEntry->h_length);
sin.sin_family = PF_INET;
sin.sin_port = htons(PORT);
if(0 > connect(SocketNumber,(struct sockaddr *)(&sin),sizeof(sin)))
{
printf("\7Cannot Connect.\n");
exit(1);
}
return SocketNumber;
}
void Disconnect(int SocketNumber)
{
close(SocketNumber);
}
void SendData(int SocketNumber,char *line)
{
if(strlen(line) != write(SocketNumber,line,strlen(line)))
{
printf("\7Send Failed.\n");
exit(1);
}
}
int RecvData(int SocketNumber,char *line)
{
int i;
for(i = 0 ;;)
{
if(1 != read(SocketNumber,line + i,1))
continue;
else
{
i++;
*(line + i) = (char)0;
if(((char)('\n')) == *(line + i - 1))
return i;
}
}
}
int main()
{
int Sock;
char Key[256];
char Data[256];
char buff[256];
Sock = Connect(); /* Connect */
printf("Connected.\n");
for(;;)
{
char *p;
printf("Input Keyword = ");
fflush(stdout);
gets(Key);
sprintf(buff,"%s=\n",Key);
SendData(Sock,buff); /* Send It ! */
if(0 == strlen(Key))
break; /* If End Mark */
/* ---- Waiting for Server response Here --- */
(void)RecvData(Sock,buff); /* Recv It ! */
if((char *)NULL != (p = strchr(buff,'\n')))
*p = (char)0;
if((char *)NULL != (p = strchr(buff,'=')))
p++;
printf("Keyword = [%s] / Data = [%s]\n\n",Key,p);
}
Disconnect(Sock);
printf("Disocnnected.\n");
}
|