What I want to ask is this:
When running the server code in Visual Studio and the client code in Unity on Windows, the server successfully receives responses from the client.
However, when running the server code in Visual Studio and the client code as a mobile app on a phone, the server fails to receive responses from the client.
What is the reason for this?
The smartphone and my laptop are connected to the same Wi-Fi network.
Server Code :
using System.Net;
using System.Net.Sockets;
using System.Text;
namespace ServerCore
{
internal class Program
{
static void Main(string[] args)
{
string hostName = Dns.GetHostName();
IPHostEntry ipHostEntry = Dns.GetHostEntry(hostName);
IPAddress ipAddress = ipHostEntry.AddressList[1];
IPEndPoint endPoint = new IPEndPoint(ipAddress, 7777);
Socket listenSocket = new Socket(endPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
try
{
listenSocket.Bind(endPoint);
listenSocket.Listen(10);
while (true)
{
Console.WriteLine("Listening..");
//클라이언트 입장을 기다린다.
Socket clientSocket = listenSocket.Accept();
//받는다
byte[] recvBuffer = new byte[1024];
int recvBytes = clientSocket.Receive(recvBuffer);
string recvData = Encoding.UTF8.GetString(recvBuffer, 0, recvBytes);
Console.WriteLine($"[From Client] {recvData}");
//보낸다
byte[] sendBuff = Encoding.UTF8.GetBytes("Welcome To MMORPG Server");
clientSocket.Send(sendBuff);
//쫒아낸다.
clientSocket.Shutdown(SocketShutdown.Both);
clientSocket.Close();
}
}
catch (Exception ex)
{
}
}
}
}
Client Code :
using System.Net;
using System.Net.Sockets;
using System.Text;
using UnityEngine;
public class NetWorkTest : MonoBehaviour
{
void Start()
{
string hostName = Dns.GetHostName();
IPHostEntry ipHostEntry = Dns.GetHostEntry(hostName);
IPAddress ipAddress = ipHostEntry.AddressList[1];
IPEndPoint endPoint = new IPEndPoint(ipAddress, 7777);
Socket socket = new Socket(endPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
socket.Connect(endPoint);
byte[] send = Encoding.UTF8.GetBytes("Hello From Unity");
socket.Send(send);
byte[] recv = new byte[1024];
int len = socket.Receive(recv);
Debug.Log(Encoding.UTF8.GetString(recv, 0, len));
socket.Close();
}
}