Guest User

Untitled

a guest
Jun 21st, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.82 KB | None | 0 0
  1. ## socket.h [c++]
  2. // a tcp socket class
  3. #ifndef SOCKET_H
  4. #define SOCKET_H
  5.  
  6. #include <cstring>
  7. #include <iostream>
  8. #include <cstdio>
  9. #include <unistd.h>
  10.  
  11. #ifdef __CYGWIN32__
  12. #include <winsock.h>
  13. {
  14. WSADATA ws;
  15. if (WSAStartup(MAKEWORD(1,1), &ws) != 0)
  16. {perror("WSAStartup failed.\n");exit(1);}
  17. }
  18.  
  19. void close(int sock){ closesocket(sock);}
  20. #endif
  21.  
  22. #ifdef __unix__
  23. #include <sys/types.h>
  24. #include <sys/socket.h>
  25. #include <netdb.h>
  26. #endif
  27.  
  28.  
  29. class socket
  30. {
  31. public:
  32. socket(const char*, const char*);
  33. void listen();
  34. void connect();
  35. void send(const void *msg);
  36. void recv(void* dest, int maxsize);
  37. ~socket();
  38. private:
  39. struct addrinfo filled,*output,*real;
  40. struct sockaddr_storage incoming;
  41. int errchk,sock,list_sock,len;
  42. socklen_t size_storage;
  43. };
  44.  
  45. socket::socket(const char *host,const char *port)
  46. {
  47. memset(&filled,0,sizeof filled);
  48. filled.ai_flags=AI_PASSIVE;
  49. filled.ai_family=AF_UNSPEC;
  50. filled.ai_socktype=SOCK_STREAM;
  51. if(errchk=getaddrinfo(host,port,&filled,&output) != 0)
  52. {perror("getaddrinfo failed"); exit(-1);}
  53. for(real=output;real != NULL;real=real->ai_next)
  54. {
  55. if(sock=socket(real->ai_family,real->ai_socktype,real->ai_protocol) == -1)
  56. {perror("linked list entry fail\n"); exit(-1); continue;}
  57. break;
  58. }
  59. }
  60.  
  61. void socket::listen()
  62. {
  63. int y=1;
  64. if (setsockopt(sock,SOL_SOCKET,SO_REUSEADDR,&y,sizeof(int)) == -1)
  65. {perror("setsockopt failed to clearn port--BADD\n");exit(1);}
  66. if (bind(sock,real->ai_addr,real->ai_addrlen) == -1)
  67. {
  68. close(sock);
  69. perror("bind failed"); exit(1);
  70. }
  71. if(errchk=listen(sock,10) == -1)
  72. {perror("listening on port failed"); exit(1);}
  73. size_storage=sizeof incoming
  74. if(list_sock=accept(sock,(struct sockaddr *)&incoming,&size_storage) == -1)
  75. {perror("accepting connection failed"); exit(1);}
  76. sock=list_sock;
  77. close(list_sock);
  78. }
  79.  
  80.  
  81. void socket::connect()
  82. {
  83. if(errchk=connect(sock,real->ai_addr,real->ai_addrlen) == -1)
  84. {perror("connection to host failed\n");exit(1);}
  85. }
  86.  
  87. void socket::send(const void *msg)
  88. {
  89. len=strlen(msg);
  90. if(errchk=send(sock,msg,len,0) == -1)
  91. perror("failed to send buffer at all\n");
  92. if(errchk != len)
  93. std::cout<<"failed to send all of the buffer, loop plz\n";
  94. }
  95.  
  96. void socket::recv(void* dest, int maxsize)
  97. {
  98. if(errchk=recv(sock,dest,maxsize,0) == -1)
  99. perror("failed to receive data at all\n");
  100. if(errchk==0)
  101. std::cout<<"none of the data was received, 0 bytes\n":
  102. }
  103.  
  104. socket::~socket()
  105. {
  106. close(sock);
  107. }
  108. #endif SOCKET_H
  109.  
  110.  
  111.  
  112. ## sockettest.cpp [c++]
  113. //testing socket class
  114.  
  115. #include "socket.h"
  116. #include <iostream>
  117.  
  118. int main(void)
  119. {
  120. socket newconn("google.com","80");
  121. newconn.connect();
  122. char* buf=new char[512];
  123. char* msg="GET /index.html HTTP/1.1\0x0D\0x0A";
  124. newconn.send(msg); newconn.recv(buf,512);
  125. std::cout<<buf<<"\n";
  126. }
  127.  
  128. ## error messages from compiling [plain_text]
  129.  
  130. In file included from socket.h:11,
  131. from socktest.cc:3:
  132. /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
  133. In file included from socket.h:11,
  134. from socktest.cc:3:
  135. /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
  136. /usr/include/sys/unistd.h:206: error: previous declaration `int gethostname(char*, size_t)' here
  137. In file included from socktest.cc:3:
  138. socket.h:12: error: expected unqualified-id before '{' token
  139. socket.h:12: error: expected `,' or `;' before '{' token
  140. socket.h: In function `void close(int)':
  141. socket.h:18: error: new declaration `void close(int)'
  142. /usr/include/sys/unistd.h:29: error: ambiguates old declaration `int close(int)'
  143. In file included from /usr/include/sys/socket.h:15,
  144. from socket.h:23,
  145. from socktest.cc:3:
  146. /usr/include/cygwin/socket.h: At global scope:
  147. /usr/include/cygwin/socket.h:29: error: redefinition of `struct sockaddr'
  148. /usr/lib/gcc/i686-pc-cygwin/3.4.4/../../../../include/w32api/winsock.h:318: error: previous definition of `struct sockaddr'
  149. In file included from /usr/include/sys/socket.h:15,
  150. from socket.h:23,
  151. from socktest.cc:3:
  152. /usr/include/cygwin/socket.h:59: error: redefinition of `struct linger'
  153. /usr/lib/gcc/i686-pc-cygwin/3.4.4/../../../../include/w32api/winsock.h:108: error: previous definition of `struct linger'
  154. In file included from socket.h:23,
  155. from socktest.cc:3:
  156. /usr/include/sys/socket.h:29: error: declaration of C function `int accept(int, sockaddr*, socklen_t*)' conflicts with
  157. /usr/lib/gcc/i686-pc-cygwin/3.4.4/../../../../include/w32api/winsock.h:434: error: previous declaration `SOCKET accept(SOCKET, sockaddr*, int*)' here
  158. /usr/include/sys/socket.h:30: error: declaration of C function `int bind(int, const sockaddr*, socklen_t)' conflicts with
  159. /usr/lib/gcc/i686-pc-cygwin/3.4.4/../../../../include/w32api/winsock.h:435: error: previous declaration `int bind(SOCKET, const sockaddr*, int)' here
  160. /usr/include/sys/socket.h:31: error: declaration of C function `int connect(int, const sockaddr*, socklen_t)' conflicts with
  161. /usr/lib/gcc/i686-pc-cygwin/3.4.4/../../../../include/w32api/winsock.h:437: error: previous declaration `int connect(SOCKET, const sockaddr*, int)' here
  162. /usr/include/sys/socket.h:32: error: declaration of C function `int getpeername(int, sockaddr*, socklen_t*)' conflicts with
  163. /usr/lib/gcc/i686-pc-cygwin/3.4.4/../../../../include/w32api/winsock.h:439: error: previous declaration `int getpeername(SOCKET, sockaddr*, int*)' here
  164. /usr/include/sys/socket.h:33: error: declaration of C function `int getsockname(int, sockaddr*, socklen_t*)' conflicts with
  165. /usr/lib/gcc/i686-pc-cygwin/3.4.4/../../../../include/w32api/winsock.h:440: error: previous declaration `int getsockname(SOCKET, sockaddr*, int*)' here
  166. /usr/include/sys/socket.h:34: error: declaration of C function `int listen(int, int)' conflicts with
  167. /usr/lib/gcc/i686-pc-cygwin/3.4.4/../../../../include/w32api/winsock.h:444: error: previous declaration `int listen(SOCKET, int)' here
  168. /usr/include/sys/socket.h:35: error: declaration of C function `int recv(int, void*, size_t, int)' conflicts with
  169. /usr/lib/gcc/i686-pc-cygwin/3.4.4/../../../../include/w32api/winsock.h:445: error: previous declaration `int recv(SOCKET, char*, int, int)' here
  170. /usr/include/sys/socket.h:37: error: declaration of C function `int recvfrom(int, void*, size_t, int, sockaddr*, socklen_t*)' conflicts with
  171. /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
  172. /usr/include/sys/socket.h:39: error: declaration of C function `int send(int, const void*, size_t, int)' conflicts with
  173. /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
  174. /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
  175. /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
  176. /usr/include/sys/socket.h:44: error: declaration of C function `int setsockopt(int, int, int, const void*, socklen_t)' conflicts with
  177. /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
  178. /usr/include/sys/socket.h:46: error: declaration of C function `int getsockopt(int, int, int, void*, socklen_t*)' conflicts with
  179. /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
  180. /usr/include/sys/socket.h:47: error: declaration of C function `int shutdown(int, int)' conflicts with
  181. /usr/lib/gcc/i686-pc-cygwin/3.4.4/../../../../include/w32api/winsock.h:450: error: previous declaration `int shutdown(SOCKET, int)' here
  182. /usr/include/sys/socket.h:48: error: declaration of C function `int socket(int, int, int)' conflicts with
  183. /usr/lib/gcc/i686-pc-cygwin/3.4.4/../../../../include/w32api/winsock.h:451: error: previous declaration `SOCKET socket(int, int, int)' here
  184. In file included from socket.h:24,
  185. from socktest.cc:3:
  186. /usr/include/netdb.h:73: error: redefinition of `struct hostent'
  187. /usr/lib/gcc/i686-pc-cygwin/3.4.4/../../../../include/w32api/winsock.h:100: error: previous definition of `struct hostent'
  188. /usr/include/netdb.h:87: error: redefinition of `struct netent'
  189. /usr/lib/gcc/i686-pc-cygwin/3.4.4/../../../../include/w32api/winsock.h:135: error: previous definition of `struct netent'
  190. /usr/include/netdb.h:94: error: redefinition of `struct servent'
  191. /usr/lib/gcc/i686-pc-cygwin/3.4.4/../../../../include/w32api/winsock.h:141: error: previous definition of `struct servent'
  192. /usr/include/netdb.h:102: error: redefinition of `struct protoent'
  193. /usr/lib/gcc/i686-pc-cygwin/3.4.4/../../../../include/w32api/winsock.h:147: error: previous definition of `struct protoent'
  194. In file included from socktest.cc:3:
  195. socket.h:38: error: field `filled' has incomplete type
  196. socket.h: In constructor `socket::socket(const char*, const char*)':
  197. socket.h:46: error: `filled' undeclared (first use this function)
  198. socket.h:46: error: (Each undeclared identifier is reported only once for each function it appears in.)
  199. socket.h:47: error: `AI_PASSIVE' undeclared (first use this function)
  200. socket.h:50: error: `getaddrinfo' undeclared (first use this function)
  201. socket.h:52: error: invalid use of undefined type `struct addrinfo'
  202. socket.h:38: error: forward declaration of `struct addrinfo'
  203. socket.h:54: error: invalid use of undefined type `struct addrinfo'
  204. socket.h:38: error: forward declaration of `struct addrinfo'
  205. socket.h:54: error: invalid use of undefined type `struct addrinfo'
  206. socket.h:38: error: forward declaration of `struct addrinfo'
  207. socket.h:54: error: invalid use of undefined type `struct addrinfo'
  208. socket.h:38: error: forward declaration of `struct addrinfo'
  209. socket.h: In member function `void socket::listen()':
  210. socket.h:65: error: invalid use of undefined type `struct addrinfo'
  211. socket.h:38: error: forward declaration of `struct addrinfo'
  212. socket.h:65: error: invalid use of undefined type `struct addrinfo'
  213. socket.h:38: error: forward declaration of `struct addrinfo'
  214. socket.h:70: error: no matching function for call to `socket::listen(int&, int)'
  215. socket.h:61: note: candidates are: void socket::listen()
  216. socket.h:73: error: expected `;' before "if"
  217. socket.h: In member function `void socket::connect()':
  218. socket.h:82: error: invalid use of undefined type `struct addrinfo'
  219. socket.h:38: error: forward declaration of `struct addrinfo'
  220. socket.h:82: error: invalid use of undefined type `struct addrinfo'
  221. socket.h:38: error: forward declaration of `struct addrinfo'
  222. socket.h: In member function `void socket::send(const void*)':
  223. socket.h:88: error: invalid conversion from `const void*' to `const char*'
  224. socket.h:89: error: no matching function for call to `socket::send(int&, const void*&, int&, int)'
  225. socket.h:87: note: candidates are: void socket::send(const void*)
  226. socket.h: In member function `void socket::recv(void*, int)':
  227. socket.h:97: error: no matching function for call to `socket::recv(int&, void*&, int&, int)'
  228. socket.h:96: note: candidates are: void socket::recv(void*, int)
  229. socket.h:100: error: expected `;' before ':' token
  230. In file included from socktest.cc:3:
  231. socket.h:107:8: extra tokens at end of #endif directive
  232. socktest.cc: In function `int main()':
  233. socktest.cc:8: error: expected `;' before "newconn"
  234. socktest.cc:8: warning: statement is a reference, not call, to function `socket'
  235. socktest.cc:8: warning: statement has no effect
  236. socktest.cc:9: error: `newconn' undeclared (first use this function)
Add Comment
Please, Sign In to add comment