CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 22
  1. #1
    Join Date
    Apr 2002
    Location
    Mumbai,India
    Posts
    567

    socket programming

    Hi
    Can somebody explain me in detail how to use socket in my vc++ program
    i know we have to use CSocket but how?
    please help me
    thanks in advance
    vishal

  2. #2
    Join Date
    Jul 2003
    Location
    Korea
    Posts
    60
    Please read MSDN help, there are mountain of articles and sampels about socket programming.
    Quang

  3. #3
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652

  4. #4
    Join Date
    Apr 2002
    Location
    Mumbai,India
    Posts
    567

    Unhappy

    Hi
    I have studied this socket through msdn .....
    but my problem is how to actually implement those things in my code.......
    it would be better if somebody could explain me in detail...i mean to say.....
    after a wizard had created a project how to move forward...
    i have added a afxsock.h header file in my code...both in server as well as client.....
    now i have also created a object of CSocket class and also started to listen but not able to accept the client request.....
    also whether i have to make server listen in a loop or it continue to listen untill some client ask for connection......
    also where i should place my code for server to listen....
    and where to place my code to accept the conection.....
    thanks
    vishal
    this is my first try to socket programming
    NOTE:I am using vc++ for coding

  5. #5
    Join Date
    Apr 2002
    Location
    Mumbai,India
    Posts
    567

    Unhappy

    i am still looking for the answer.......
    atleast suggest me the method to solve this........or some good tutorial which would help me to create a program in vc++ for socket using CSocket
    thanks
    vishal

  6. #6
    Join Date
    Aug 2003
    Posts
    16

  7. #7
    Join Date
    Jul 2003
    Location
    Korea
    Posts
    60
    As your description, I guess that you did not call :
    AfxSocketInit(NULL) in your InitInstance() function to initialize windows sockets.

    To create server socket:

    1. CSocket::Create( port to listen );
    2. CSocket::Listen();

    Whenever connect request is accepted, OnAccepted(...) will be called, you have to override this function:

    Your socket::OnAccepted(...)
    {
    3. Create new socket
    4. Accepted( paramater is your newly created socket);
    }

    This is simple, and it works fine.
    Quang

  8. #8
    Join Date
    Apr 2002
    Location
    Mumbai,India
    Posts
    567
    OK please look below for my code.......

    in my server application-------------->
    1>simple dialog based application.
    2>selected winsock control in 3rd step of wizard.
    3>in OnInitDialog() function i have added the following line

    if(!AfxSocketInit())
    {
    AfxMessageBox(IDP_SOCKETS_INIT_FAILED);
    return FALSE;
    }
    CSocket csServer;
    csServer.Create(3000);
    csServer.Listen(5);
    //MessageBox("I am server");

    5>since i am not able to decide how i will accept the connection,i have for the example purpose put a new button on my dialog box and put the following in its double click functions

    CSocket tempSocket;
    objSocket.Accept(tempSocket);
    int k = GetLastError();
    ////////////////////////////////////////////////////////////////////////////////////////
    switch(k)
    {
    case WSANOTINITIALISED: AfxMessageBox("A successful AfxSocketInit must occur before using this API.");break;
    case WSAENETDOWN : AfxMessageBox("Network is down"); break;
    case WSAEADDRINUSE : AfxMessageBox("Specified address is already in use");break;
    case WSAEINPROGRESS :AfxMessageBox("a blocking windows socket call is in progress");break;
    case WSAEADDRNOTAVAIL:AfxMessageBox("The specified address is not available from the local machine.");break;
    case WSAEAFNOSUPPORT :AfxMessageBox("Addresses in the specified family cannot be used with this socket");break;
    case WSAECONNREFUSED :AfxMessageBox("The attempt to connect was rejected.");break;
    case WSAEDESTADDRREQ :AfxMessageBox(" A destination address is required.");break;
    case WSAEFAULT :AfxMessageBox("The nSockAddrLen argument is incorrect.");break;
    case WSAEINVAL :AfxMessageBox("Invalid host address.");break;
    case WSAEISCONN:AfxMessageBox("The socket is already connected.");break;
    case WSAEMFILE :AfxMessageBox("No more file descriptors are available.");break;
    case WSAENETUNREACH:AfxMessageBox("The network cannot be reached from this host at this time.");break;
    case WSAENOBUFS:AfxMessageBox("No buffer space is available. The socket cannot be connected.");
    case WSAENOTSOCK :AfxMessageBox("The descriptor is not a socket.");
    case WSAETIMEDOUT :AfxMessageBox(" Attempt to connect timed out without establishing a connection.");break;
    case WSAEWOULDBLOCK :AfxMessageBox("The socket is marked as nonblocking and the connection cannot be completed immediately.");break;
    }


    and in my client side----------------->
    1>simple dialog based application
    2>selected winsock control during wizard step.
    3>added the following code in its OnInitDialog()
    CSocket csClient;
    csClient.Create();
    //int k = csClient.Connect("jyoti.dev.mundu.com",2209);
    if(!(csClient.Connect("jyoti.dev.mundu.com",3000)))
    {
    MessageBox("Error in connection");
    }

    now tell me where i am going wrong
    i am not able to connect
    thanks vishal
    Last edited by VishalNikiSharma; December 19th, 2003 at 04:56 AM.

  9. #9
    Join Date
    Apr 2002
    Location
    Mumbai,India
    Posts
    567
    still waiting for the answer.........

  10. #10
    Join Date
    Jul 2003
    Posts
    72
    Do NOT use CSocket or any of Microsoft's wrapper classes. They are ****!

    Use the raw/bsd socket interface. Microsoft's **** implements these calls in there classes but its hidden from you.

    Search for send, recv, accept, listen etc.. there are millions of examles out there. Check codeguru and codeproject for example programs they are all over the place.

  11. #11
    Join Date
    Feb 2003
    Location
    Bangalore, India
    Posts
    1,354
    To use wrapper class or apis depends on the application and the designer. If it is just a simple small server then csock is enough. But when you are talking about scalabailty, performance and more control over what is happening then there is nothing better than using apis.

    [To the poster]
    Instead of using switch like that you can display the value and look the error using tools->error lookup. Display error no for each error handling. Your problem might be the address resolution. Try using ip address
    Even if our suggestions didn't help, please post the answer once you find it. We took the effort to help you, please return it to others.

    * While posting code sections please use CODE tags
    * Please check the codeguru FAQ and do a little search to see if your question have been answered before.
    * Like a post, Rate The Post
    * I blog: Network programming, Bible

    I do all things thru CHRIST who strengthens me

  12. #12
    Join Date
    Apr 2002
    Location
    Mumbai,India
    Posts
    567

    hi

    atlast i am able to connect my server with client using socket...
    but now how to pass data from client to server...
    i have seen many articles...but still not able to pass data...
    could somebody give a small snippet for this.....
    i have different app for client and server
    i tried receive and send
    though on the client side it shows that it have sent some number(for me it is 10),but at server ends it gives error
    why so?
    thansk
    vishal

  13. #13
    Join Date
    Feb 2003
    Location
    Bangalore, India
    Posts
    1,354
    what error? Look at my post...for every error that occurs you must handle it by getting the errorno using GetLastError(). Without mentioning the error that you got it is not possible to guess the solution.
    Even if our suggestions didn't help, please post the answer once you find it. We took the effort to help you, please return it to others.

    * While posting code sections please use CODE tags
    * Please check the codeguru FAQ and do a little search to see if your question have been answered before.
    * Like a post, Rate The Post
    * I blog: Network programming, Bible

    I do all things thru CHRIST who strengthens me

  14. #14
    Join Date
    Apr 2002
    Location
    Mumbai,India
    Posts
    567
    not i mean to say..
    from the client side the send command returns 10(these are number of bytes which i am sending.......
    but on the server side.....
    it shows that it received -1 bytes?
    very strange........
    please send me small code which pass data b/w server and client
    thanks
    vishal

  15. #15
    Join Date
    Feb 2003
    Location
    Bangalore, India
    Posts
    1,354
    For each send and recv (and for most of the socket ()) you should test for SOCKET_ERROR which is -1. If you get it, the errno should be retrived by using GetLastError().
    Even if our suggestions didn't help, please post the answer once you find it. We took the effort to help you, please return it to others.

    * While posting code sections please use CODE tags
    * Please check the codeguru FAQ and do a little search to see if your question have been answered before.
    * Like a post, Rate The Post
    * I blog: Network programming, Bible

    I do all things thru CHRIST who strengthens me

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured