Thread: Code error (socket prgm)

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    10

    Question Code error (socket prgm)

    Hi guys,
    I've tried the socket programming for the first time a very simple one, however I encountered some problems...
    here's the code...
    ====================

    Code:
    #include <stdio.h>
    #include <winsock.h>
    #include <iostream.h>
    void main()
    {
    	afxsocketinit();
    	int nRet,sd=0;
    	int port_to_connect_to = 5555; //port no.
    	short nPort;
    
    	Csocket.socket;
    
    	socket.create();
    
    	if(!socket.Connect(10.1.3.40,5555))
    	{
    	  int err = socket.GetLastError();
    	  cout << err << endl;
    	}
    
    }
    =========================================

    Gives me the following errors!!
    est_wskp2.cpp
    C:\C++\IP2\Fe96_vp\Test_wskp2.cpp(11) : error C2065: 'afxsocketinit' : undeclared identifier
    C:\C++\IP2\Fe96_vp\Test_wskp2.cpp(16) : error C2065: 'Csocket' : undeclared identifier
    C:\C++\IP2\Fe96_vp\Test_wskp2.cpp(16) : error C2228: left of '.socket' must have class/struct/union type
    C:\C++\IP2\Fe96_vp\Test_wskp2.cpp(18) : error C2228: left of '.create' must have class/struct/union type
    C:\C++\IP2\Fe96_vp\Test_wskp2.cpp(20) : error C2228: left of '.Connect' must have class/struct/union type
    C:\C++\IP2\Fe96_vp\Test_wskp2.cpp(20) : error C2143: syntax error : missing ')' before 'constant'
    C:\C++\IP2\Fe96_vp\Test_wskp2.cpp(20) : error C2059: syntax error : ')'
    C:\C++\IP2\Fe96_vp\Test_wskp2.cpp(21) : error C2143: syntax error : missing ';' before '{'
    C:\C++\IP2\Fe96_vp\Test_wskp2.cpp(22) : error C2228: left of '.GetLastError' must have class/struct/union type
    Error executing cl.exe.
    ===========================================

    Please can some one help me out with this!!
    Basically its to do with making a connection to a server on a port.
    If the return code is -1 the connection failed else success

    Many Thanks!
    John

    Code tags added by Kermi3

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    I haven't used asfsocketinit(), but MSDN says it's declared in "Header file: Declared in Afxsock.h"

    >Csocket.socket;
    What's this?

    >void main
    main returns an int, so use int main().

    Try that for starters.

    And please use code tags.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Refugee face_master's Avatar
    Join Date
    Aug 2001
    Posts
    2,052
    why not just use the plane old SOCKET structure instead of your fancy Csocket's?

  4. #4
    Lead Moderator kermi3's Avatar
    Join Date
    Aug 1998
    Posts
    2,595

    Code Tags

    In the furture please use Code Tags. People will be much more likely to help you if you do. And they'll be happy about it

    Information on code tags may be found at:

    http://www.cprogramming.com/cboard/...&threadid=13473
    Kermi3

    If you're new to the boards, welcome and reading this will help you get started.
    Information on code tags may be found here

    - Sandlot is the highest form of sport.

  5. #5
    Registered User
    Join Date
    Aug 2002
    Posts
    170
    Inluding the proper header file should get you further down the road.

    I am also playing with some network programming, and I have found that it is MUCH easier if you derive a class from CSocket. It has built in funcitons that are called when data is received on your socket. If you don't use a derived class, you will constantly have to check the stream to see if there is data waiting. By deriving a class from CSocket you can overide the OnReceive() function and execute the proper code there.
    Best Regards,

    Bonkey

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Extended ASCII Characters in an RTF Control
    By JustMax in forum C Programming
    Replies: 18
    Last Post: 04-03-2009, 08:20 PM
  2. Enforcing Machine Code Restrictions?
    By SMurf in forum Tech Board
    Replies: 21
    Last Post: 03-30-2009, 07:34 AM
  3. Values changing without reason?
    By subtled in forum C Programming
    Replies: 2
    Last Post: 04-19-2007, 10:20 AM
  4. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 04:17 PM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM