Valhalla Legends Archive

Programming => General Programming => Topic started by: MoNksBaNe_Agahnim on November 17, 2003, 12:02 PM

Title: good link for winsock newbies (C++)
Post by: MoNksBaNe_Agahnim on November 17, 2003, 12:02 PM
http://www.pscode.com/vb/scripts/ShowCode.asp?txtCodeId=5279&lngWId=3
Title: Re:good link for winsock newbies (C++)
Post by: iago on November 17, 2003, 12:34 PM
He just indents more and more; he really needs to clean up his code a bit.

Also, I don't think this is right: cout << "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n";

I'm pretty sure there's an easier way to clear the screen! :)

Besides that, it's a good starting point, I actually borrowed some of his code a long time ago
Title: Re:good link for winsock newbies (C++)
Post by: CupHead on November 17, 2003, 12:59 PM
It's actually PSC that formats it like that.
Title: Re:good link for winsock newbies (C++)
Post by: iago on November 17, 2003, 01:09 PM
ah, I thought he just had a lot of nested stuff...


Also, he has goto's, which make it hardish to read :/
Title: Re:good link for winsock newbies (C++)
Post by: MoNksBaNe_Agahnim on November 17, 2003, 04:15 PM
ya he gooned it on the system clear... doing system("cls"); is a lot better...all he did was make it move down a bunch of lines.
Title: Re:good link for winsock newbies (C++)
Post by: TheMinistered on November 29, 2003, 09:05 PM
If you are looking for some sample code that uses winsock, here is some that I wrote:


#include "winsock2.h"
#include "stdio.h"

#define LISTEN_PORT 54



   // INITIALIZE WINSOCK
   WSADATA wsadata;

   if (WSAStartup (MAKEWORD(2,2), &wsadata) != 0) {
      printf("The call to the following function failed: WSAStartup\n");
      return FALSE;
   }

   // CREATE THE SOCKET
   SOCKET listensocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

   if (listensocket == INVALID_SOCKET) {
      printf("The call to the following function failed: socket\n");
      return FALSE;
   }

   // FILL IN THE ADDRESS STRUCTURE (THE "NAME")
   SOCKADDR_IN sockaddr;

   sockaddr.sin_family = AF_INET;
   sockaddr.sin_addr.s_addr = INADDR_ANY;
   sockaddr.sin_port = htons(LISTEN_PORT);

   // BIND THE NAME TO THE SOCKET
   int nReturn = bind(listensocket, (SOCKADDR*)&sockaddr, sizeof(SOCKADDR));

   if (nReturn == SOCKET_ERROR) {
      printf("The call to the following function failed: bind\n");
      closesocket(listensocket);
      return FALSE;
   }

   // START LISTENING ON THE SOCKET
   nReturn = listen(listensocket, SOMAXCONN);

   if (nReturn == SOCKET_ERROR) {
      printf("The call to the following function failed: listen\n");
      closesocket(listensocket);
      return FALSE;
   }
   
   // CLEAN UP AFTER WINSOCK
   closesocket(listensocket);
   WSACleanup();


references: www.msdn.microsoft.com
Title: Re:good link for winsock newbies (C++)
Post by: thetempest on November 30, 2003, 05:01 PM
very nice sample code  ;D

if you want a tutorial that gives sample code along with it of that quality you might wanna check this out.

www.hal-pc.org/~johnnie2/winsock.html
Title: Re:good link for winsock newbies (C++)
Post by: dev invisible on December 10, 2003, 01:58 PM
 nice code TheMinistered