58 lines
944 B
C
58 lines
944 B
C
#include "client/client.h"
|
|
#include <lwip/sockets.h>
|
|
|
|
#define PORT 9000
|
|
const char *address = "192.168.40.88";
|
|
|
|
int sendJSON(char *json, int len) {
|
|
|
|
int fd = 0, res = 0;
|
|
|
|
struct sockaddr_in server;
|
|
|
|
fd = socket(AF_INET, SOCK_STREAM, 0);
|
|
if (fd < 0) {
|
|
return -1;
|
|
}
|
|
|
|
server.sin_family = AF_INET;
|
|
server.sin_port = htons(PORT);
|
|
|
|
res = inet_pton(AF_INET, address, &server.sin_addr);
|
|
if (res < 0) {
|
|
close(fd);
|
|
return -2;
|
|
}
|
|
|
|
res = connect(fd, (struct sockaddr *)&server, sizeof(server));
|
|
if (res < 0) {
|
|
close(fd);
|
|
return -3;
|
|
}
|
|
|
|
res = sendAll(fd, json, len);
|
|
if (res < 0) {
|
|
close(fd);
|
|
return -4;
|
|
}
|
|
|
|
close(fd);
|
|
return 0;
|
|
}
|
|
|
|
int sendAll(int sock, char *buf, int len) {
|
|
|
|
int total = 0, bytesleft = len, n = 0;
|
|
|
|
while (total < len) {
|
|
n = send(sock, buf + total, bytesleft, 0);
|
|
if (n < 0) {
|
|
break;
|
|
}
|
|
total += n;
|
|
bytesleft -= n;
|
|
}
|
|
|
|
return n == -1 ? -1 : 0;
|
|
}
|