00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016 #ifndef __MXSOCKET_H__D
00017 #define __MXSOCKET_H__D
00018
00019
00020 #include<iostream>
00021 #include<string>
00022
00023
00024 #ifndef _WIN32
00025 #include <sys/types.h>
00026 #include <sys/stat.h>
00027 #include <sys/types.h>
00028 #include <sys/socket.h>
00029 #include <netinet/in.h>
00030 #include <arpa/inet.h>
00031 #include<unistd.h>
00032
00033 typedef int mx_socket;
00034
00035 #else
00036
00037 #include<winsock.h>
00038
00039 typedef SOCKET mx_socket;
00040
00041 #endif
00042
00043 #include"mx_exception.h"
00044
00045
00046 namespace mx
00047 {
00048
00051 class mxSocket {
00052
00053 public:
00055 mxSocket();
00057 ~mxSocket();
00061 mxSocket(mx_socket nsock);
00065 mxSocket(const mxSocket &nsock);
00070 mxSocket &operator=(const mxSocket &nsock);
00071
00076 bool createSocket();
00077
00081 bool isConnected() { return connected; }
00087 bool connectTo(std::string ip, unsigned int port);
00088
00093 bool listenAt(int port);
00094
00099 mx_socket acceptNewSocket(std::string &ip);
00103 mx_socket acceptsocket();
00104
00109 ssize_t Read(void *data, size_t size);
00114 ssize_t Write(void *data, size_t size);
00115
00117 const int getsocket() const { return current_socket; }
00119 int closeSocket();
00120 private:
00121 mx_socket current_socket;
00122 bool connected;
00123 };
00124
00125
00126 #ifdef _WIN32
00127
00128 struct InitWinsock32 {
00129
00130 public:
00131
00132 InitWinsock32()
00133 {
00134 WSADATA socka;
00135 WSAStartup(MAKEWORD(2,2), &socka);
00136
00137 }
00138
00139 ~InitWinsock32()
00140 {
00141 WSACleanup();
00142 }
00143 };
00144
00145 static InitWinsock32 socket_init_;
00146 #endif
00147
00148 }
00149
00150 #endif
00151
00152