Home > other >  Unity by socket implementation of mobile phone as the client, the computer as a message from the ser
Unity by socket implementation of mobile phone as the client, the computer as a message from the ser

Time:09-27

I want to use cell phone when the client send messages to a computer, but not on the computer like mobile phone (two projects at the same time in the computer test can normal transfer), if the phone when the service side, when the client computer can normal communication,
The server and client code is written in the script for the unity,
Why will appear this kind of problem, is that what permissions didn't open a computer, or a problem with the IP address?
Sorrow bald head... For bosses to solve! Be urgent! Online etc.!

CodePudding user response:

First of all, they are in the same local area network (LAN)

CodePudding user response:

reference 1/f, 001 China reply on the tip of the tongue:
first of all, they are in the same local area network (LAN)

Hello ~
Even the same WiFi, cell phones and computers IP address is on the computer, after manual input by the input field, but mobile phone card in the connection here

CodePudding user response:

It is know that in general the debug, the feeling of your description is not in the same local area network (LAN), it could also be a firewall problem

CodePudding user response:

references on the tip of the tongue of China's 001 reply: 3/f
that in general the debug once know, feel your description is not in the same local area network (LAN), it could also be a firewall problem

This is part of the client code
Using UnityEngine;
Using System. The Collections;

Using System.Net.NetworkInformation;
Using System.Net;
Using System.Net.Sockets;
Using System. IO;

Using.net;


Net namespace
{
Public class SocketClient: MonoBehaviour

{
//public GameObject test;

//public string myIP;
Private static byte [] result=new byte [1024].
Private static byte [] result_2=new byte [1024].
Public static string realResult=null;
Private static Socket clientSocket;
//whether the identity of the connected
Public bool IsConnected=false;

Public SocketClient ()
{
ClientSocket=new Socket (AddressFamily. InterNetwork, SocketType Stream, ProtocolType. Tcp);
}

///& lt; Summary>
///connect to specify the IP and port server
///& lt;/summary>
///& lt; Param name="IP" & gt;
///& lt; Param name="port" & gt;
IP public void ConnectServer (string, int port)
{
IPAddress mIp=IPAddress. Parse (IP);
IPEndPoint ip_end_point=new IPEndPoint (mIp, port);

Try
{
ClientSocket. Connect (ip_end_point);
IsConnected=true;
The Debug Log (" connect to the server success ");
}
Catch
{
IsConnected=false;
The Debug Log (" connection server failed ");
return;
}

Try
{
//server distributed data length
Int receiveLength=clientSocket. The Receive (result);
ByteBuffer buffer=new ByteBuffer (result);
Int len=buffer. ReadShort ();
String data=(https://bbs.csdn.net/topics/buffer.ReadString);

The Debug Log (" data from server: "+ data).
}
Catch {}

}

///& lt; Summary>
///to send data to the server
///& lt;/summary>
Public void SendMessages (string data)
{
If (IsConnected==false)
return;
Try
{
ByteBuffer buffer=new ByteBuffer ();
Buffer. WriteString (data);
ClientSocket. Send (WriteMessage (buffer ToBytes ()));
}
Catch
{
IsConnected=false;
ClientSocket. Shutdown (SocketShutdown. Both);
ClientSocket. Close ();
}
}

///& lt; Summary>
///the data transformation, the network data sent to two parts, one is data length, the other is the main data
///& lt;/summary>
///& lt; Param name="message" & gt;
///& lt; Returns>
Private static byte [] WriteMessage (byte [] message)
{
MemoryStream ms=null;
Using (ms=new MemoryStream ())
{
Ms. Position=0;
BinaryWriter writer=new BinaryWriter (ms);
Ushort msglen=(ushort) message. Length;
Writer. Write (msglen);
Writer. Write (message);
Writer. Flush ();
Return Ms. ToArray ();
}
}

//connection is closed
Public void SocketQuit ()
{
//close the client first
If (clientSocket!=null)
{
IsConnected=false;
ClientSocket. Shutdown (SocketShutdown. Both);
ClientSocket. Close ();
The Debug Log (" to close the client ");
}
}

}

}


Using UnityEngine;
Using System. The Collections;
Using.net;
Using System.Net.Sockets;
Using System.Net.NetworkInformation;
Using UnityEngine. UI;

Using UnityEngine. Android;
Using System. Linq;

Public class SocketClientTest: MonoBehaviour
{
String myIP;
Int myPort;
SocketClient mSocket=new SocketClient ();
Public Text myText;
Public InputField inputIP;//get the input IP
Public GameObject startPanel;//interface
//static int I=0;

//public GameObject player;//the location of the record player and send
//string x, y, z;


//Use this for initialization
Void the Start ()
{
MyText. Text="Now Start";
MyPort=1998;
});
}

//Update is called once per frame
Void the Update ()
{
//x=player. The transform. The position, x.T oString ();
//y=player. The transform. The position, y.T oString ();
//z=player. The transform. The position. Z.T oString ();
//mSocket ConnectServer (myIP, 1989);
//mSocket. SendMessages (" x "+ x +" y "+ y + z + z);
}

Public void OnPressBtn ()
{
Destroy (startPanel);
MyIP=inputIP. Text;
MSocket. ConnectServer (myIP, myPort);
MyText. Text="IP=" + myIP + ", the port="+ myPort. The ToString ();
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related