Valhalla Legends Forums Archive | General Programming | good link for winsock newbies (C++)

AuthorMessageTime
MoNksBaNe_Agahnim
http://www.pscode.com/vb/scripts/ShowCode.asp?txtCodeId=5279&lngWId=3
November 17, 2003, 6:02 PM
iago
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
November 17, 2003, 6:34 PM
CupHead
It's actually PSC that formats it like that.
November 17, 2003, 6:59 PM
iago
ah, I thought he just had a lot of nested stuff...


Also, he has goto's, which make it hardish to read :/
November 17, 2003, 7:09 PM
MoNksBaNe_Agahnim
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.
November 17, 2003, 10:15 PM
TheMinistered
If you are looking for some sample code that uses winsock, here is some that I wrote:

[code]
#include "winsock2.h"
#include "stdio.h"

#define LISTEN_PORT 54
[/code]

[code]
   // 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();
[/code]

references: www.msdn.microsoft.com
November 30, 2003, 3:05 AM
thetempest
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
November 30, 2003, 11:01 PM
dev invisible
nice code TheMinistered
December 10, 2003, 7:58 PM

Search