Jump to content

Socket Programming

- - - - -

  • Please log in to reply
10 replies to this topic

#1
hetra

hetra

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 297 posts
  • Location:Australia
  • Programming Language:C, C++, PHP, Python, Delphi/Object Pascal, Assembly
  • Learning:Python, Assembly
Hello all,

I have downloaded Beej's Guide to Network Programming. It is for *nix platforms (it has a way to edit it for windows platforms but they seemed a little complicated). I was wanting to know if there lies any guides designed for Windows platforms.

Any help would be great.

#2
Ancient Dragon

Ancient Dragon

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 400 posts
Depending on how the code was written MS-Windows sockets works very similar to *nix, the main difference is in the header files and libraries. MS-Windows uses winsock2.h and ws2_32.lib. I have a copy of a rather large free library that I got from another site which no longer exists. The source code is portable for both *nix and MS-Windows, and for several different compilers. Included in the library are some socket stuff that you might be interested in seeing. If you want it I'll send it to you -- the zip file is about 2 mg. It even contains and example client/server programs that use sockets.
Visit Grandpa's Forums, a social networking forum, with family-oriented arcade games, blogs, discussion forums, and photo albums.

#3
hetra

hetra

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 297 posts
  • Location:Australia
  • Programming Language:C, C++, PHP, Python, Delphi/Object Pascal, Assembly
  • Learning:Python, Assembly
Yeah that sounds great! Thanks. Also, in the guide it says this (just an extract):

Quote

This is what you'll have to do (unless you install Cygwin!): first, ignore pretty much all of the
system header files I mention in here. All you need to include is:
#include <winsock.h>
Wait! You also have to make a call to WSAStartup() before doing anything else with the sockets
library. The code to do that looks something like this:
#include <winsock.h>

{

WSADATA wsaData; // if this doesn't work

//WSAData wsaData; // then try this instead

// MAKEWORD(1,1) for Winsock 1.1, MAKEWORD(2,0) for Winsock 2.0:

if (WSAStartup(MAKEWORD(1,1), &wsaData) != 0) {

fprintf(stderr, "WSAStartup failed.\n");

exit(1);
}

Can someone please explain that (it seemed a little confusing at the time).

#4
Ancient Dragon

Ancient Dragon

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 400 posts
winsock.h is now replaced by winsock2.h The rest of that mearly initializes the socket library for a specific version of the library. Read the remarks section here
Visit Grandpa's Forums, a social networking forum, with family-oriented arcade games, blogs, discussion forums, and photo albums.

#5
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,118 posts
  • Location:Vancouver, Eh! Cleverness: 200
WSAData, without "data" being all capitals, is a struct that contains information about the Windows Socket implementation as noted here: WSADATA Structure (Windows). MAKEWORD is a macro for concatination, but we'll leave that out, it will assign a version number to wsaData.

EDIT: Dragon got to it before me.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#6
hetra

hetra

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 297 posts
  • Location:Australia
  • Programming Language:C, C++, PHP, Python, Delphi/Object Pascal, Assembly
  • Learning:Python, Assembly
It also says to initialise WSACleanup() after the sockets library is done with, does that mean at the end of the program?

Also, would work in C++ as well? (fprintf and exit(1) seemed like C code).

#7
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,118 posts
  • Location:Vancouver, Eh! Cleverness: 200
Those functions are both valid in C++. You'd ideally call WSACleanup() after you do not require the socket to send or recieve data, if you wish to place it as a destructor in a class or at the end of your program you could fine.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#8
hetra

hetra

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 297 posts
  • Location:Australia
  • Programming Language:C, C++, PHP, Python, Delphi/Object Pascal, Assembly
  • Learning:Python, Assembly
Okay, I think I'll research classes for sockets more. Thanks alot Nullw0rm.

#9
Muted

Muted

    Learning Programmer

  • Members
  • PipPipPip
  • 86 posts

Hunter100 said:

Okay, I think I'll research classes for sockets more. Thanks alot Nullw0rm.

If you prefer YouTube tutorials, here's one that explains it very well: YouTube - Winsock C++ Tutorial part 1 of 2
“You may be disappointed if you fail, but you are doomed if you don't try.”
- Beverly Sills

#10
hetra

hetra

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 297 posts
  • Location:Australia
  • Programming Language:C, C++, PHP, Python, Delphi/Object Pascal, Assembly
  • Learning:Python, Assembly
Thank You also, I was looking for some video tutorials but couldn't find any low enough for me to begin.

#11
Muted

Muted

    Learning Programmer

  • Members
  • PipPipPip
  • 86 posts

Hunter100 said:

Thank You also, I was looking for some video tutorials but couldn't find any low enough for me to begin.

You're welcome. :)
“You may be disappointed if you fail, but you are doomed if you don't try.”
- Beverly Sills




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users