Go to the documentation of this file.00001 #include "DTSpyHelper.h"
00002 #include <errno.h>
00003 #include <sys/time.h>
00004 #include <sys/types.h>
00005 #include <unistd.h>
00006 #include <sys/socket.h>
00007 #include <netinet/in.h>
00008 #include <arpa/inet.h>
00009 #include <string.h>
00010
00011 #ifdef __wasAPPLE__
00012 typedef int socklen_t;
00013 #endif
00014
00015 DTCtcp::DTCtcp()
00016 {
00017 DTCtcp(0);
00018 }
00019
00020 DTCtcp::DTCtcp(int localport)
00021 {
00022
00023
00024 connected=false;
00025
00026 printf("zeroing...\n");
00027 bzero ((char *) &myaddr, sizeof(myaddr));
00028 printf("zeroing done..\n");
00029
00030
00031 sock = socket (AF_INET, SOCK_STREAM, 0);
00032 printf("create socket..\n");
00033
00034 if (sock < 0)
00035 {
00036 printf("no socket...\n");
00037 throw DTtcpExcp(errno);
00038 }
00039
00040
00041 myaddr.sin_family = AF_INET;
00042 myaddr.sin_port = htons (localport);
00043
00044
00045 int blen = 65536*8;
00046
00047
00048
00049
00050
00051
00052 printf("setting socket opts reuse...\n");
00053 if(setsockopt(sock,SOL_SOCKET,SO_REUSEADDR,(char *)&blen,sizeof(blen))<0)
00054 throw DTtcpExcp(errno);
00055
00056
00057
00058 printf("binding...\n");
00059
00060 port = localport;
00061
00062 if (port)
00063 if(bind(sock,(struct sockaddr *)&myaddr,sizeof (myaddr)) < 0)
00064 { perror ("bind failed");
00065 throw DTtcpExcp(errno);
00066 }
00067
00068
00069 }
00070
00071 DTCtcp::DTCtcp(int snew, int opt)
00072 {
00073
00074 connected = true;
00075 port =0;
00076
00077 sock = snew;
00078 }
00079
00080 DTCtcp::DTCtcp(DTCtcp* aconn)
00081 {
00082
00083 connected = aconn->connected;
00084 port = aconn->port;
00085
00086 sock = aconn->sock;
00087 }
00088
00089 DTCtcp::~DTCtcp()
00090 {
00091 printf("deleting DTCtcp\n");
00092
00093 shutdown(sock,SHUT_RDWR);
00094 close (sock);
00095 }
00096
00097 short
00098 DTCtcp::Id()
00099 {
00100 long maddr = clientAddr.sin_addr.s_addr;
00101 maddr = htonl (maddr);
00102 return maddr&0xff;
00103 }
00104
00105 unsigned long
00106 DTCtcp::addr()
00107 {
00108 unsigned long maddr = clientAddr.sin_addr.s_addr;
00109 maddr = htonl (maddr);
00110 return maddr;
00111 }
00112
00113 int
00114 DTCtcp::Disconnect()
00115 {
00116 connected = false;
00117 return shutdown(sock,SHUT_RDWR);
00118 }
00119
00120 DTCtcp *
00121 DTCtcp::Accept()
00122 {
00123
00124
00125
00126 bzero ((char *) &clientAddr, sizeof (clientAddr));
00127
00128 if (listen (sock, 2) < 0)
00129 {
00130 perror ("listen failed");
00131 throw DTtcpExcp(errno);
00132 }
00133
00134 int len = sizeof (clientAddr);
00135
00136 int snew = accept (sock, (struct sockaddr *) &clientAddr,(socklen_t *) &len);
00137 if (snew <=0)
00138 {
00139 perror ("accept failed");
00140 throw DTtcpExcp(errno);
00141 }
00142
00143 return new DTCtcp(snew,0);
00144 }
00145
00146 void
00147 DTCtcp::Connect(unsigned long in,int toport)
00148 {
00149 clientAddr.sin_family = AF_INET;
00150 clientAddr.sin_addr.s_addr = htonl (in);
00151 clientAddr.sin_port = htons (toport);
00152
00153 if (connect (sock, (struct sockaddr *)&clientAddr, sizeof (clientAddr)) < 0)
00154 {
00155 perror ("connect failed");
00156 throw DTtcpExcp(errno);
00157 }
00158 connected = true;
00159 }
00160
00161 void
00162 DTCtcp::Connect(const char *host,int toport)
00163 {
00164 clientAddr.sin_family = AF_INET;
00165 clientAddr.sin_addr.s_addr = inet_addr (host);
00166 clientAddr.sin_port = htons (toport);
00167
00168 if (connect (sock, (struct sockaddr *)&clientAddr, sizeof (clientAddr)) < 0)
00169 {
00170 perror ("connect failed");
00171 throw DTtcpExcp(errno);
00172 }
00173 connected = true;
00174 }
00175
00176 int
00177 DTCtcp::WaitData(int timeout)
00178 {
00179
00180 fd_set rfds;
00181 struct timeval tv;
00182
00183 FD_ZERO(&rfds);
00184 FD_SET(sock,&rfds);
00185
00186 tv.tv_sec = timeout;
00187 tv.tv_usec = 0;
00188
00189 int retva = select (1,&rfds,0,&rfds,&tv);
00190 if (retva)
00191 if (FD_ISSET(0,&rfds)) return 1;
00192 else return -1;
00193 else return 0;
00194 }
00195
00196 int
00197 DTCtcp::Receive(char *buffer,int size)
00198 {
00199
00200
00201 int howmany = 0;
00202 int toberead = size;
00203 do
00204 {
00205
00206 int readnow = recv (sock, &buffer[howmany], toberead,0) ;
00207
00208 if (readnow <= 0 )
00209 {printf("some rrorrs...%d\n",errno); throw DTtcpExcp(errno);}
00210 else {
00211 howmany+=readnow; toberead-=readnow;}
00212 } while (toberead>0);
00213 return howmany;
00214 }
00215
00216 int
00217 DTCtcp::Send(char *buffer,int size)
00218 {
00219 if (connected==false) throw DTtcpExcp(EPIPE);
00220 int myret = write (sock, buffer, size) ;
00221 if (myret<0) throw DTtcpExcp(errno);
00222 return myret;
00223 }