/* daytime.c:
* A sample daytime client C code
* Example daytime client:
*/
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <netinet/in.h>
/*
* This function reports the error and
* exits back to the shell:
*/
static void displayError (const char *on_what) {
fputs (strerror(errno),stderr);
fputs (": ",stderr);
fputs (on_what,stderr);
fputc ('\n',stderr);
exit (1);
}
int main (int argc,char **argv) {
int z;
char *srvr_addr = NULL;
struct sockaddr_in adr_srvr;/* AF_INET */
int len_inet; /* length */
int s; /* Socket */
struct servent *sp; /* Service entry */
char dtbuf[128]; /* Date/Time info */
/*
* Use a server address from the command
* line, if one has been provided.
* Otherwise, this program will default
* to using the arbitrary address
* 127.0.0.:
*/
if ( argc >= 2 ) {
srvr_addr = argv[1]; // Addr on cmdline
}
else {
srvr_addr = "127.0.0.1"; // Use default address
}
/*
* Lookup the daytime tcp service:
*/
sp = getservbyname("daytime","tcp");
if ( !sp ) {
fputs("Unknown service: daytime tcp\n", stderr);
exit(1);
}
/*
* Create a server socket address:
*/
memset(&adr_srvr,0,sizeof adr_srvr);
adr_srvr.sin_family = AF_INET;
adr_srvr.sin_port = sp->s_port;
adr_srvr.sin_addr.s_addr =
inet_addr(srvr_addr);
if ( adr_srvr.sin_addr.s_addr == INADDR_NONE ) {
displayError ("bad address.");
}
len_inet = sizeof adr_srvr;
/*
* Create a TCP/IP socket to use:
*/
s = socket(PF_INET,SOCK_STREAM,0);
if ( s == -1 ) {
displayError("socket()");
}
/*
* Connect to the server:
*/
z = connect(s,&adr_srvr,len_inet);
if ( z == -1 ) {
displayError("connect(2)");
}
/*
* Read the date/time info:
*/
z = read(s,&dtbuf,sizeof dtbuf-1);
if ( z == -1 ) {
displayError("read(2)");
}
/*
* Report the Date & Time:
*/
dtbuf[z] = 0; /* null terminate string */
printf("Date & Time is: %s\n",dtbuf);
/*
* Close the socket and exit:
*/
close(s);
putchar('\n');
return 0;
}
All the readers,
ReplyDeletePlease go through with the "updated daytime client example ".
--Saurabh Gupta
I get Connection timed out: connect(2),even if i run with sudo. Do you have any idea why i get this error?
ReplyDelete