/*-----------------------------Peer---To---Peer-------*/ private void PeerToPeerConnectionHost() { object HostTemp; //NewPack.ChipColor = true; try { while (true) { HostTemp = formatter.Deserialize(P2PConnectStream); if (HostTemp is CPacket.Message) { CPacket.Message receptionMessage = new CPacket.Message(); receptionMessage = HostTemp as CPacket.Message; if (Chat.InvokeRequired) { BeginInvoke(new kdelegate(AddToList), ((CPacket.Message)HostTemp).m_message, 2); } //MessageBox.Show(receptionMessage.m_message); } if (HostTemp is CPacket.LogOut) { CPacket.LogOut LogOut = new CPacket.LogOut(); LogOut = HostTemp as CPacket.LogOut; MessageBox.Show(LogOut.UsersDisconnected + " disconnected"); if (P2PConnectStream != null || !P2P.IsAlive) { if (which == true)//hostmode { HostListener.Stop(); P2PConnectStream.Close(); P2PConnectStream = null; } if (which == false) { P2PConnectStream.Close(); P2PConnectStream = null; } //P2PConnectStream = null; break;//makes the thread die } } if (HostTemp is CPacket.GameData) { NewPack = HostTemp as CPacket.GameData; UpdateBoard(NewPack); } if (HostTemp is CPacket.LoginPacket) { CPacket.LoginPacket Viewer = new CPacket.LoginPacket(); Viewer = HostTemp as CPacket.LoginPacket; //formatter.Serialize() } if (HostTemp is CPacket.Again) { // MessageBox.Show("You Lost"); //CPacket.Again againReceived = new CPacket.Again(); //againReceived = HostTemp as CPacket.Again; ////if (againReceived.response == true) //{ // this.EraseBoard(); //.GameDesign(); //} } if (HostTemp is CPacket.Losing) { MessageBox.Show("You Lost"); //CPacket.Again againReceived = new CPacket.Again(); //againReceived = HostTemp as CPacket.Again; ////if (againReceived.response == true) //{ // this.EraseBoard(); //.GameDesign(); //} } } } catch (System.IO.IOException P2Perror) { //System.Runtime.Serialization.SerializationException } }
public void WFCProcedure() { int y; for (int x = 4; x >= 0; x--) { openLocation.Push(x); } NetworkStream tempstream = null; Int32 port = 3005; LoginData User = new LoginData(); listener = new TcpListener(IPAddress.Any, port); listener.Start(); object temp = null; try { while (true) { connection = listener.AcceptSocket(); // blocking call CheckConnect.Image = IconList.Images[1]; //change the icon on server form tempstream = new NetworkStream(connection); //temporary Networkstream temp = formatter.Deserialize(tempstream); if (temp is CPacket.LoginPacket) { User.user_id = ((CPacket.LoginPacket)temp).user_id; User.user_ip = ((IPEndPoint)connection.RemoteEndPoint).Address; } if (openLocation.Count == 0) { CPacket.LogOut LogOut = new CPacket.LogOut(); formatter.Serialize(tempstream, LogOut); } else { openLocation.Pop(); AllSockets[NextLocation] = new LoginData(); AllSockets[NextLocation].TheSocket = connection; AllSockets[NextLocation].ConnStream = tempstream; AllSockets[NextLocation].Connected = true; AllSockets[NextLocation].user_id = User.user_id; AllSockets[NextLocation].user_ip = User.user_ip; //MessageBox.Show(User.user_id + " is connected"); AllSockets[NextLocation].position = NextLocation;; CheckConnect.Image = IconList.Images[1]; CPacket.LoginPacket UserConnected = new CPacket.LoginPacket(); if (UserBox.InvokeRequired) { BeginInvoke(new kdelegate(AddToList), User.user_id); } for (int x = 0; x < NextLocation; x++) { if (AllSockets[x] != null && AllSockets[x].user_id != User.user_id) { UserConnected.user_id = AllSockets[x].user_id; formatter.Serialize(AllSockets[x].ConnStream, temp); //send old users new user formatter.Serialize(tempstream, UserConnected); // send new user old users } } AllThreads[NextLocation] = new Thread(new ParameterizedThreadStart(AreYouTalkingtome)); AllThreads[NextLocation].Start(AllSockets[NextLocation]); NextLocation++; } } } catch (SocketException e) { MessageBox.Show("Server Shutting down!"); } }//Wait for connection