Hi,everyone,I want to learn the socket programming these days
and practice programming on HP-UX server.
I have no any idea about network programming and the knowledge of
socket.
I just write the code according the book "Advanced Unix programming"
with the socket server code like this:
####################socketserver.c###############################
#include <netdb.h>
#include <errno.h>
#include <syslog.h>
#include <sys/socket.h>
#define BUFLEN 128
#define QLEN 10
#ifndef HOST_NAME_MAX
#define HOST_NAME_MAX 256
#endif
int initserver(int, struct sockaddr *, socklen_t, int);
void err_sys(const char *s)
{
perror(s);
exit(-1);
}
void
serve(int sockfd)
{
int clfd;
FILE *fp;
char buf[BUFLEN];
for (;;) {
clfd = accept(sockfd, NULL, NULL);
if (clfd < 0) {
/*syslog(LOG_ERR, "ruptimed: accept error: %s",
strerror(errno));*/
printf("ruptimed: accept error: %s",strerror(errno));
exit(1);
}
if ((fp = popen("/usr/bin/uptime", "r")) == NULL) {
sprintf(buf, "error: %s\n", strerror(errno));
send(clfd, buf, strlen(buf), 0);
} else {
while (fgets(buf, BUFLEN, fp) != NULL)
send(clfd, buf, strlen(buf), 0);
pclose(fp);
}
close(clfd);
}
}
int
main(int argc, char *argv[])
{
printf("begin to run on server!");
struct addrinfo *ailist, *aip;
struct addrinfo hint;
int sockfd, err, n;
char *host;
char *hostip = "10.131.3.71";
if (argc != 1)
err_sys("usage: ruptimed");
#ifdef _SC_HOST_NAME_MAX
printf("the _SC_HOST_NAME_MAX is defined");
n = sysconf(_SC_HOST_NAME_MAX);
if (n < 0) /* best guess */
#endif
n = HOST_NAME_MAX;
host = malloc(n);
if (host == NULL)
{
perror("malloc error");
exit(-1);
}
//if (gethostname(host, n) < 0)
// err_sys("gethostname error");
//printf("hostname:%s",host);
//daemonize("ruptimed");
hint.ai_flags = AI_CANONNAME;
hint.ai_family = 0;
hint.ai_socktype = SOCK_STREAM;
hint.ai_protocol = 0;
hint.ai_addrlen = 0;
hint.ai_canonname = NULL;
hint.ai_addr = NULL;
hint.ai_next = NULL;
if ((err = getaddrinfo(hostip,/* "ruptime"*/NULL, &hint,
&ailist)) != 0) {
/*syslog(LOG_ERR, "ruptimed: getaddrinfo error: %s",
gai_strerror(err));*/
printf("error! getaddrinfo error! %s",gai_strerror(err));
exit(1);
}
for (aip = ailist; aip != NULL; aip = aip->ai_next) {
if ((sockfd = initserver(SOCK_STREAM, aip->ai_addr,
aip->ai_addrlen, QLEN)) >= 0) {
serve(sockfd);
exit(0);
}
}
exit(1);
}
int
initserver(int type, const struct sockaddr *addr, socklen_t alen,
int qlen)
{
int fd;
int err = 0;
if ((fd = socket(addr->sa_family, type, 0)) < 0)
return(-1);
if (bind(fd, addr, alen) < 0) {
err = errno;
goto errout;
}
if (type == SOCK_STREAM || type == SOCK_SEQPACKET) {
if (listen(fd, qlen) < 0) {
err = errno;
goto errout;
}
}
return(fd);
errout:
close(fd);
errno = err;
return(-1);
}
#################end-
socketserver.c#####################################
I don't want to make the socketserver code become a
daemon process since this topic is out of my control
so I comment the //daemonize("ruptimed");
and all the //syslog...
I just want to take it as a normal programme.
since I found the gethostname(host, n) < 0 become TRUE(
I DON'T KNOW WHY!)
so I have to choose a static Ip address of the HP-UX
hostip instead of hostname.
when I use getaddrinfo() function,the second parameter
confuse me,I don't know what is service name stand for
so I change to NULL.
Then, I compile it and run it:
++++++++++
jack>socketserver &
++++++++++
jack>ps -aef | grep -i socketserver
++++++++++
show:
#############
jack 15337 26859 0 09:41:48 pts/tb 0:00 grep -i socketserver
jack 15105 26859 0 09:40:59 pts/tb 0:00 socketserver
#############
it seems quite right
so I begin to wirte the client code,
which is the example for AUP
###############socketclient.c###############################
#include <stdio.h>
#include <unistd.h>
#include <netdb.h>
#include <errno.h>
#include <sys/socket.h>
#define MAXADDRLEN 256
#define MAXSLEEP 28
#define BUFLEN 128
int
connect_retry(int sockfd, const struct sockaddr *addr, socklen_t alen)
{
int nsec;
/*
* Try to connect with exponential backoff.
*/
for (nsec = 1; nsec <= MAXSLEEP; nsec <<= 1) {
if (connect(sockfd, addr, alen) == 0) {
/*
* Connection accepted.
*/
return(0);
}
/*
* Delay before trying again.
*/
if (nsec <= MAXSLEEP/2)
//sleep(nsec);
sleep(1); //wait too long ,change to sleep 1 second
}
return(-1);
}
void
print_uptime(int sockfd)
{
int n;
char buf[BUFLEN];
while ((n = recv(sockfd, buf, BUFLEN, 0)) > 0)
write(1, buf, n);
if (n < 0)
{
perror("recv error");
//err_sys("recv error");
exit(-1);
}
}
int
main(int argc, char *argv[])
{
struct addrinfo *ailist, *aip;
struct addrinfo hint;
int sockfd, err;
/*
if (argc != 2)
{
perror("usage: ruptime hostname");
exit(-1);
}
*/
char *farhost = "10.131.3.71";
hint.ai_flags = 0;
hint.ai_family = 0;
hint.ai_socktype = SOCK_STREAM;
hint.ai_protocol = 0;
hint.ai_addrlen = 0;
hint.ai_canonname = NULL;
hint.ai_addr = NULL;
hint.ai_next = NULL;
if ((err = getaddrinfo(/*argv[1]*/farhost,/* "ruptime"*/NULL,
&hint, &ailist)) != 0)
{
/* fprintf(STDOUT_FILENO,"getaddrinfo error: %s", strerror
(err));*/
exit(-1);
}
for (aip = ailist; aip != NULL; aip = aip->ai_next) {
if ((sockfd = socket(aip->ai_family, SOCK_STREAM, 0)) < 0)
{
err = errno;
perror("errno here1 ");
}
if (connect_retry(sockfd, aip->ai_addr, aip->ai_addrlen) < 0)
{
err = errno;
perror("connect_retry!");
} else {
print_uptime(sockfd);
exit(0);
}
}
fprintf(stderr, "can't connect to %s: %s\n", farhost,
strerror(err));
exit(1);
}
############################################################
I compile it and run client code on the same machine
but it doesn't work!
The code trying connet the socket several times
and failed with error:
+++++++++++++++++++err-massage+++++++++++++++++++++++++++++++++++++
connect_retry!: Socket is already connected
can't connect to 10.131.3.71: Socket is already connected
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
What's the problem? Any Idea?
|