This project is read-only.

socket in xna

Jul 1, 2012 at 4:33 PM
Edited Jul 1, 2012 at 4:36 PM

Hi ohan, I know maybe that is not the right place to ask for help about socket in xna, but I try to make a question in the xna official forum of microsoft but nobody answer me. 

I have to create a socket in xna where the client(written in c++) send the values of cameraPose of a device. The xna server read them and displays on the screen. Besides, I display also the rgb video stream of the camera device.

So far, i've created this function:

 

public void StartListeningSync()
        {
            // Data buffer for incoming data.
            byte[] bytes = new Byte[1024];

            // Establish the local endpoint for the socket.
            // Dns.GetHostName returns the name of the 
            // host running the application.
            IPHostEntry ipHostInfo = Dns.Resolve(Dns.GetHostName());
            //IPAddress ipAddress = ipHostInfo.AddressList[0];
            IPEndPoint localEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 4000);

            // Create a TCP/IP socket.
            Socket listener = new Socket(AddressFamily.InterNetwork,
                SocketType.Stream, ProtocolType.Tcp);
            
            // Bind the socket to the local endpoint and 
            // listen for incoming connections.

        
            try{
                
                listener.Bind(localEndPoint);
                listener.Listen(10);

                // Start listening for connections.
                // while (true)
                // {
                Console.WriteLine("Waiting for a connection...");
                // Program is suspended while waiting for an incoming connection.
                Socket handler = listener.Accept();
                 
                        connesso = true;
                        Console.WriteLine("connesso");
                   
                // An incoming connection needs to be processed.
                  while (true)
                    {
                        bytes = new byte[1024];
                        int bytesRec = handler.Receive(bytes);
                        content = System.BitConverter.ToSingle(bytes, 0);
                        prova = content * 4;
                     
                        // Show the data on the console.
                        Console.WriteLine("Text received : {0}" + content + "     e prodotto " + prova);
                   }
                    
                }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }

    }

 

I put it into the update() method and seems it works, as a matter of fact It can read the data from the client; but, it stops the rest of the program. In short, while the socket is open and read the data, the rgb video doesn't start. Besides, I can't wait the end of the connection, because the values of camera pose are always updated. So, I would like to display both the rgb and the data from the client in real time. 

Where or how do you suggest me to insert the socket in a xna project? 

I'm asking you because I noticed that you know xna framework very well and maybe you can help me! :D

 

cheers

 

salvo

Coordinator
Jul 3, 2012 at 9:30 PM

Well, Socket programming in .NET has nothing to do with XNA, so you may want to separate your problem from XNA. You should first test your C# server application with your C++ client application to make sure that your Socket programming is correct (that it doesn't cause the program to stop). You may also want to write out your image data to disk to make sure it shows an image captured by the video camera.

Ohan

Jul 3, 2012 at 10:47 PM

thanks for replying. ok, so I have to separate the two situation. However, I need the values of position of the camera sent by the client because I would like to draw a virtual object on to the rgb video. If I don't tell to xna that the camera(device) is moving, the object will always be in the same position of the window.

More or less As you have done in dominoAr: if i move the camera closer to the dominos, they become bigger. In my case, there wouldn't be any marker, and the position of the camera would be already determinated by the client program.

cheers

 

salvo