68 #include <sys/socket.h> 70 #include <arpa/inet.h> 71 #include <sys/types.h> 72 #include <netinet/in.h> 92 unopenedSocketFD = socket(PF_INET, SOCK_STREAM, 0);
93 if (unopenedSocketFD == -1)
94 cerr <<
"Could not create socket" << endl;
97 memset(&Address, 0,
sizeof(Address));
98 Address.sin_family = AF_INET;
99 Address.sin_port = htons(_Port);
100 Address.sin_addr.s_addr = INADDR_ANY;
101 int err = bind(unopenedSocketFD, (sockaddr*) &Address,
sizeof(Address));
104 if (errno == EADDRINUSE)
105 cerr<<
"Could not bind socket because socket is already in use. "<<
106 "Make sure you have closed hpctraceviewer, wait 30 seconds and try again. " <<
107 "Alternatively, you can choose a port other than "<< _Port<<
"."<<endl;
109 cerr <<
"Could not bind socket. Error was " << strerror(errno) << endl;
113 err = listen(unopenedSocketFD, 5);
115 cerr<<
"Listen failed: " << strerror(errno)<<endl;
118 cout <<
"Waiting for connection on port " << getPort() << endl;
126 unsigned int len =
sizeof(client);
127 socketDesc = accept(unopenedSocketFD, (sockaddr*) &client, &len);
129 cerr <<
"Error on accept" << endl;
130 file = fdopen(socketDesc,
"r+b");
138 struct sockaddr_in sin;
139 socklen_t len =
sizeof(sin);
140 if (getsockname(unopenedSocketFD, (
struct sockaddr *) &sin, &len) == -1)
142 cerr <<
"Could not obtain port" << endl;
146 return ntohs(sin.sin_port);
157 shutdown(socketDesc, SHUT_RDWR);
159 close(unopenedSocketFD);
166 fwrite(Buffer, 4, 1, file);
172 fwrite(Buffer, 8, 1, file);
177 int r =
fwrite(Data, 1, Length, file);
179 cerr<<
"Only wrote "<<r <<
" / " <<Length<<endl;
184 writeShort(toWrite.length());
185 fwrite(toWrite.c_str(), toWrite.length(), 1, file);
192 fwrite(Buffer, 2, 1, file);
197 int e = fflush(file);
199 cerr <<
"Error on sending" << endl;
242 short Len = readShort();
244 char* Msg =
new char[Len + 1];
259 Long longForm = readLong();
272 cout <<
"Connection closed" << endl;
virtual void writeShort(short)
virtual ~DataSocketStream()
virtual void writeInt(int)
static void writeInt(char *buffer, int ToWrite)
size_t MONITOR_EXT_WRAP_NAME() fread(void *ptr, size_t size, size_t count, FILE *stream)
virtual void writeDouble(double)
virtual void writeRawData(char *, int)
static int64_t readLong(char *buffer)
static Long convertDoubleToLong(double d)
static int readInt(char *buffer)
virtual void writeString(string)
virtual void writeLong(Long)
size_t MONITOR_EXT_WRAP_NAME() fwrite(const void *ptr, size_t size, size_t count, FILE *stream)
static short readShort(char *buffer)
static void writeLong(char *buffer, int64_t ToWrite)
static double convertLongToDouble(Long l)
static void writeShort(char *buffer, short ToWrite)