-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSocketServer.cpp
More file actions
151 lines (99 loc) · 2.86 KB
/
SocketServer.cpp
File metadata and controls
151 lines (99 loc) · 2.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#include "SocketServer.h"
int readLine(int sockFd, char *buffer, int maxSize) {
char c = 0;
int count = 0;
bzero(buffer,maxSize);
while ( (c!=10)&&(count<maxSize) ) {
read(sockFd, &c, 1);
buffer[count] = c;
count++;
}
if (count>=2)
buffer[count-2] = 0;
if (count>maxSize-2) {
return -1;
}
return count;
}
int initServer(int portNo) {
int sockFd, n;
struct sockaddr_in servAddr;
int keepServing = 1;
sockFd = socket(AF_INET, SOCK_STREAM, 0);
if (sockFd > 0) {
bzero((char *) &servAddr, sizeof(servAddr));
servAddr.sin_family = AF_INET;
servAddr.sin_port = htons(portNo);
servAddr.sin_addr.s_addr = INADDR_ANY;
int optVal = 1;
setsockopt(sockFd,SOL_SOCKET,SO_REUSEADDR,&optVal,sizeof(int));
if (bind(sockFd, (struct sockaddr *) &servAddr, sizeof(servAddr)) < 0) {
sockFd = -1;
} else {
listen(sockFd,1);
}
}
return sockFd;
}
int waitForConnection(int sockFd, struct sockaddr_in &cliAddr) {
//struct sockaddr_in cliAddr;
socklen_t clilen;
int newSockFd = -1;
clilen = sizeof(cliAddr);
newSockFd = accept(sockFd, (struct sockaddr *) &cliAddr, &clilen);
return newSockFd;
}
int sendImageBuffer(int clientSocket, uint8_t *imageBuffer, int imageSizeW, int imageSizeH) {
int errorState = 0;
char utfSize[4];
utfSize[1] = (imageSizeW&0xff00)>>8;
utfSize[0] = imageSizeW&0xff;
utfSize[3] = (imageSizeH&0xff00)>>8;
utfSize[2] = imageSizeH&0xff;
int n = write(clientSocket,utfSize,4);
if (n < 0) {
errorState = 1;
}
n = write(clientSocket,imageBuffer,imageSizeW*imageSizeH);
if (n < 0) {
errorState = 1;
}
return errorState;
}
int sendFluxImage(int clientSocket, float *fluxImage, int fluxImageW, int fluxImageH) {
int errorState = 0;
char utfSize[4];
utfSize[1] = (fluxImageW&0xff00)>>8;
utfSize[0] = fluxImageW&0xff;
utfSize[3] = (fluxImageH&0xff00)>>8;
utfSize[2] = fluxImageH&0xff;
int n = write(clientSocket,utfSize,4);
if (n < 0) {
errorState = 1;
}
n = write(clientSocket,(uint8_t *)fluxImage,fluxImageW*fluxImageH*4);
if (n < 0) {
errorState = 1;
}
return errorState;
}
int SendResultBuffer(int clientSocket, char *lInBuffer) {
int errorState = 0;
int lInBufferSize = strlen(lInBuffer);
if (lInBufferSize == 0) {
lInBufferSize = 2;
lInBuffer[0] = 13;
lInBuffer[1] = 10;
}
// send string size (for utf string reading...)
char utfSize[2];
utfSize[0] = (lInBufferSize&0xff00)>>8;
utfSize[1] = lInBufferSize&0xff;
int n = write(clientSocket,utfSize,2);
if (n < 0)
errorState = 1;
n = write(clientSocket,lInBuffer,strlen(lInBuffer));
if (n < 0)
errorState = 1;
return errorState;
}