Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

socket connection

Status
Not open for further replies.

ceodav

Programmer
Feb 21, 2003
106
IT
Hi,

i need some information on how to build an application which wait on a port.
When a connection is established it has to accept the connection, receive a file,
execute some operations and send the results.


i need to use winsocket but i don't know how to do it.

Any suggestion or any example for VC++ 6?

thanks

Davide
 
well... sockets programming is a huge topic! try reading the following tutorials.. tehy are a bit long, but well worth it!

this is excellent.. although it says taht it is a tutorial for scokets programming in unix... even if you use windows, its a great tutorial... you won't find too many differences in teh basics.. the tutorial points out differences for coding in unix and windows.

another good tutorial.

basically, you have to call listen(....) to listen for incoming connections and accept(...) to accept onto a socket. but there are a TON of details you have to take care of before doing all that.. such as defining SOCKADDR_IN structures (where you can define the ports you want to use like you mentioned) and all that... don't worry if you don't understand every line of code in the tutorials.. just use the basic structure!

also, before you do any coding.. when you create a project, make sure you go to Project->Settings->Link tab. Find the text box there that says "Object/Library modules" (or something like that).. at teh end of the line of stuff that is already there type in wsock32.lib and save the settings. IF YOU DON'T DO THIS YOUR CODE WILL COMPILE, BUT YOU WILL GET LINKER ERRORS WHEN YOU TRY TO BUILD OR RUN THE PROGRAM!

hope this helps! good luck
 
Hi,

thanks for your help.

Actually i found in internet something about create, Listen and so on but i'm stuck on catching events.

When Server is in Listen mode it has to wait for a connection and then accept. Client and Server should then communicate by means of Send and Receive commands.

But how is possible to catch them?

Have I to build a class derived from CAsyncSocket?

In fact on some example i saw they use the methods OnAccept, OnSend but from now i cannot use them.

Thanks again

Davide
 
I have never used CAsyncSocket.. i would suggest that you just use plain and simple winsock... i have heard that Cwhatever scoket classes are not very good!

i dont' know what you mean by "catching events".

server code should look something like this:

Code:
	SOCKET listeningSocket;
	SOCKADDR_IN sinfo;
	listeningSocket=socket(AF_INET,               // Go over TCP/IP
                           SOCK_STREAM,           // This is a stream-oriented socket
                           IPPROTO_TCP);          // Use TCP rather than UDP

	sinfo.sin_family = AF_INET;
    sinfo.sin_addr.s_addr = INADDR_ANY;       // Since this socket is listeningfor connections,
                                              // any local address will do
    sinfo.sin_port = htons(listeningport);          // Convert integer 8888 tonetwork-byte order
                                              // and insert into the port field

	int nret = bind(listeningSocket, (LPSOCKADDR)&sinfo, sizeof(struct
				sockaddr));
	if (nret == SOCKET_ERROR){
			cout<<"PRINTERROR(bind())"<<endl;;
			closesocket(listeningSocket);
		
	}

	while (true){
		
		nret = listen(listeningSocket, 10);
		if (nret!=SOCKET_ERROR){
			
				SOCKET s= accept(listeningSocket,
						NULL,                        
						NULL);
			
			
			if (s==INVALID_SOCKET){
				cout<<"There was an error on accept"<<endl;
			}
			else{
				cout<<"Connection established"<<endl;
//you will probably want to start a thread here.
//close all the braces appropriately..
 
eh.. sorry.. some of the comments went over to the next line without //... i'm sure you can figure out where that happened!
 
and i forgot to mention something else! i wish this thing would let me edit my posts!

CSOCKET type socket classes are used with MFC. but it is not absolutely necessary to use them! you can easily just use plain old winsock. i do not know much about these... and i can't help you with them much... i have heard they are not very good/reliable.... but i've never used them myself.. so don't hold me to it! if you just want to do basic stuff, they should be ok i guess.. and if you fnid them easier to use, go ahead! i guess i'm a little bit biased :p

i also forgot to mention that you have to actually start winsock before any of that code will work. also, the code i posted will not compile because i just coiped and pasted it from a project i'm working on. you will have to change teh variables that are not defined in teh above to your own variables.. should be easy enough to do.

code to start winsock 1:

Code:
////////////START WINSOCK///////////////////////////////
	WORD wVersionRequested = MAKEWORD(1,1);
	WSADATA wsaData;
	int nRet = WSAStartup(wVersionRequested, &wsaData);
	if (wsaData.wVersion != wVersionRequested)
	{
		cout<<"wrong version"<<endl;
		return 1;
	}
/////////////////////////////////////////////////////////

this start up code will work. there is no reason it should not compile. just make sure to make the modification i mentioned in the VC++ settings.

hope this helps.. and sorry for posting so many times! would be easier if i could edit my posts!
 
hi!

i have already implemented something very simple with CAsyncSocket but i think i will try with your examples since i'm in the beginning of this job.


many thanks for your help it was very useful.

bye

Davide
 
drewdaman:
Code:
 while (true){
        
        nret = listen(listeningSocket, 10);
        if (nret!=SOCKET_ERROR){
            
                SOCKET s= accept(listeningSocket,
                        NULL,                        
                        NULL);

Are you sure, listen() should be looped? I think it should be called once after socket initialization and after bind(). Only accept() should be inside of the loop, am I right?
 
good point..

but the code i posted works just fine- in that i can establsih mutliple connections. i don't see why it wouldn't work the way you're saying ... in fact, your way might be better... but my way works too :p! in fact, i had intended to put that outside the loop, but somehow i didn't! what might happen with my code is that teh "10" in the parameters for listen would be useless.. the listening socket would be able to accept 10 connections on every iteration of the loop.. i didn't test my code with that many connections nad never ran into that problem. thanks for pointing that out mingis!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top