#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "../states/iso15118_20_states.h"
size_t clientsend(int sock_fd, const char *outdata)
{
return send(sock_fd, outdata, strlen(outdata), 0);
}
size_t clientrecv(int sock_fd, char *indata, size_t buflen)
{
size_t nbytes = recv(sock_fd, indata, buflen - 1, 0);
if(nbytes > 0)
{
indata[nbytes] = '\n';
}
return nbytes;
}
void start_client()
{
const char* host = "0.0.0.0";
int port = 7000;
int sock_fd;
struct sockaddr_in serv_name;
int status;
char indata[1024] = {0}, outdata[1024] = {0};
// create a socket
sock_fd = socket(AF_INET, SOCK_STREAM, 0);
if (sock_fd == -1) {
perror("Socket creation error");
exit(1);
}
// server address
serv_name.sin_family = AF_INET;
inet_aton(host, &serv_name.sin_addr);
serv_name.sin_port = htons(port);
status = connect(sock_fd, (struct sockaddr *)&serv_name, sizeof(serv_name));
if (status == -1) {
perror("Connection error");
exit(1);
}
EVCCSessionSetup evccsessionsetup(nullptr,sock_fd);
evccsessionsetup.process_message(nullptr);
while (1) {
printf("Please input message: ");
memset(outdata, 0, sizeof(outdata));
fgets(outdata,sizeof(outdata), stdin);
printf("send: %s\n", outdata);
clientsend(sock_fd,outdata);
//send(sock_fd, outdata, strlen(outdata), 0);
memset(indata, 0, sizeof(indata));
size_t nbytes = clientrecv(sock_fd, indata, sizeof(indata));
//recv(sock_fd, indata, sizeof(indata) - 1, 0);
if (nbytes <= 0) {
close(sock_fd);
printf("server closed connection.\n");
break;
}
indata[nbytes] = '\0';
printf("recv: %s\n", indata);
}
//return 0;
}