/*
server1.c
This is a sample for a server code using socket API.
(c) M. Okada
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "INExp.h"
#define BLEN 100
#define PORT 1201
int soc;
char buf[BLEN];
int main(void){
int i;
char init[BLEN] = "";
for( i = 0 ; i < BLEN ; ++i ){ strcat( init, "¥0" ); }
if(( soc=setup_server(PORT))==-1){
exit( EXIT_FAILURE );
}
printf("SAMPLE Server Start.¥n");
strcpy(buf,"This is Server1.¥n");
write(soc,buf,strlen(buf));
strncpy(buf,init,BLEN);
while(1){
read(soc, buf, BLEN);
printf("%s %d characters¥n",buf,strlen(buf)-2); /* -2 、マ /r/n */
if(strncmp(buf,"¥¥end¥r¥n",6)==0
|| strncmp(buf,"¥¥END¥r¥n",6)==0){
break;
}
write(soc, buf, strlen(buf));
strncpy(buf,init,BLEN);
}
strcpy(buf,"Bye bye!!¥n");
write(soc,buf,strlen(buf));
close_server(soc);
printf("SAMPLE Server Terminated.¥n");
return EXIT_SUCCESS;
}