void startThreadForSyncTCP() { syncSockTCP = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); label.text += "\r\n self " + MGFoundtion.getInternIP() + ";serverip " + MGGlobalDataCenter.defaultCenter().serverIp + ";clientIP" + MGGlobalDataCenter.defaultCenter().clientIP; if (MGGlobalDataCenter.defaultCenter().isFrontRoler) { syncIEP = new IPEndPoint(IPAddress.Parse(MGGlobalDataCenter.defaultCenter().clientIP), MGGlobalDataCenter.defaultCenter().SyncPort); //服务器的IP和端口 try { //因为客户端只是用来向特定的服务器发送信息,所以不需要绑定本机的IP和端口。不需要监听。 syncSockTCP.Connect(syncIEP); } catch (SocketException e) { label.text += " unable to connect to server" + e.ToString(); } InvokeRepeating("syncNetworkTCP", 0.1f, 0.010f); } else { syncIEP = new IPEndPoint(IPAddress.Any, MGGlobalDataCenter.defaultCenter().SyncPort); //本机预使用的IP和端口 syncSockTCP.Bind(syncIEP); syncThread = new Thread(syncToReceiveTCP); syncThread.IsBackground = true; syncThread.Start(); } }
void startThreadForSync() { syncSock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); //初始化一个Scoket实习,采用UDP传输 label.text += "\r\n self " + MGFoundtion.getInternIP() + ";serverip " + MGGlobalDataCenter.defaultCenter().serverIp + ";clientIP" + MGGlobalDataCenter.defaultCenter().clientIP; if (MGGlobalDataCenter.defaultCenter().isFrontRoler) { //label.text += "\r\n self " + MGFoundtion.getInternIP() + ";serverip " + MGGlobalDataCenter.defaultCenter().serverIp; IPAddress clientAddress = IPAddress.Broadcast; syncIEP = new IPEndPoint(clientAddress, MGGlobalDataCenter.defaultCenter().SyncPort); //初始化一个发送广播和指定端口的网络端口实例 syncEP = (EndPoint)syncIEP; //syncSock.Bind(syncEP);//绑定这个实例 syncSock.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, 1); //设置该scoket实例的发送形式 InvokeRepeating("syncNetwork", 0.1f, 0.010f); } else { //label.text += "\r\n self " + MGFoundtion.getInternIP() + ";serverip " + MGGlobalDataCenter.defaultCenter().serverIp; //Debug.Log("serverIp="+MGGlobalDataCenter.defaultCenter().serverIp); IPAddress serverAddress = IPAddress.Any; syncIEP = new IPEndPoint(serverAddress, MGGlobalDataCenter.defaultCenter().SyncPort); //初始化一个侦听局域网内部所有IP和指定端口 syncEP = (EndPoint)syncIEP; syncSock.Bind(syncEP); //绑定这个实例 syncThread = new Thread(syncToReceive); syncThread.IsBackground = true; syncThread.Start(); } }
void Start() { isReceiveIP = false; isCancelListen = false; udpReceive = new listenForServerDelegate(UDPStartToReceive); MGGlobalDataCenter.defaultCenter().serverIp = MGFoundtion.getInternIP(); }