Search Unity

Socket Crash!!!

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

  1. DarkBaby

    DarkBaby

    Joined:
    Jun 23, 2009
    Posts:
    8
    Here is my code. When I launch the program, there is a crash!
    /***********************************************************/

    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 NetWorkClient : MonoBehaviour, IDisposable{

    public GameObject prefab;

    private int port;
    private string hostIp;
    private bool socketInUse = true;
    private Connection signalConnection = null;
    private Connection dataConnection = null;
    private Socket signalSocket = null;
    private Socket dataSocket = null;
    private IPEndPoint endPoint = null;

    private enum State {Normal, Suspended, Stopped};

    public void Start() {
    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;

    endPoint = new IPEndPoint(IPAddress.Parse(this.hostIp), this.port);
    signalSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
    signalSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
    signalSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.DontLinger, true);

    dataSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
    dataSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
    dataSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.DontLinger, true);

    CreateSignalThread();
    CreateDataThread();
    }

    protected virtual void Dispose(bool disposing) {
    if (disposing) {
    this.signalConnection.Abort();
    this.dataConnection.Abort();
    }
    }

    public void Dispose() {
    Dispose(true);
    GC.SuppressFinalize(this);
    GC.Collect();
    }

    public void OnApplicationQuit() {
    Dispose();
    Debug.Log("Game Over");
    }

    public void CreateSignalThread() {
    Thread signalThread = new Thread(new ThreadStart(SignalCommunicate));
    signalConnection = new Connection(signalSocket, signalThread);
    signalThread.Start();
    }

    public void SignalCommunicate() {
    Debug.Log("Waiting for SignalConnection...");
    signalSocket.Connect(endPoint);
    Debug.Log("SignalConnection Established");

    }

    public void CreateDataThread() {
    Thread dataThread = new Thread(new ThreadStart(DataCommunicate));
    dataConnection = new Connection(dataSocket, dataThread);
    dataThread.Start();
    }

    public void DataCommunicate() {
    Debug.Log("Waiting for DataConnection...");
    dataSocket.Connect(endPoint);
    Debug.Log("DataConnection Established");
    }

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

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

    public Connection(Socket socket, Thread thread) {
    this.mySocket = socket;
    this.myThread = thread;
    }

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

    public Socket MySocket {
    get { return mySocket; }
    set { mySocket = value; }
    }

    public void Abort() {
    if(mySocket.Connected) {
    try {
    mySocket.Shutdown(SocketShutdown.Both);
    } catch(SocketException se) {
    Debug.Log(se.ToString());
    }
    }
    mySocket.Close();
    Debug.Log("Client socket shut down");
    myThread.Abort();
    }
    }

    /*
    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();
    } */

    }