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
"