Search code examples
react-nativesocketstcp

Not able to receive response from server using react-native-tcp-socket


Description

I am using react-native-tcp-socket to communicate with a server through my react-native app. When I am using write() to send data to a server. I am not getting response in the data event. Whereas, when I am using telnet to communicate from the server through terminal I am getting the response. Can someone please guide what I am doing wrong here, or suggest some other package for this.

Steps to reproduce

code:

 client = TcpSocket.createConnection(
        {
          port: 7070,
          host: 'priceserver.attache.app'
        },
        () => {
          // sending data to server
          client.write('GETQUOTES');
        },
      );
      // socket event for - connection established
      client.on('connect', (on) => {
        console.log('connected to', on);
      });

      // socket event for - data received
      client.on('data', (data) => {
        console.log('Data ==>', data.toString());
      });

      // socket event for - error occured
      client.on('error', (error) => {
        console.log(error);
      });

      // socket event for - connection closed
      client.on('close', () => {
        console.log('Connection closed!');
      });

Current behavior

all events except data are working

Expected behavior

I should get data in the data event's callback

Screenshots Here is a screenshot of terminal, in which I am getting response. Screenshot from 2021-01-15 12-23-15

Relevant information

OS Ubuntu 20.04
react-native 0.63.2
react-native-tcp-socket ^4.5.5

Solution

  • just saw the question. Self answering for anyone that needs this.

    I needed to add a carriage return at the end of every request I was sending to the server so it was like this

    client.write('GETQUOTES\r');
    

    Hope this helps anyone who stumbles upon this problem.