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!

C++ EXPLAINED - NEW PROGRAMMER

Status
Not open for further replies.

luke99

MIS
Dec 20, 2002
8
GB
Hi Guys!

I return to the best forum ever, if i need help, now is the time! Need some help with understanding these code line in a simple server-client program that demonstrates what's going on. I have a general idea, but there are few command that i am not entirely sure why they are there and what EXCATLT they do! Could someone spare some time and explain these to me? Thanks. Luke

Here we go:

1. hSock=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);

2. result = connect(hSock,(struct sockaddr*)&adr,sizeof(addr));

3. write(hSock,buffer,nSent);

4. result = close(hSock)

5. hServerSocket=socket(AF_INET,SOCK_STREAM,IPROTO_TCP);

6. result = bind(hServerSocket,(struct sockaddr*)&Address,sizeof(Address));

7. result = listen(hServerSocket,SIZEQUEUE);

8. hSocket=accept(hServerSocket,(struct sockaddr*)&Address,&nAddressSize);

9. read(hSocket,pBuffer,SIZEBUFFER);

10. write(hSocket,pBuffer,strlen(pBuffer)+1);

11. if(close(hSocket) == ERROR) {
printf(“\nCould not close socket\n”);
return 0;
}

that's it.

Thanks a lot
 
1.) The socket function creates a socket that is bound to a specific service provider. AF_INET is the Address family, SOCK_STREAM is the Type of the socket and IPPROTO_TCP is the protocol used by the socket.

2.) The connect function establishes a connection to a specified socket. hSock is the socket created in #1,(struct sockaddr*)&adr is the name of the socket you are connecting to, sizeof(addr) is just the size of the name you are connecting to.

Don't have anymore time now, you can find all of these on msdn.microsoft.com and see what each function does. Hope this helps.
 
Server socket is the socket which listen for clients connection. For waiting clients connections you call accept. Function listen initializes a serevr socket, so it could accept backlog number of clients at the same time.

Client use connect. When it connects, on the server and on the client side is created a socket per side. So this socket is used to communicate. So is possible to read from, and to write in sockets. What is written on client side is read on server side and what is written on server side, is read on the client side. A good idea is to create on serevr side a new thread per each new connection and to enter an infinite "while(read.." loop while is ok for reading.

Ion Filipski
1c.bmp
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top