www.xbdev.net xbdev - software development
Saturday December 16, 2017
home | about | contact | Donations


XBOX Programming..

More than just a toy...


Prt10 - "Sockets..."

Author: Ben_3D@xbdev.net



With a few lines of code you can connect your xbox to the internet...giving your the possiblity of connecting multiple xbox's together ...creating a web browser...a networked game...etc

I've kept the coding to an absolute basic....it simply initilises your xbox to use the network card, then you connect using sockets to a port and IP address.  In the example below I just chose yahoo.com, but you can change that if you want.


After the program has finished running, the code will reset your xbox, and in the directory you placed your xbe, you should find a file called "DebugInfo.txt" which will have the html of yahoo.com.... there's no real error correction, and just shows how simple it would be to make a chat client/server for the xbox.


Toodles for now.



The whole code is shown below:



/*                                                                         */

/* FileName: main.cpp                                                      */

/*                                                                         */

/* Details: XBOX Socket Basics (prt10)                                     */

/*                                                                         */

/* Author: Ben_3D                                                          */

/*                                                                         */

/* www.xfactordev.net                                                      */

/*                                                                         */




//Main header file for the XDK

#include <xtl.h>


#include <stdio.h>


// Use this simple function to save our data..feed information back into a

// text file in the same directory as our xbe.

void debug(char *str)


      // Used during debugging of the program


      FILE *fp;

      fp=fopen("D:\\DebugInfo.txt", "a+");

      fprintf(fp, "%s\n", str);





void Initialise()



      -1- Initilize Any Main Functions


      debug("Starting up network");



      XNetStartupParams xnsp;

      memset(&xnsp, 0, sizeof(xnsp));

      xnsp.cfgSizeOfStruct = sizeof(XNetStartupParams);


      INT err = XNetStartup(&xnsp);



      DWORD TIMELIMIT = 6000;


      DWORD dwStart = GetTickCount();

      debug("Network started, wait for 6 seconds");

      while ((GetTickCount() - dwStart) < TIMELIMIT)


            // wait it out!!




void Connect()



      WSADATA WsaData;

    int iResult = WSAStartup( MAKEWORD(2,2), &WsaData );

    if( iResult != NO_ERROR )

       debug("Error at WSAStartup");


      char aa[5000];

      SOCKET m_socket;

      m_socket = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);


      int  whathappened=WSAGetLastError ();

      wsprintf(aa, "SOCKET = %ld\n", m_socket);


      wsprintf(aa, "Whathappened= %ld\n", whathappened);



      if (m_socket == INVALID_SOCKET) {

            debug("Error at socket()");





      sockaddr_in service;

      service.sin_family = AF_INET;

      service.sin_addr.s_addr = inet_addr(""); // yahoo.com -> ""

      //service.sin_addr.s_addr = inet_addr("");

      service.sin_port = htons(80);


      int results=connect(m_socket,(sockaddr*) &service,sizeof(struct sockaddr));


      if( results == SOCKET_ERROR)


            debug("Error at connect()");




      send(m_socket, "GET / \r\n", strlen("GET / \r\n"), 0);


      int rr = 1;

      while (rr)


            rr = recv(m_socket, aa, 500, 0);






void CleanUp()




      // Shutdown Winsock






void ReBoot()



   XLaunchNewImage( NULL, (LAUNCH_DATA*)&LaunchData );



//Application entry point

void __cdecl main()


        // Initilisze our xbox so we can connect to the network card



        // Connect to our socket, get the html from yahoo and save it to a debug text file



        // Tidy up and be a good boy



        // Reboot our xbox as all it done and finished.










Received HTML in DebugFile.txt is shown below:


Starting up network
Network started, wait for 6 seconds
SOCKET = -805272824

Whathappened= 183

<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
<meta name="robots" content="noindex,follow">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta http-equiv="refresh" content="60; url=http://www.yahoo.com/">
<title>Yahoo! - Incorrect URL</title>
<body bgcolor="white" alink="white">
<table width=650 cellpadding=0 cellspacing=2 border=0>
<tr><td width=1% valign=top>
<a href="http://www.yahoo.com/"><img src=http://us.i1.yimg.coF7 号h
m/us.yimg.com/i/yahoo.gif width=147 height=31 border=0 alt="Yahoo!"></a></td>
<td align=right><font face=arial size=-1><a href="http://www.yahoo.com/">Yahoo!</a> - <a href="http://help.yahoo.com/">Help</a></font>
<hr size=1 noshade></td></tr></table>
<table width=500 cellpadding=7 cellspacing=0 border=0>
<tr><td align=center><font face=arial size=+1><b>Sorry, the page you requested was not found.</b></font></td></tr>
<tr><td><font face=arial size=-1>Please check the URL for proper spelling and cF7 号h
If you're still having trouble locating a destination on Yahoo!, try visiting the Yahoo! home page
<a href="http://www.yahoo.com/"><b>(www.yahoo.com)</b></a>, or look through a list of
<a href="http://docs.yahoo.com/docs/family/more.html"><b>Yahoo!'s online services</a></b>.
Also, you may find what you're looking for if you try searching below.</font></td></tr>
<table width/b></font></td></tr>
<tr><td><font face=arial size=-1>Please check the URL for proper spelling and cF7 号h
=100% cellpadding=0 cellspacing=0 border=0 bgcolor=cccccc><tr><td height=1></td></tr></table>
<table width=100% cellpadding=10 cellspacing=0 border=0 bgcolor=eeeeee>
<tr><td align=center>
<form name=s1 style="margin-bottom:0" action="http://search.yahoo.com/search">
<table cellpadding=0 cellspacing=0 border=0><tr><td>
<input type=text size=30 name=p title="enter search terms here">&nbsp;
<input type=submit value=Search>&nbsp;&nbsp;</td><td><font face=arial size=-2>&#149;&nbsp;
<a href="http://seF7 号h
arch.yahoo.com/search/options?p=">advanced search</a><br>&#149;&nbsp;
<a href="http://buzz.yahoo.com/">most popular</a></font></td></tr></table></form>
<table width=100% cellpadding=0 cellspacing=0 border=0 bgcolor=cccccc><tr><td height=1></td></tr></table>
<tr><td align=center><font face=arial size=-1>Please try <b><a href="http://help.yahoo.com/">Yahoo! Help Central</a></b>
if you need more assistance.</font></td></tr>
<hr size=1 noshade width=650>
F7 号h
<font face=arial size=-2>Copyright &copy; 2003 Yahoo! Inc. All rights reserved.
<a href="http://privacy.yahoo.com/">Privacy Policy</a> -
<a href="http://docs.yahoo.com/info/terms/">Terms of Service</a></font></center>



Happy coding.


(Back Tutorials Page)



 Visitor: 9534626  { } Copyright (c) 2002-2017 xbdev.net - All rights reserved.
Designated tutorial and software are the property of their respective owners.