client.cpp
资源名称:client.rar [点击查看]
上传用户:deqinfy
上传日期:2022-06-19
资源大小:2k
文件大小:6k
源码类别:
TCP/IP协议栈
开发平台:
C/C++
- #include <iostream>
- #include <errno.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <arpa/inet.h>
- #include <unistd.h>
- #include <strings.h>
- using namespace std;
- #define MAXLINE 4096
- #define SERV_PORT 9700
- int Accept(int fd, struct sockaddr *sa, socklen_t *salenptr){
- int n;
- again:
- if ( (n = accept(fd, sa, salenptr)) < 0) {
- #ifdef EPROTO
- if (errno == EPROTO || errno == ECONNABORTED)
- #else
- if (errno == ECONNABORTED)
- #endif
- goto again;
- else
- cout<< "accept error";
- }
- return(n);
- }
- void Bind(int fd, const struct sockaddr *sa, socklen_t salen){
- if (bind(fd, sa, salen) < 0)
- cout<< "bind error";
- }
- void Connect(int fd, const struct sockaddr *sa, socklen_t salen){
- if (connect(fd, sa, salen) < 0)
- cout<< "connect error";
- }
- void Getpeername(int fd, struct sockaddr *sa, socklen_t *salenptr){
- if (getpeername(fd, sa, salenptr) < 0)
- cout<<"getpeername error";
- }
- void Getsockname(int fd, struct sockaddr *sa, socklen_t *salenptr){
- if (getsockname(fd, sa, salenptr) < 0)
- cout<< "getsockname error";
- }
- void Getsockopt(int fd, int level, int optname, void *optval, socklen_t *optlenptr){
- if (getsockopt(fd, level, optname, optval, optlenptr) < 0)
- cout<< "getsockopt error";
- }
- /* include Listen */
- void Listen(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)
- cout<< "listen error";
- }
- /* end Listen */
- #ifdef HAVE_POLL
- int Poll(struct pollfd *fdarray, unsigned long nfds, int timeout){
- int n;
- if ( (n = poll(fdarray, nfds, timeout)) < 0)
- cout<< "poll error";
- return(n);
- }
- #endif
- ssize_t Recv(int fd, void *ptr, size_t nbytes, int flags){
- ssize_t n;
- if ( (n = recv(fd, ptr, nbytes, flags)) < 0)
- cout<< "recv error";
- return(n);
- }
- ssize_t Recvfrom(int fd, void *ptr, size_t nbytes, int flags,
- struct sockaddr *sa, socklen_t *salenptr){
- ssize_t n;
- if ( (n = recvfrom(fd, ptr, nbytes, flags, sa, salenptr)) < 0)
- cout<< "recvfrom error";
- return(n);
- }
- ssize_t Recvmsg(int fd, struct msghdr *msg, int flags){
- ssize_t n;
- if ( (n = recvmsg(fd, msg, flags)) < 0)
- cout<< "recvmsg error";
- return(n);
- }
- int Select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds,
- struct timeval *timeout){
- int n;
- if ( (n = select(nfds, readfds, writefds, exceptfds, timeout)) < 0)
- cout<< "select error";
- return(n); /* can return 0 on timeout */
- }
- void Send(int fd, const void *ptr, size_t nbytes, int flags){
- if (send(fd, ptr, nbytes, flags) != nbytes)
- cout<< "send error";
- }
- void Sendto(int fd, const void *ptr, size_t nbytes, int flags,
- const struct sockaddr *sa, socklen_t salen){
- if (sendto(fd, ptr, nbytes, flags, sa, salen) != nbytes)
- cout<< "sendto error";
- }
- void Sendmsg(int fd, const struct msghdr *msg, int flags){
- int i;
- ssize_t nbytes;
- nbytes = 0; /* must first figure out what return value should be */
- for (i = 0; i < msg->msg_iovlen; i++)
- nbytes += msg->msg_iov[i].iov_len;
- if (sendmsg(fd, msg, flags) != nbytes)
- cout<< "sendmsg error";
- }
- void Setsockopt(int fd, int level, int optname, const void *optval, socklen_t optlen){
- if (setsockopt(fd, level, optname, optval, optlen) < 0)
- cout<< "setsockopt error";
- }
- void Shutdown(int fd, int how){
- if (shutdown(fd, how) < 0)
- cout<< "shutdown error";
- }
- /* include Socket */
- int Socket(int family, int type, int protocol){
- int n;
- if ( (n = socket(family, type, protocol)) < 0)
- cout<< "socket error";
- return(n);
- }
- /* end Socket */
- void Socketpair(int family, int type, int protocol, int *fd){
- int n;
- if ( (n = socketpair(family, type, protocol, fd)) < 0)
- cout<< "socketpair error";
- }
- ssize_t writen(int fd, const void *vptr, size_t n){
- size_t nleft;
- ssize_t nwritten;
- const char *ptr;
- ptr = (const char *)vptr;
- nleft = n;
- while (nleft > 0) {
- if ( (nwritten = write(fd, ptr, nleft)) <= 0) {
- if (errno == EINTR)
- nwritten = 0; /* and call write() again */
- else
- return(-1); /* error */
- }
- nleft -= nwritten;
- ptr += nwritten;
- }
- return(n);
- }
- /* end writen */
- void Writen(int fd, void *ptr, size_t nbytes){
- if (writen(fd, ptr, nbytes) != nbytes)
- cout <<"writen error";
- }
- static ssize_t my_read(int fd, char *ptr){
- static int read_cnt = 0;
- static char *read_ptr;
- static char read_buf[MAXLINE];
- if (read_cnt <= 0) {
- again:
- if ( (read_cnt = read(fd, read_buf, sizeof(read_buf))) < 0) {
- if (errno == EINTR)
- goto again;
- return(-1);
- } else if (read_cnt == 0)
- return(0);
- read_ptr = read_buf;
- }
- read_cnt--;
- *ptr = *read_ptr++;
- return(1);
- }
- ssize_t readline(int fd, void *vptr, size_t maxlen){
- int n, rc;
- char c, *ptr;
- ptr = (char *)vptr;
- for (n = 1; n < maxlen; n++) {
- if ( (rc = my_read(fd, &c)) == 1) {
- *ptr++ = c;
- if (c == 'n')
- break; /* newline is stored, like fgets() */
- } else if (rc == 0) {
- if (n == 1)
- return(0); /* EOF, no data read */
- else
- break; /* EOF, some data was read */
- } else
- return(-1); /* error, errno set by read() */
- }
- *ptr = 0; /* null terminate like fgets() */
- return(n);
- }
- /* end readline */
- ssize_t Readline(int fd, void *ptr, size_t maxlen){
- ssize_t n;
- if ( (n = readline(fd, ptr, maxlen)) < 0)
- cout <<"readline error";
- return(n);
- }
- void str_cli(FILE *fp, int sockfd){
- char sendline[MAXLINE], recvline[MAXLINE];
- fgets(sendline, MAXLINE, fp);
- Writen(sockfd, sendline, strlen(sendline));
- sendline[strlen(sendline)-1] = ' ';
- cout << "Sending Command: '" << sendline << "' to servern";
- if (Readline(sockfd, recvline, MAXLINE) == 0)
- cout << "str_cli: server terminated prematurely";
- for(int i=0;i<strlen(recvline);i++)
- if(recvline[i] == (char)(136))
- recvline[i] = 'n';
- fputs(recvline, stdout);
- }
- int main(int argc, char **argv){
- int sockfd;
- struct sockaddr_in servaddr;
- if (argc != 2)
- cout << "usage: tcpcli <IPaddress>";
- cout << "Please enter the Command to be sent to server :: ";
- sockfd = Socket(AF_INET, SOCK_STREAM, 0);
- bzero(&servaddr, sizeof(servaddr));
- servaddr.sin_family = AF_INET;
- servaddr.sin_port = htons(SERV_PORT);
- inet_pton(AF_INET, argv[1], &servaddr.sin_addr);
- Connect(sockfd, (const struct sockaddr *) &servaddr, sizeof(servaddr));
- str_cli(stdin, sockfd); /* do it all */
- exit(0);
- }