Search Unity

Communicate with Java program

Discussion in 'Multiplayer' started by DarkBaby, Jun 24, 2009.

  1. DarkBaby

    DarkBaby

    Joined:
    Jun 23, 2009
    Posts:
    8
    Hello,

    I am now writing a c# script which uses System.Net.Sockets to communicate with a Java program. All goes well but How can I close the socket(socketListener here) finally? When the game is finished(by clicking the button), which function will be called?

    Thank you in advance guys and forgive me for my poor English

    /**********************************************************/

    using UnityEngine;
    using System.Collections;
    using System.Collections.Generic;
    using System;
    using System.Net;
    using System.Net.Sockets;
    using System.Threading;
    using System.Text;

    public class NetWork : MonoBehaviour {

    private int port;
    private string hostIp;
    private Thread serverThread;
    private Socket socketListener;
    private List<Connection> connections;

    public Thread ServerThread {
    get { return this.serverThread; }
    set { this.serverThread = value; }
    }

    void Start() {
    serverThread = null;
    socketListener = null;
    connections = new List<Connection>();
    string hostName = Dns.GetHostName();
    string hostIp = Dns.GetHostByName(hostName).AddressList[0].ToString();
    System.Console.Write(hostIp + "\n");
    Debug.Log("Host IP: " + hostIp);

    this.hostIp = hostIp;
    this.port = 20000;

    IPEndPoint endPoint = new IPEndPoint(IPAddress.Parse(this.hostIp), this.port);
    this.socketListener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
    System.Console.Write("Waiting for connection... \n");
    Debug.Log("Waiting for connection...");
    socketListener.Bind(endPoint);
    socketListener.Listen(20);

    serverThread = new Thread(new ThreadStart(Ping));
    serverThread.IsBackground = true;
    serverThread.Start();
    }

    public void Ping() {
    while (true) {
    Socket socket = socketListener.Accept();
    System.Console.Write("Connection established\n");
    Debug.Log("Connection established");

    Connection connection = new Connection(socket);
    Thread thread = new Thread(new ThreadStart(connection.Interact));
    thread.IsBackground = true;
    thread.Name = socket.RemoteEndPoint.ToString();
    connection.MyThread = thread;
    connections.Add(connection);
    thread.Start();

    Thread.Sleep(100);
    }
    }

    public void OnApplicationQuit() {
    try {
    this.socketListener.Shutdown(SocketShutdown.Both);
    } catch(SocketException se) {
    Debug.Log(se.ToString());
    }

    this.serverThread.Abort();
    this.socketListener.Close();
    }

    private class Connection {
    private Socket mySocket;
    private Thread myThread;

    public Connection(Socket socket) {
    this.mySocket = socket;
    myThread = null;
    }

    public Thread MyThread {
    get { return myThread; }
    set { myThread = value; }
    }

    public void Interact() {
    while (true) {
    string recvStr = "";
    byte[] recvBytes = new byte[1024];
    int bytes = mySocket.Receive(recvBytes, recvBytes.Length, 0);

    if (bytes == 0)
    break;

    recvStr += Encoding.ASCII.GetString(recvBytes, 0, bytes);

    Debug.Log("server gets message: " + recvStr);
    string sendStr = "OK! Client send message successful!";
    byte[] bs = Encoding.ASCII.GetBytes(sendStr);
    mySocket.Send(bs, bs.Length, 0);

    Thread.Sleep(100);
    }
    Abort();
    }

    public void Abort() {
    try {
    mySocket.Shutdown(SocketShutdown.Both);
    } catch(SocketException se) {
    Debug.Log(se.ToString());
    }
    Debug.Log("Linked cut");
    mySocket.Close();
    myThread.Abort();
    }
    }

    }
     
  2. perlohmann

    perlohmann

    Joined:
    Feb 12, 2009
    Posts:
    221
     
  3. DarkBaby

    DarkBaby

    Joined:
    Jun 23, 2009
    Posts:
    8
    I've tried your code but I got a runtime error when I quit the Game View. Then I commented the line //socketListener.Close();
    I had non longer the error but when I reclicked the flash button for entering the Game, certainly I got a problem because the socket was not closed.
     
  4. perlohmann

    perlohmann

    Joined:
    Feb 12, 2009
    Posts:
    221
    see: http://forum.unity3d.com/viewtopic.php?t=23875

    it might be an idea to put the thread.abort and listener.close in a try/catch block too.

    furthermore it might also be an idea to alow the socket to rebind to an exsisting bound port. (cant remember the code to do it, but its a socketoption).