標籤:
一個簡單的用戶端擷取伺服器時間的例子:
伺服器代碼:
#include <iostream>#include "unp.h"#include "my_err.h"#define DEFAULT_PORT 8000int main(int argc, const char * argv[]){ int listenfd, connfd; struct sockaddr_in servaddr; char buff[MAXLINE]; time_t ticks; listenfd = Socket(AF_INET, SOCK_STREAM, 0); bzero(&servaddr, sizeof(servaddr)); servaddr.sin_family = AF_INET; servaddr.sin_addr.s_addr = htonl(INADDR_ANY); servaddr.sin_port = htons(DEFAULT_PORT); //連接埠號碼 Bind(listenfd, (SA *) &servaddr, sizeof(servaddr)); Listen(listenfd, LISTENQ); for ( ; ; ){ std::cout << "wait connect" << std::endl; connfd = Accept(listenfd, (SA *) NULL, NULL); ticks = time(NULL); snprintf(buff, sizeof(buff), "%.24s\r\n", ctime(&ticks)); Write(connfd, buff, strlen(buff)); Close(connfd); } std::cout << "Hello, World!\n"; return 0;}
用戶端代碼:
#include <iostream>#include "unp.h"#include "my_err.h"int main(int argc, const char * argv[]){ int sockfd, n; char recvline[MAXLINE + 1]; struct sockaddr_in servaddr;// if (argc != 2)// err_quit("usage: a.out <Ipaddress>"); if ( (sockfd = Socket(AF_INET, SOCK_STREAM, 0)) <0) err_sys("socket error"); bzero(&servaddr, sizeof(servaddr)); servaddr.sin_family = AF_INET; servaddr.sin_port = htons(8000); if (inet_pton(AF_INET, "192.168.1.59", &servaddr.sin_addr) <= 0) //127.0.0.1 err_quit("inet_pton error for %s", argv[1]); if (connect(sockfd, (SA *) &servaddr, sizeof(servaddr)) < 0 ) err_sys("connect error"); while ( (n = read(sockfd, recvline, MAXLINE)) > 0 ) { recvline[n] = 0; if (fputs(recvline , stdout) == EOF) err_sys("fputs error"); } if (n < 0 ) err_sys("read error"); return 0;}
my_err.h 檔案
#ifndef time1_my_err_h#define time1_my_err_h#include <errno.h> /* for definition of errno */#include <stdarg.h> /* ISO C variable aruments */static void err_doit(int, int, const char *, va_list);/* * Nonfatal error related to a system call. * Print a message and return. */voiderr_ret(const char *fmt, ...){ va_list ap; va_start(ap, fmt); err_doit(1, errno, fmt, ap); va_end(ap);}/* * Fatal error related to a system call. * Print a message and terminate. */voiderr_sys(const char *fmt, ...){ va_list ap; va_start(ap, fmt); err_doit(1, errno, fmt, ap); va_end(ap); exit(1);}/* * Fatal error unrelated to a system call. * Error code passed as explict parameter. * Print a message and terminate. */voiderr_exit(int error, const char *fmt, ...){ va_list ap; va_start(ap, fmt); err_doit(1, error, fmt, ap); va_end(ap); exit(1);}/* * Fatal error related to a system call. * Print a message, dump core, and terminate. */voiderr_dump(const char *fmt, ...){ va_list ap; va_start(ap, fmt); err_doit(1, errno, fmt, ap); va_end(ap); abort(); /* dump core and terminate */ exit(1); /* shouldn't get here */}/* * Nonfatal error unrelated to a system call. * Print a message and return. */voiderr_msg(const char *fmt, ...){ va_list ap; va_start(ap, fmt); err_doit(0, 0, fmt, ap); va_end(ap);}/* * Fatal error unrelated to a system call. * Print a message and terminate. */voiderr_quit(const char *fmt, ...){ va_list ap; va_start(ap, fmt); err_doit(0, 0, fmt, ap); va_end(ap); exit(1);}/* * Print a message and return to caller. * Caller specifies "errnoflag". */static voiderr_doit(int errnoflag, int error, const char *fmt, va_list ap){ char buf[MAXLINE]; vsnprintf(buf, MAXLINE, fmt, ap); if (errnoflag) snprintf(buf+strlen(buf), MAXLINE-strlen(buf), ": %s", strerror(error)); strcat(buf, "\n"); fflush(stdout); /* in case stdout and stderr are the same */ fputs(buf, stderr); fflush(NULL); /* flushes all stdio output streams */}intSocket(int family, int type, int protocol){intn; if ( (n = socket(family, type, protocol)) < 0)err_sys("socket error");return(n);}voidBind(int fd, const struct sockaddr *sa, socklen_t salen){if (bind(fd, sa, salen) < 0)err_sys("bind error");}voidClose(int fd){if (close(fd) == -1)err_sys("close error");}voidWrite(int fd, void *ptr, size_t nbytes){if (write(fd, ptr, nbytes) != nbytes)err_sys("write error");}intAccept(int fd, struct sockaddr *sa, socklen_t *salenptr){intn; again:if ( (n = accept(fd, sa, salenptr)) < 0) {#ifdefEPROTOif (errno == EPROTO || errno == ECONNABORTED)#else if (errno == ECONNABORTED)#endif goto again; else err_sys("accept error");}return(n);}voidListen(int fd, int backlog){char*ptr; /*4can override 2nd argument with environment variable */if ( (ptr = getenv("LISTENQ")) != NULL)backlog = atoi(ptr); if (listen(fd, backlog) < 0)err_sys("listen error");}#endif
對比 window socket http://blog.csdn.net/tutuboke/article/details/45799667
linux socket基礎