Thread: Winsock

  1. #1
    Rabite SirCrono6's Avatar
    Join Date
    Nov 2003
    Location
    California, US
    Posts
    269

    Question Winsock

    Hmm... I was using Winsock in a console (following a random tutorial I found on the internet) and so far I wanted to see if I got any errors. So I wrote all the code (that I have read):
    Code:
    #include <iostream>
    #include <windows.h>
    
    int main( void )
    {
      WSADATA wsaDat;
      if( WSAStartup( ( 1, 1 ), &wsaDat ) != 0 )
      {
        std::cout << "WSA initialization failed!";
      }
      SOCKET socket;
      socket = ( AF_INET, SOCK_STREAM, 0 );
      if( socket == INVALID_SOCKET )
      {
        std::cout << "Failed to create socket!";
      }
      SOCKADDR_IN sockAddr;
      sockAddr.sin_port = 50;
      sockAddr.sin_family = AF_INET;
      sockAddr.sin_addr.S_un.S_un_b.s_b1 = 127;
      sockAddr.sin_addr.S_un.S_un_b.s_b2 = 0;
      sockAddr.sin_addr.S_un.S_un_b.s_b3 = 0;
      sockAddr.sin_addr.S_un.S_un_b.s_b4 = 1;
      if( bind( socket, ( SOCKADDR* )( &sockAddr ), sizeof( sockAddr ) ) == SOCKET_ERROR )
      {
        std::cout << "Failed to bind!";
      }
      std::cin.get();
      return 0;
    }
    I linked libwsock.a and ended with a single error:

    [Build Error] ["Internet.exe"] Error 1

    I haven't found anything to cause that error yet. See signature for compiler etc.

    - SirCrono6

    P.S. Ah! Haven't seen C++ code in so long!
    From C to shining C++!

    Great graphics, sounds, algorithms, AI, pathfinding, visual effects, cutscenes, etc., etc. do NOT make a good game.
    - Bubba

    IDE and Compiler - Code::Blocks with MinGW
    Operating System - Windows XP Professional x64 Edition

  2. #2
    Handy Andy andyhunter's Avatar
    Join Date
    Dec 2004
    Posts
    540
    It has nothing to do with C++, it is all about your knowledge of networking.

    For instance, where is your include file for winsock?
    Something like this is expected:
    Code:
    #include <WINSOCK2.h>
    This is for the new version of winsock.

    Also this:
    Code:
    if( WSAStartup( ( 1, 1 ), &wsaDat ) != 0 )
    You are missing something in here, such as the MAKEWORD macro, aka
    Code:
    if( WSAStartup( MAKEWORD( 1, 1 ), &wsaDat ) != 0 )
    And are you linking with the correct library?

    Plus this does belong in the Networking Forum.

    Anywho, hope this helps.

    edit: Just bothered to read your sig, you have the newest version of winsock. You should use it.
    Last edited by andyhunter; 02-12-2005 at 09:44 PM.
    i don't think most standard compilers support programmers with more than 4 red boxes - Misplaced

    It is my sacred duity to stand in the path of the flood of ignorance and blatant stupidity... - quzah

    Such pointless tricks ceased to be interesting or useful when we came down from the trees and started using higher level languages. - Salem

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Winsock issues
    By tjpanda in forum Windows Programming
    Replies: 3
    Last Post: 12-04-2008, 08:32 AM
  2. Winsock Messaging Program
    By Morgul in forum Windows Programming
    Replies: 13
    Last Post: 04-25-2005, 04:00 PM
  3. Winsock - Where do i start?
    By Brain Cell in forum Networking/Device Communication
    Replies: 5
    Last Post: 02-14-2005, 01:39 PM
  4. Where do I initialize Winsock and catch messages for it?
    By Lithorien in forum Windows Programming
    Replies: 10
    Last Post: 12-30-2004, 12:11 PM
  5. winsock
    By pode in forum Networking/Device Communication
    Replies: 2
    Last Post: 09-26-2003, 12:45 AM