//ConnectInformation info = new ConnectInformation(); //ConnectionSettings settings = new ConnectionSettings(); //settings.HostAddress = new Microsoft.DirectX.DirectPlay.Address("127.0.0.1", //23456);; //Guid g = new Guid("69456-442-3482443-91914"); //info.Flags = ConnectFlags.LaunchNew; //info.GuidApplication = g; //info.ConnectionSettings = settings; /info.UseConnectionSettings = false; //connectioninfo = theClient.ConnectApplication(info, 60000, null); //theClient.SetConnectionSettings(connectioninfo, settings); // Include appropriate namespaces using Microsoft.DirectX; using Microsoft.DirectX.DirectPlay; // Hold onto the Peer for later use Peer peer; Guid appGuid = new Guid("69456-442-3482443-91914"); private void InitializeDirectPlay() { peer = new Peer(); peer.IndicateConnect += new IndicateConnectEventHandler(PlayerConnected); peer.Receive += new ReceiveEventHandler(ReceiveMessage); peer.PlayerDestroyed += new PlayerDestroyedEventHandler(PlayerDestroyed); } //DirectPlay – Hosting a Session private void Host() { // Set up the description of our application ApplicationDescription desc = new ApplicationDescription(); desc.GuidApplication = appGuid; // GUID uniquely identifies this app desc.Flags = SessionFlags.NoDpnServer; // Don't use DPN server (advanced setting) desc.SessionName = "Mech Session"; // Name of the session Address addr = new Address(); // Our address addr.ServiceProvider = "Mechreg" Address.ServiceProviderTcpIp; // We're using TCP/IP addr.AddComponent("66.27.61.206", 1439); // Port to listen on // Start listening peer.Host(desc, addr); } DirectPlay – Sending Data private void SendMessage(string msg) { // Create a new network packet and shove the string into it // Overloads for Arrays and primitives also available NetworkPacket packet = new NetworkPacket(); packet.Write(msg); // Send the network packet peer.SendTo( (int) PlayerID.AllPlayers, // Send to everyone packet, // Data to send 0, // Timeout (default) SendFlags.Sync); // Send synchronously } //DirectPlay – Handling Server Events // All of these methods are called on a thread *other* than the main // thread – so be very careful with UI calls private void PlayerConnected(object sender, IndicateConnectEventArgs e) { // A new player has connected – handle appropriately MessageBox.Show("Player has connected"); } private void ReceiveMessage(object sender, ReceiveEventArgs e) { // A new message has arrived – handle appropriately MessageBox.Show(e.Message.ReceiveData.ReadString()); } private void PlayerDestroyed(object sender, PlayerDestroyedEventArgs e) { // A player has left the game – handle appropriately MessageBox.Show("Player has disconnected"); } //Hello World – Client-Side DirectPlay //DirectPlay setup on the server side //create a Peer object //call Connect //Send data/respond to Receive event //respond to SessionTerminated event //beware – events raised on different threads! //DirectPlay – Client Initialization // Include appropriate namespaces using Microsoft.DirectX; using Microsoft.DirectX.DirectPlay; // Hold onto the Peer for later use Peer peer; Guid appGuid = new Guid("69456-442-3482443-91914"); private void InitializeDirectPlay() { peer = new Peer(); peer.Receive += new ReceiveEventHandler(ReceiveMessage); peer.SessionTerminated += new SessionTerminatedEventHandler(SessionTerminated); } //DirectPlay – Client Connection private void ConnectToHost() { // Set up the description of our application ApplicationDescription desc = new ApplicationDescription(); desc.GuidApplication = appGuid; desc.Flags = SessionFlags.NoDpnServer; // Don't use DPN server (advanced setting) desc.SessionName = "Mech Session”; // Which session do we want? // The address of the machine we're trying to connect to Address addr = new Address("66.27.61.206", 1439); // Our local address Address local = new Address(); local.ServiceProvider = "Mechreg"; Address.ServiceProviderTcpIp; // We're using TCP/IP peer.Connect( desc, // Description of the application addr, // The address we're connecting to local, // Where we are null, // User connection date (unused for us) ConnectFlags.Sync); // Connect synchronously } //DirectPlay – Handling Client Events // All of these methods are called on a thread *other* than the main // thread – so be very careful with UI calls private void ReceiveMessage(object sender, ReceiveEventArgs e) { // A new message has arrived – handle appropriately MessageBox.Show(e.Message.ReceiveData.ReadString()); } private void SessionTerminated(object sender, SessionTerminatedEventArgs e) { // The server terminated the session – handle appropriately MessageBox.Show("Server has terminated session"); }