Question by
Comini2 · Feb 24, 2018 at 06:21 PM ·
networkingserverclientudp
UDPClient can't receive and send ?
I've made this code to test connection with my UDP java server, the client can't receive any data from the server, but the server receives the data from the client. What am i doing wrong ?
public class GameClient : MonoBehaviour {
private UdpClient client;
private IPEndPoint endPoint;
private IPEndPoint serverEp;
private Thread receiveThread;
private Thread sendThread;
public int sendRate = 12;
private void Awake() {
}
private void Start () {
endPoint = new IPEndPoint(IPAddress.Any, 0);
serverEp = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 6666);
client = new UdpClient();
receiveThread = new Thread(new ThreadStart(receiveData));
sendThread = new Thread(new ThreadStart(sendData));
receiveThread.Start();
sendThread.Start();
}
private void Update () {
}
private void receiveData() {
while (true) {
byte[] data = client.Receive(ref endPoint);
print(Encoding.UTF8.GetString(data));
}
}
private void sendData() {
while (true) {
byte[] data = Encoding.UTF8.GetBytes("01Comini");
client.Send(data, data.Length, serverEp);
Thread.Sleep(1000 / sendRate);
}
}
private void OnDisable() {
if(receiveThread != null)
receiveThread.Abort();
if(sendThread != null)
sendThread.Abort();
client.Close();
}
}
Comment