Opening Socket

This is a discussion on Opening Socket within the C Programming forums, part of the General Programming Boards category; Hey all, I need to open a socket in C. I am a total newbie to C-programming so I used ...

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    4

    Question Opening Socket

    Hey all,

    I need to open a socket in C. I am a total newbie to C-programming so I used the MSDN example and got through it, making all the steps. I want to compile the program now, but I don't understand what's going wrong.
    I get these errors:

    Quote Originally Posted by Compiler
    C:\Users\<snip>\Cclient>cobc -x ClientSocket.c
    ClientSocket.c
    ClientSocket_main.c
    ClientSocket_main.obj : error LNK2005: _main already defined in ClientSocket.obj
    ClientSocket_main.obj : error LNK2019: unresolved external symbol _CLIENTSOCKET referenced in function _main
    ClientSocket.exe : fatal error LNK1120: 1 unresolved externals

    Can anyone give me a good starting point to programming a simple socket in C?
    Last edited by Wander; 12-20-2010 at 06:36 AM.

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    1,620
    Did you include the right headers?
    Devoted my life to programming...

  3. #3
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    1,620
    Maybe you should post the code ( in code quotes please ).
    Devoted my life to programming...

  4. #4
    Registered User
    Join Date
    Dec 2010
    Posts
    4
    Quote Originally Posted by Sipher View Post
    Did you include the right headers?
    I assume MSDN has no errors in it's code-examples...

    Code:
    #define WIN32_LEAN_AND_MEAN
    
    #include <windows.h>
    #include <winsock2.h>
    #include <ws2tcpip.h>
    #include <stdlib.h>
    #include <stdio.h>
    
    // Need to link with Ws2_32.lib, Mswsock.lib, and Advapi32.lib
    #pragma comment (lib, "Ws2_32.lib")
    #pragma comment (lib, "Mswsock.lib")
    #pragma comment (lib, "AdvApi32.lib")
    
    #define DEFAULT_BUFLEN 512
    #define DEFAULT_PORT "10000"
    
    int __cdecl main(int argc, char **argv) 
    {
        WSADATA wsaData;
        SOCKET ConnectSocket = INVALID_SOCKET;
    
    ...etc...

  5. #5
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    1,620
    Code:
    int __cdecl main(int argc, char **argv)
    O_o ... What are you trying to do with main?! Your compiler is right to complain! Do you want to import or export it?

    Where do you use that macro _CLIENTSOCKET?
    Devoted my life to programming...

  6. #6
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    1,620
    What compiler are you using?
    Devoted my life to programming...

  7. #7
    Registered User
    Join Date
    Dec 2010
    Posts
    4
    Quote Originally Posted by Sipher View Post
    Code:
    int __cdecl main(int argc, char **argv)
    O_o ... What are you trying to do with main?! Your compiler is right to complain! Do you want to import or export it?

    Where do you use that macro _CLIENTSOCKET?
    I got it from this example: MSDN

    And never: the name of _CLIENTSOCKET changes if I change the filename of the c sourcecode. If I name it cside, the error changes to _CSIDE

    EDIT: The compiler is based on MSVC
    Last edited by Wander; 12-20-2010 at 06:59 AM.

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You have two files: ClientSocket.c and ClientSocket_main.c. If they both include a main() function, that's one main() too many. I don't see the word _CLIENTSOCKET anywhere, so I don't know what you've done there.

  9. #9
    Registered User
    Join Date
    Dec 2010
    Posts
    4
    Problem solved. I was compiling it with the wrong options :/
    Thanx for the quick replies!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Why the local socket program occur core dump?
    By chenayang in forum Linux Programming
    Replies: 16
    Last Post: 08-16-2010, 08:39 AM
  2. Function call from another .c module
    By Ali.B in forum C Programming
    Replies: 14
    Last Post: 08-03-2009, 11:45 AM
  3. Problem with socket descriptors
    By McKracken in forum C Programming
    Replies: 1
    Last Post: 07-22-2009, 08:51 AM
  4. socket programming question, closing sockets...
    By ursula in forum Networking/Device Communication
    Replies: 2
    Last Post: 05-31-2009, 05:17 PM
  5. socket newbie, losing a few chars from server to client
    By registering in forum Linux Programming
    Replies: 2
    Last Post: 06-07-2003, 11:48 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21