0

I have two project, a Java server and a unity client written in C#. When I run java server , the unity app has connected successfully on Windows. But when I tried connect on real android device it is not working.The Android is connected to the laptop using a USB cable.

Simple java code

package main;


import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

public class Server1 {

    private static  final int portnumber=6422;
    public static void main(String[] args){
        ServerSocket serverSocket =null;
        try {
            System.out.println("Server starts on port"+portnumber);
            serverSocket = new ServerSocket(portnumber);

            //Client connecting
            System.out.println("Waiting for clients to connect");
            Socket socket = serverSocket.accept();
            System.out.println("A client has connected");

            System.out.println("server has ended");
        }catch (IOException e){
            e.printStackTrace();

        }
    }
}

Client code

using UnityEngine;
using System.Net.Sockets;
using System.IO;
using System;
public class Client : MonoBehaviour
{

    private bool socketready;
    private TcpClient socket;

    public void ConnectToServer()
    {

        //if already connected , ignore this function
        if (socketready)
            return;

        //my laptop private ip address.
        string host = "192.168.1.2";
        int port = 6422;
        //create the socket
        try
        {
            socket = new TcpClient(host, port);
            socketready = true;
        }
        catch (Exception e)
        {
            print("socket error: " + e.Message);
        }
    }}

In unity I have button(will call ConnectToServer).The problem is when I try click this button cannot get "A client has connected" in java console.

On android studio it says "socket error: Connection timed out "

Programmer
  • 121,791
  • 22
  • 236
  • 328
StepHan
  • 61
  • 2
  • 12
  • 1
    Put `Debug.Log` int `ConnectToServer` function to make sure that it is being called at-allby the Button. – Programmer Jul 11 '17 at 19:35
  • The code works on Windows – StepHan Jul 11 '17 at 19:45
  • 1
    Ok. I missed that part. *"The Android is connected to the laptop using a USB cable"*... I don't think that's useful here. You are supposed to make sure that the Android device and the computer the Java server is running on are both connected to the-same network. – Programmer Jul 11 '17 at 19:49
  • Yes! Both of them connected to the same network. That is why I used for `host` my private ip address-`192.168.1.2` – StepHan Jul 11 '17 at 19:57
  • 1
    Since you are printing the socket error, why not actually tell us what the error is? You can either use [this](https://stackoverflow.com/questions/44690357/how-to-read-debug-log-when-using-android/44690501#44690501) to get the error or simple create a UI text on the screen and show the error (`e.Message`) there. It would be good to know what Unity version and Android version you are using just in-case. – Programmer Jul 11 '17 at 20:01
  • It says `socket error: connection timed out`. I have Unity 5.6.0p4 and my android device `4.2.2`..in unity min Api level is `4.1` – StepHan Jul 11 '17 at 20:19
  • 1
    Ok.If you are sure that the IP of the device that is running the Java program is `192.168.1.2` and you are connected to the-same network, can you try `IPAddress.Parse("192.168.1.2");` instead of directly using `192.168.1.2`. For example `socket = new TcpClient(IPAddress.Parse("192.168.1.2");, port);`.... – Programmer Jul 11 '17 at 20:26
  • 1
    One more thing. I assume that the Java program is running on Windows, please disable firewall for that Java program. You can Google *"How to disable Firewall for Applications"*. – Programmer Jul 11 '17 at 20:28
  • @Programmer you just with your last comment answered to the entire question(I meant **Firewall**). If it is not hard move that comment to the answer – StepHan Jul 12 '17 at 07:27

1 Answers1

1

Your Firewall seems to be blocking the connection from the Android device. Just add the Compiled Java application to the Firewall exception list to make sure that it does not block and incoming traffic.


Unrelated but you need to make sure that the connect function is being done in another Thread. Also make make sure that your receive code is also done in another Thread. Putting the receive code in the connect Thread is also fine.

Finally, note that you can't use Unity's API in another Thread. You will need a way to execute your a code in the main Thread if you are using Unity's API from another Thread. Please take a look at this wrapper I made for that.

Programmer
  • 121,791
  • 22
  • 236
  • 328