Murilo de Jesus
Murilo de Jesus

Reputation: 123

How do I convert messages to utf-8

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);
}

enter image description here

enter image description here

Upvotes: 0

Views: 245

Answers (1)

Hablapatabla
Hablapatabla

Reputation: 56

I don't know Javascript, so I can not speak to the specifics of that, but I have a few intuitions that may/may not be able to help out without looking at your C Client.

UTF-8

Firstly, I doubt that not using a UTF-8 encoding for your messages is throwing this error. It is difficult to suggest a solution to this problem without seeing what kind of data you are sending. This seems like a pretty simple implementation, and I have a hunch that your data is probably normal ASCII characters. ASCII characters map directly to UTF-8. If you are using some other encoded data, you'll need to look into the specifics of UTF-8 encoding/decoding. This may be a good place to start.

Is something else afoot?

Error : Error: read ECONNRESET

ECONNRESET usually indicates that the other side of the TCP connection unexpectedly closed their end of the connection, or closed the connection while there is data outstanding. The server is raising this error while the client seems to exit normally. It is far more likely that there is some application-protocol error. Go back through your code and do some rubber-duck debugging. Is there some behavior that is allowing the client to close its end of the connection before the server is ready? One potential cause is using non-blocking sockets. If your client is using a non-blocking socket, you could be calling write()/send(), then exiting immediately afterward, severing the connection before your server has a chance to read.

It is possible but unlikely that UTF-8 encoding (or lack thereof) is causing this problem, and much more likely that your client is, well, resetting the connection. It is difficult to say exactly why your client is closing its end of the connection without seeing any code. Step through both your client and server, and ask yourself at each read/write/connect/accept/close what the current state of the connection on both ends is, and how this function will affect it. If this is neither a UTF-8 or TCP connection issue, then it is possible the issue is JS-specific. This thread would be a good place to start in that case. Again, your implementation is very simple and I doubt there is a more subtle framework issue happening.

Network programming is tricky and requires patience and deliberate thought. Brush up on your understanding of TCP connections, and your understanding of how your programs are working, before jumping to a data encoding issue.

Upvotes: 1

Related Questions