Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ## socket.h [c++]
- // a tcp socket class
- #ifndef SOCKET_H
- #define SOCKET_H
- #include <cstring>
- #include <iostream>
- #include <cstdio>
- #include <unistd.h>
- #ifdef __CYGWIN32__
- #include <winsock.h>
- {
- WSADATA ws;
- if (WSAStartup(MAKEWORD(1,1), &ws) != 0)
- {perror("WSAStartup failed.\n");exit(1);}
- }
- void close(int sock){ closesocket(sock);}
- #endif
- #ifdef __unix__
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <netdb.h>
- #endif
- class socket
- {
- public:
- socket(const char*, const char*);
- void listen();
- void connect();
- void send(const void *msg);
- void recv(void* dest, int maxsize);
- ~socket();
- private:
- struct addrinfo filled,*output,*real;
- struct sockaddr_storage incoming;
- int errchk,sock,list_sock,len;
- socklen_t size_storage;
- };
- socket::socket(const char *host,const char *port)
- {
- memset(&filled,0,sizeof filled);
- filled.ai_flags=AI_PASSIVE;
- filled.ai_family=AF_UNSPEC;
- filled.ai_socktype=SOCK_STREAM;
- if(errchk=getaddrinfo(host,port,&filled,&output) != 0)
- {perror("getaddrinfo failed"); exit(-1);}
- for(real=output;real != NULL;real=real->ai_next)
- {
- if(sock=socket(real->ai_family,real->ai_socktype,real->ai_protocol) == -1)
- {perror("linked list entry fail\n"); exit(-1); continue;}
- break;
- }
- }
- void socket::listen()
- {
- int y=1;
- if (setsockopt(sock,SOL_SOCKET,SO_REUSEADDR,&y,sizeof(int)) == -1)
- {perror("setsockopt failed to clearn port--BADD\n");exit(1);}
- if (bind(sock,real->ai_addr,real->ai_addrlen) == -1)
- {
- close(sock);
- perror("bind failed"); exit(1);
- }
- if(errchk=listen(sock,10) == -1)
- {perror("listening on port failed"); exit(1);}
- size_storage=sizeof incoming
- if(list_sock=accept(sock,(struct sockaddr *)&incoming,&size_storage) == -1)
- {perror("accepting connection failed"); exit(1);}
- sock=list_sock;
- close(list_sock);
- }
- void socket::connect()
- {
- if(errchk=connect(sock,real->ai_addr,real->ai_addrlen) == -1)
- {perror("connection to host failed\n");exit(1);}
- }
- void socket::send(const void *msg)
- {
- len=strlen(msg);
- if(errchk=send(sock,msg,len,0) == -1)
- perror("failed to send buffer at all\n");
- if(errchk != len)
- std::cout<<"failed to send all of the buffer, loop plz\n";
- }
- void socket::recv(void* dest, int maxsize)
- {
- if(errchk=recv(sock,dest,maxsize,0) == -1)
- perror("failed to receive data at all\n");
- if(errchk==0)
- std::cout<<"none of the data was received, 0 bytes\n":
- }
- socket::~socket()
- {
- close(sock);
- }
- #endif SOCKET_H
- ## sockettest.cpp [c++]
- //testing socket class
- #include "socket.h"
- #include <iostream>
- int main(void)
- {
- socket newconn("google.com","80");
- newconn.connect();
- char* buf=new char[512];
- char* msg="GET /index.html HTTP/1.1\0x0D\0x0A";
- newconn.send(msg); newconn.recv(buf,512);
- std::cout<<buf<<"\n";
- }
- ## error messages from compiling [plain_text]
- In file included from socket.h:11,
- from socktest.cc:3:
- /usr/lib/gcc/i686-pc-cygwin/3.4.4/../../../../include/w32api/winsock.h:82:2: #warning "fd_set and associated macros have been defined in sys/types. This can cause runtime problems with W32 soc
- In file included from socket.h:11,
- from socktest.cc:3:
- /usr/lib/gcc/i686-pc-cygwin/3.4.4/../../../../include/w32api/winsock.h:480: error: declaration of C function `int gethostname(char*, int)' conflicts with
- /usr/include/sys/unistd.h:206: error: previous declaration `int gethostname(char*, size_t)' here
- In file included from socktest.cc:3:
- socket.h:12: error: expected unqualified-id before '{' token
- socket.h:12: error: expected `,' or `;' before '{' token
- socket.h: In function `void close(int)':
- socket.h:18: error: new declaration `void close(int)'
- /usr/include/sys/unistd.h:29: error: ambiguates old declaration `int close(int)'
- In file included from /usr/include/sys/socket.h:15,
- from socket.h:23,
- from socktest.cc:3:
- /usr/include/cygwin/socket.h: At global scope:
- /usr/include/cygwin/socket.h:29: error: redefinition of `struct sockaddr'
- /usr/lib/gcc/i686-pc-cygwin/3.4.4/../../../../include/w32api/winsock.h:318: error: previous definition of `struct sockaddr'
- In file included from /usr/include/sys/socket.h:15,
- from socket.h:23,
- from socktest.cc:3:
- /usr/include/cygwin/socket.h:59: error: redefinition of `struct linger'
- /usr/lib/gcc/i686-pc-cygwin/3.4.4/../../../../include/w32api/winsock.h:108: error: previous definition of `struct linger'
- In file included from socket.h:23,
- from socktest.cc:3:
- /usr/include/sys/socket.h:29: error: declaration of C function `int accept(int, sockaddr*, socklen_t*)' conflicts with
- /usr/lib/gcc/i686-pc-cygwin/3.4.4/../../../../include/w32api/winsock.h:434: error: previous declaration `SOCKET accept(SOCKET, sockaddr*, int*)' here
- /usr/include/sys/socket.h:30: error: declaration of C function `int bind(int, const sockaddr*, socklen_t)' conflicts with
- /usr/lib/gcc/i686-pc-cygwin/3.4.4/../../../../include/w32api/winsock.h:435: error: previous declaration `int bind(SOCKET, const sockaddr*, int)' here
- /usr/include/sys/socket.h:31: error: declaration of C function `int connect(int, const sockaddr*, socklen_t)' conflicts with
- /usr/lib/gcc/i686-pc-cygwin/3.4.4/../../../../include/w32api/winsock.h:437: error: previous declaration `int connect(SOCKET, const sockaddr*, int)' here
- /usr/include/sys/socket.h:32: error: declaration of C function `int getpeername(int, sockaddr*, socklen_t*)' conflicts with
- /usr/lib/gcc/i686-pc-cygwin/3.4.4/../../../../include/w32api/winsock.h:439: error: previous declaration `int getpeername(SOCKET, sockaddr*, int*)' here
- /usr/include/sys/socket.h:33: error: declaration of C function `int getsockname(int, sockaddr*, socklen_t*)' conflicts with
- /usr/lib/gcc/i686-pc-cygwin/3.4.4/../../../../include/w32api/winsock.h:440: error: previous declaration `int getsockname(SOCKET, sockaddr*, int*)' here
- /usr/include/sys/socket.h:34: error: declaration of C function `int listen(int, int)' conflicts with
- /usr/lib/gcc/i686-pc-cygwin/3.4.4/../../../../include/w32api/winsock.h:444: error: previous declaration `int listen(SOCKET, int)' here
- /usr/include/sys/socket.h:35: error: declaration of C function `int recv(int, void*, size_t, int)' conflicts with
- /usr/lib/gcc/i686-pc-cygwin/3.4.4/../../../../include/w32api/winsock.h:445: error: previous declaration `int recv(SOCKET, char*, int, int)' here
- /usr/include/sys/socket.h:37: error: declaration of C function `int recvfrom(int, void*, size_t, int, sockaddr*, socklen_t*)' conflicts with
- /usr/lib/gcc/i686-pc-cygwin/3.4.4/../../../../include/w32api/winsock.h:446: error: previous declaration `int recvfrom(SOCKET, char*, int, int, sockaddr*, int*)' here
- /usr/include/sys/socket.h:39: error: declaration of C function `int send(int, const void*, size_t, int)' conflicts with
- /usr/lib/gcc/i686-pc-cygwin/3.4.4/../../../../include/w32api/winsock.h:447: error: previous declaration `int send(SOCKET, const char*, int, int)' here
- /usr/include/sys/socket.h:42: error: declaration of C function `int sendto(int, const void*, size_t, int, const sockaddr*, socklen_t)' conflicts with
- /usr/lib/gcc/i686-pc-cygwin/3.4.4/../../../../include/w32api/winsock.h:448: error: previous declaration `int sendto(SOCKET, const char*, int, int, const sockaddr*, int)' here
- /usr/include/sys/socket.h:44: error: declaration of C function `int setsockopt(int, int, int, const void*, socklen_t)' conflicts with
- /usr/lib/gcc/i686-pc-cygwin/3.4.4/../../../../include/w32api/winsock.h:449: error: previous declaration `int setsockopt(SOCKET, int, int, const char*, int)' here
- /usr/include/sys/socket.h:46: error: declaration of C function `int getsockopt(int, int, int, void*, socklen_t*)' conflicts with
- /usr/lib/gcc/i686-pc-cygwin/3.4.4/../../../../include/w32api/winsock.h:441: error: previous declaration `int getsockopt(SOCKET, int, int, char*, int*)' here
- /usr/include/sys/socket.h:47: error: declaration of C function `int shutdown(int, int)' conflicts with
- /usr/lib/gcc/i686-pc-cygwin/3.4.4/../../../../include/w32api/winsock.h:450: error: previous declaration `int shutdown(SOCKET, int)' here
- /usr/include/sys/socket.h:48: error: declaration of C function `int socket(int, int, int)' conflicts with
- /usr/lib/gcc/i686-pc-cygwin/3.4.4/../../../../include/w32api/winsock.h:451: error: previous declaration `SOCKET socket(int, int, int)' here
- In file included from socket.h:24,
- from socktest.cc:3:
- /usr/include/netdb.h:73: error: redefinition of `struct hostent'
- /usr/lib/gcc/i686-pc-cygwin/3.4.4/../../../../include/w32api/winsock.h:100: error: previous definition of `struct hostent'
- /usr/include/netdb.h:87: error: redefinition of `struct netent'
- /usr/lib/gcc/i686-pc-cygwin/3.4.4/../../../../include/w32api/winsock.h:135: error: previous definition of `struct netent'
- /usr/include/netdb.h:94: error: redefinition of `struct servent'
- /usr/lib/gcc/i686-pc-cygwin/3.4.4/../../../../include/w32api/winsock.h:141: error: previous definition of `struct servent'
- /usr/include/netdb.h:102: error: redefinition of `struct protoent'
- /usr/lib/gcc/i686-pc-cygwin/3.4.4/../../../../include/w32api/winsock.h:147: error: previous definition of `struct protoent'
- In file included from socktest.cc:3:
- socket.h:38: error: field `filled' has incomplete type
- socket.h: In constructor `socket::socket(const char*, const char*)':
- socket.h:46: error: `filled' undeclared (first use this function)
- socket.h:46: error: (Each undeclared identifier is reported only once for each function it appears in.)
- socket.h:47: error: `AI_PASSIVE' undeclared (first use this function)
- socket.h:50: error: `getaddrinfo' undeclared (first use this function)
- socket.h:52: error: invalid use of undefined type `struct addrinfo'
- socket.h:38: error: forward declaration of `struct addrinfo'
- socket.h:54: error: invalid use of undefined type `struct addrinfo'
- socket.h:38: error: forward declaration of `struct addrinfo'
- socket.h:54: error: invalid use of undefined type `struct addrinfo'
- socket.h:38: error: forward declaration of `struct addrinfo'
- socket.h:54: error: invalid use of undefined type `struct addrinfo'
- socket.h:38: error: forward declaration of `struct addrinfo'
- socket.h: In member function `void socket::listen()':
- socket.h:65: error: invalid use of undefined type `struct addrinfo'
- socket.h:38: error: forward declaration of `struct addrinfo'
- socket.h:65: error: invalid use of undefined type `struct addrinfo'
- socket.h:38: error: forward declaration of `struct addrinfo'
- socket.h:70: error: no matching function for call to `socket::listen(int&, int)'
- socket.h:61: note: candidates are: void socket::listen()
- socket.h:73: error: expected `;' before "if"
- socket.h: In member function `void socket::connect()':
- socket.h:82: error: invalid use of undefined type `struct addrinfo'
- socket.h:38: error: forward declaration of `struct addrinfo'
- socket.h:82: error: invalid use of undefined type `struct addrinfo'
- socket.h:38: error: forward declaration of `struct addrinfo'
- socket.h: In member function `void socket::send(const void*)':
- socket.h:88: error: invalid conversion from `const void*' to `const char*'
- socket.h:89: error: no matching function for call to `socket::send(int&, const void*&, int&, int)'
- socket.h:87: note: candidates are: void socket::send(const void*)
- socket.h: In member function `void socket::recv(void*, int)':
- socket.h:97: error: no matching function for call to `socket::recv(int&, void*&, int&, int)'
- socket.h:96: note: candidates are: void socket::recv(void*, int)
- socket.h:100: error: expected `;' before ':' token
- In file included from socktest.cc:3:
- socket.h:107:8: extra tokens at end of #endif directive
- socktest.cc: In function `int main()':
- socktest.cc:8: error: expected `;' before "newconn"
- socktest.cc:8: warning: statement is a reference, not call, to function `socket'
- socktest.cc:8: warning: statement has no effect
- socktest.cc:9: error: `newconn' undeclared (first use this function)
Add Comment
Please, Sign In to add comment