Thread: Socket programming in Windows

  1. #1
    Registered User
    Join Date
    Dec 2015
    Posts
    92

    Socket programming in Windows

    Hello C++ experts,

    I would like to ask someone who already used C++ for windows socket programming. I am getting following errors:

    identifier "Winsock" is undefined and many redefinition errors

    I searched through stackoverflow forums and for these errors people suggested:

    "
    You need to re-order the includes of the Windows headers like so:
    #include <WinSock2.h>
    #include <WS2tcpip.h>
    #include <Windows.h>
    In other words, the WinSock headers must be included before Windows.h.
    "
    I included WinSock header before Windows header and still can't get it working also tried other suggestions from this post:
    c++ - socket errors can&#39;t get functions in WinSock2.h - Stack Overflow

    Code:
    #include "stdafx.h"
    #include "Winsock.h"
    #include <WinSock2.h>
    #include <Windows.h>
    #include <ws2bth.h>
    #include <bthsdpdef.h>
    #include <bluetoothapis.h>
    
    #pragma comment(lib, "ws2_32.lib")
    #pragma comment(lib, "irprops.lib")
    
    int main()
    {
    	if (!Winsock::InitialzedForCurrentProcess())
    	{
    		return 1;
    	}
    
    	Winsock winsock = Winsock(AF_BTH, SOCK_STREAM, BTHPROTO_RFCOMM);
    
    	if (!winsock.IsValid())
    	{
    		return 1;
    	}
    
    	winsock.Bind();
    
    	winsock.Listen();
    
    	winsock.RegisterServcie(_T("RFCOMM Server Demo Instance"), _T("Pushing data to PC"), OBEXObjectPushServiceClass_UUID);
    
    	winsock.OnConnected([](SOCKET s){
    		char buffer[1024] = { 0 };
    
    		memset(buffer, 0, sizeof(buffer));
    
    		int r = 0;
    
    		do
    		{
    			r = recv(s, (char*)buffer, sizeof(buffer), 0);
    
    			printf("result:%d, %s\n", r, buffer);
    
    		} while (r != 0);
    
    		closesocket(s);
    	});
    
    	winsock.Accept();
    
    	WSACleanup();
    
        return 0;
    }
    code source: [Bluetooth] – How to Host a RFCOMM Service in C++ – Practice & Share

    Best regards

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Remove the include of "stdafx.h" to be sure that is NOT causing the issue.

    Or you can edit the header to include the headers in the right order.

    Creating a Basic Winsock Application (Windows)

    Tim S.
    Last edited by stahta01; 05-28-2017 at 04:33 PM. Reason: Added link
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > memset(buffer, 0, sizeof(buffer));
    What does this line do, that the previous line didn't?

    > r = recv(s, (char*)buffer, sizeof(buffer), 0);
    If the buffer is completely full, where is the \0 that will stop printf(%s) from roaming all through memory?

    If the first message is "I'm a banana" and the second message is "hello", what will be printed the second time?

    In addition to removing stdafx.h, also remember to go into project->compiler settings and locate the tick box which says "use pre-compiled headers". Make sure this is turned OFF.

    > RegisterServcie
    Is this really how it's spelt?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Dec 2015
    Posts
    92
    Quote Originally Posted by Salem View Post
    > memset(buffer, 0, sizeof(buffer));
    What does this line do, that the previous line didn't?

    > r = recv(s, (char*)buffer, sizeof(buffer), 0);
    If the buffer is completely full, where is the \0 that will stop printf(%s) from roaming all through memory?

    If the first message is "I'm a banana" and the second message is "hello", what will be printed the second time?

    In addition to removing stdafx.h, also remember to go into project->compiler settings and locate the tick box which says "use pre-compiled headers". Make sure this is turned OFF.

    > RegisterServcie
    Is this really how it's spelt?
    Salem this is not my code, I copy/pasted it from msdn site as I mentioned at the end of my post. I tried removing stdafx.h and turned off VS option "use precompiled headers"


    about website: Creating a Basic Winsock Application (Windows)

    I already tried it before and I get the same errors

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by High Voltage View Post
    Salem this is not my code, I copy/pasted it from msdn site as I mentioned at the end of my post. I tried removing stdafx.h and turned off VS option "use precompiled headers"


    about website: Creating a Basic Winsock Application (Windows)

    I already tried it before and I get the same errors
    I do NOT believe you!

    Edit: I see no way you can get the only error you listed!!
    identifier "Winsock" is undefined

    Goodbye.

    Tim S.
    Last edited by stahta01; 05-29-2017 at 08:51 AM.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  6. #6
    'Allo, 'Allo, Allo
    Join Date
    Apr 2008
    Posts
    639
    The original code requires the rest of the files from the github link in the blog post, not just what was pasted into the post.

  7. #7
    Registered User
    Join Date
    Dec 2015
    Posts
    92
    Quote Originally Posted by adeyblue View Post
    The original code requires the rest of the files from the github link in the blog post, not just what was pasted into the post.
    Thank you it compiled now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Socket Programming on Windows
    By Nidal in forum C++ Programming
    Replies: 5
    Last Post: 10-20-2011, 10:33 AM
  2. Socket programming in C on windows 7
    By wahla in forum Networking/Device Communication
    Replies: 1
    Last Post: 09-27-2010, 08:59 AM
  3. How to make winsock.h for windows socket programming?
    By thomas_joyee in forum Networking/Device Communication
    Replies: 9
    Last Post: 03-17-2010, 11:28 PM
  4. About Device Driver Programming And Socket Programming
    By pritesh in forum Linux Programming
    Replies: 6
    Last Post: 01-23-2010, 03:46 AM
  5. socket programming for windows in C
    By lepricaun in forum C Programming
    Replies: 15
    Last Post: 09-08-2004, 11:34 PM

Tags for this Thread