-
December 18th, 2003, 11:42 PM
#1
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
-
December 19th, 2003, 12:18 AM
#2
Please read MSDN help, there are mountain of articles and sampels about socket programming.
Quang
-
December 19th, 2003, 12:36 AM
#3
-
December 19th, 2003, 01:09 AM
#4
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
-
December 19th, 2003, 03:15 AM
#5
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
-
December 19th, 2003, 03:22 AM
#6
-
December 19th, 2003, 03:41 AM
#7
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
-
December 19th, 2003, 04:53 AM
#8
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.
-
December 19th, 2003, 07:13 AM
#9
still waiting for the answer.........
-
December 19th, 2003, 07:35 AM
#10
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.
-
December 20th, 2003, 12:48 AM
#11
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
-
December 20th, 2003, 06:25 AM
#12
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
-
December 20th, 2003, 06:43 AM
#13
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
-
December 20th, 2003, 06:53 AM
#14
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
-
December 20th, 2003, 07:00 AM
#15
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|