I'm developing a sockets that the client is in c and the server is in javascript and when I send the data from one to the other, the error read ECONNRESET occurs. I researched the subject and says it is because it is not in uft-8. How do I fix this problem?
Edit. After realizing that my problem is with the client, I will publish the client's code here.
server
socket.on('data',function(data){
var bread = socket.bytesRead;
var bwrite = socket.bytesWritten;
console.log('Bytes read : ' + bread);
console.log('Bytes written : ' + bwrite);
console.log('Data sent to server : ' + data);
//echo data
var is_kernel_buffer_full = socket.write('Data ::' + data);
});
client
#define closesocket close
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <locale.h>
#include <netdb.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define PROTOPORT 9925
extern int errno;
char localhost[] = "localhost";
int main(int argc, char * argv[])
{
struct message {int code; char str[132];} msg;
struct hostent *ptrh;
struct protoent *ptrp;
struct sockaddr_in sad;
int sd;
int port;
char *host;
int n;
char buf[1000];
char *text;
#ifdef WIN32
WSADATA wsaData;
WSAStartup(0x0101, &wsaData);
#endif
memset((char *)&sad,0,sizeof(sad));
sad.sin_family = AF_INET;
if (argc > 2) {
port = atoi(argv[2]);
} else {
port = PROTOPORT;
}
if (port > 0)
sad.sin_port = htons((u_short)port);
else {
fprintf(stderr,"Bad port number %s\n",argv[2]);
exit(1);
}
if (argc > 1) {
host = argv[1];
} else {
host = localhost;
}
ptrh = gethostbyname(host);
if ( ((char *)ptrh) == NULL ) {
fprintf(stderr,"Invalid host: %s\n", host);
exit(1);
}
memcpy(&sad.sin_addr, ptrh->h_addr, ptrh->h_length);
if ( ((int)(ptrp = getprotobyname("tcp"))) == 0) {
fprintf(stderr, "Cannot map \"tcp\" to protocol number");
exit(1);
}
sd = socket(AF_INET, SOCK_STREAM, ptrp->p_proto);
if (sd < 0) {
fprintf(stderr, "Socket creation failed\n");
exit(1);
}
if (connect(sd, (struct sockaddr *)&sad, sizeof(sad)) < 0) {
fprintf(stderr,"Connect failed\n");
exit(1);
}
while (msg.code < 2) {
printf ("Client: connection established with server\n");
printf ("hello from client\n");
printf ("Codigo = ");
msg.code = atoi (fgets(buf, sizeof(buf), stdin));
printf ("Mensagem = ");
text = fgets(msg.str, sizeof(msg.str), stdin);
send(sd, &msg/*buf*/, sizeof msg /*strlen(*buf)*/, 0);
n = recv(sd, &msg /*buf*/, sizeof(msg /*buf*/), 0);
printf ("Resposta = %s\n", msg.str);
}
closesocket(sd);
exit(0);
}