views:

721

answers:

8

New guy want to learn about socket programming in win32. I know bit of MFC but thats not good point to start as it just hides the internal details form programmer.

+1  A: 

Use winsock functions (Winsock2.h).

Philibert Perusse
A: 

Two options:

  1. write a file stream based code that talks to the serial port.
  2. use a C++ library for Serial Port communication (google for this)

The first option means writing 'boiler plate' code so you would be better off using a 3rd party library for that.

The most common 'starting trouble' with serial port programming is to establish a communication link with the device. You NEED to have the correct settings for the port name, baud rate, parity etc (usually given by the manufacturer of device).

Then there will also be a 'command set' for the device. For example if you send '?' to the device, it will return with device information like manufacturer name etc (the '?' command is an example, its not a standard - command set varies from device to device).

All serial communications work like this: - you write a command to the serial port - poll the read buffer for response from the device

To my knowledge there are no built-in Serial port communication classes in C++ or MFC but there are a lot of 3rd party libs for this.

Sesh
socket != serial port
anon
The OP is asking about sockets, not serial ports.
ChrisW
To Sesh defense, it is possible to use MFC sockets for serial communication as well.
Philibert Perusse
+1  A: 

You are right that MFC will obscure some details, I have no idea why they decided to wrap it.

There are a number of good books and tutorials on WinSock programming. Here is one;

http://www.codeproject.com/KB/IP/winsockintro01.aspx

+1  A: 

The answer is to use the win32 socket API.

There's information about this API in MSDN.

It (the so-called 'Winsock' API) is similar to (based on) the Unix/Posix API, so as well as MSDN a lot of the literature (books) about using sockets on Unix are relevent too.

Googling will find you code samples, presumably.

You'll want to know a little about networks: what an "IP address" and a "port" are, for example.

You'll want to choose a protocol (UDP or TCP).

One of the things that's different about Win32 is that you can use ReadFile and WriteFile when you have a connected socket, which means you can use the options for overlapped I/O that are associated with ReadFile and WriteFile.

ChrisW
+1  A: 

http://beej.us/guide/bgnet/

Beej's guide is a pretty common starting point. It has been a few years since I started with this stuff, but iirc, the guide uses Berkeley sockets and points out the differences with winsock where appropriate.

Sam Hoice
+1  A: 

I wholeheartedly agree with ChrisW and comend you for passing on MFC.

See the MSDN Winsock Reference for lots of great WinSock information.

Also, while it may be too large to learn off of, I have what I consider to be a very nice Socket Server Class - let me know if you find any of it useful.

NTDLS
A: 

A small hint would be (assuming you're using Visual Studio and the Winsocket librabry) that you need to link WS2_32.Lib manually. I find that this small step is something a lot of starters miss, and get some weird errors (weird in the eye of the beholder) because of it. http://social.microsoft.com/Forums/en-US/vcgeneral/thread/cfefa4a5-1f1a-4987-8bc7-f3197cb5826c

Also; imho you shouldn't use MFC. There's plenty of great guides for winsocket programming on the google-net :)

Best of luck

cwap