react-native-tcp-socket: Starting a Server always returns EADDRINUSE on every port

Description

I just followed the example from the npm page for creating a tcp server. If i try to run this it always loggs following: image I’ve tried multiple ports but nothing helped. I always get the “Adress already in use” error.

Steps to reproduce

Steps to reproduce the behavior:

  1. Start a new RN app with the example from the React Nativ Get Started Documentation
  2. Download and install react-nativ-tcp-socket as shown on the github page
  3. Implement the Server example as shown in the image in the Description section of this bug report
  4. Start the app on youre smartphone with npx react-native run-android
  5. A node winow should have poped up where you can now see logs and bundle infos for debugging. It also should now show the error “adress already in use”

The Code of the App:

import * as React from 'react';
import {Text, View} from 'react-native';
import TcpSocket from 'react-native-tcp-socket';

export interface IMainViewProps {}

export interface IMainViewState {
  log: string;
}

const Port = 12345;

export default class MainView extends React.Component<
  IMainViewProps,
  IMainViewState
> {
  constructor(props: IMainViewProps) {
    super(props);

    this.state = {
      log: '',
    };
  }

  writeToLog(msg: string, callback?: () => void) {
    this.setState({log: this.state.log + msg + '\n'}, callback);
  }

  componentDidMount() {
    this.writeToLog('Starting Server on port ' + Port + '...', () => {
      const server = TcpSocket.createServer((socket) => {
        socket.on('data', (data) => {
          this.writeToLog(data);
        });

        socket.on('error', (error) => {
          console.log('An error ocurred with client socket ', error);
        });

        socket.on('close', (error) => {
          console.log('Closed connection with ', socket.address());
        });
      }).listen({port: Port, host: 'localhost'});

      server.on('error', (error) => {
        console.log('An error ocurred with the server', error);
      });

      server.on('close', () => {
        console.log('Server closed connection');
      });
    });
  }

  public render() {
    return (
      <View style={{padding: 10}}>
        <Text style={{color: '#3D9970'}}>{this.state.log}</Text>
      </View>
    );
  }
}

Current behavior

After the App executed it shows the “Starting Server on port…” message and in the console log of the app i can see a “EADDRINUSE” error which means that the given adress is already in use.

Expected behavior

It should just start the tcp server.

Screenshots I have added them in the documentation section of this bug report.

Relevant information

OS Windows 10 Pro N & Androidversion 10QKQ1.191215.002 (Android 10)
react-native 0.63.3
@types/react-native ^0.63.2
react-native-tcp-socket ^4.5.4

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 17 (5 by maintainers)

Most upvoted comments

Glad you found a workaround. I might have an idea on how to fix that, but it requires more testing.

Another question I got for @Rapsssito is: why are you JSON stringify the socket. address() variable in the example if it returns a string anyway? This doesn’t make any sense to me.

You are right, it is a remainder from the old example code, I will correct it on the next update.

Ok, I think I finally got a solution. I just added this to the componentWillUnmount() function:

  componentWillUnmount() {
    this.server.close();
    this.server.destroy();
    this.server = null;
  }

This somehow works at least for hot-reloads but not for a complete reload (like shaking your phone and then clicking reload or typing r in the dev console). However, if you want to reload your app you need to close the app completely on you’re phone (just on your phone not in the dev console) and then reopen the app on your phone.

This is my last post for now and I will close this bug since this should work as a workaround. If someone finds any “real” solution you can post it here.

New Update:

It looks like if i use the exact code as in the template it does work. Even with the before tested ports that didn’t work it now works. And i realy dont know why. I am trying a few things and if i stumble upon the issue i will post it here but for now this worked for me. Thanks a lot @Rapsssito