Help understanding a code

This is a discussion on Help understanding a code within the Networking/Device Communication forums, part of the General Programming Boards category; Hello there something i dont understand in johnni winsock tutrial it says accept() here in this code get info about ...

  1. #1
    Registered User
    Join Date
    Dec 2008
    Posts
    183

    Help understanding a code

    Hello there something i dont understand in johnni winsock tutrial it says accept() here in this code get info about the user but on msdn it says it just accept the connection please help me I just wanna know what accept does in this code
    Code:
    #include <windows.h>
    #include <winsock.h>
    #include <stdio.h>
    
    #define NETWORK_ERROR -1
    #define NETWORK_OK     0
    
    void ReportError(int, const char *);
    
    
    int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR lpCmd, int nShow)
    {
    	WORD sockVersion;
    	WSADATA wsaData;
    	int nret;
    
    	sockVersion = MAKEWORD(1, 1);			// We'd like Winsock version 1.1
    
    
    	// We begin by initializing Winsock
    	WSAStartup(sockVersion, &wsaData);
    
    
    	// Next, create the listening socket
    	SOCKET listeningSocket;
    
    	listeningSocket = socket(AF_INET,		// Go over TCP/IP
    			         SOCK_STREAM,   	// This is a stream-oriented socket
    				 IPPROTO_TCP);		// Use TCP rather than UDP
    
    	if (listeningSocket == INVALID_SOCKET)
    	{
    		nret = WSAGetLastError();		// Get a more detailed error
    		ReportError(nret, "socket()");		// Report the error with our custom function
    
    		WSACleanup();				// Shutdown Winsock
    		return NETWORK_ERROR;			// Return an error value
    	}
    
    
    	// Use a SOCKADDR_IN struct to fill in address information
    	SOCKADDR_IN serverInfo;
    
    	serverInfo.sin_family = AF_INET;
    	serverInfo.sin_addr.s_addr = INADDR_ANY;	// Since this socket is listening for connections,
    							// any local address will do
    	serverInfo.sin_port = htons(8888);		// Convert integer 8888 to network-byte order
    							// and insert into the port field
    
    
    	// Bind the socket to our local server address
    	nret = bind(listeningSocket, (LPSOCKADDR)&serverInfo, sizeof(struct sockaddr));
    
    	if (nret == SOCKET_ERROR)
    	{
    		nret = WSAGetLastError();
    		ReportError(nret, "bind()");
    
    		WSACleanup();
    		return NETWORK_ERROR;
    	}
    
    
    	// Make the socket listen
    	nret = listen(listeningSocket, 10);		// Up to 10 connections may wait at any
    							// one time to be accept()'ed
    
    	if (nret == SOCKET_ERROR)
    	{
    		nret = WSAGetLastError();
    		ReportError(nret, "listen()");
    
    		WSACleanup();
    		return NETWORK_ERROR;
    	}
    
    
    	// Wait for a client
    	SOCKET theClient;
    
    	theClient = accept(listeningSocket,
    			   NULL,			// Optionally, address of a SOCKADDR_IN struct
    			   NULL);			// Optionally, address of variable containing
    							// sizeof ( struct SOCKADDR_IN )
    
    	if (theClient == INVALID_SOCKET)
    	{
    		nret = WSAGetLastError();
    		ReportError(nret, "accept()");
    
    		WSACleanup();
    		return NETWORK_ERROR;
    	}
    
    
    	// Send and receive from the client, and finally,
    	closesocket(theClient);
    	closesocket(listeningSocket);
    
    
    	// Shutdown Winsock
    	WSACleanup();
    	return NETWORK_OK;
    }
    
    
    void ReportError(int errorCode, const char *whichFunc)
    {
       char errorMsg[92];					// Declare a buffer to hold
    							// the generated error message
       
       ZeroMemory(errorMsg, 92);				// Automatically NULL-terminate the string
    
       // The following line copies the phrase, whichFunc string, and integer errorCode into the buffer
       sprintf(errorMsg, "Call to %s returned error %d!", (char *)whichFunc, errorCode);
    
       MessageBox(NULL, errorMsg, "socketIndication", MB_OK);
    }

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,471
    accept accepts the connection AND returns handle to the created socket

    this handle will be used to send/receive data to/from connected client, so you can call it "user info" if you are trying to explain sockets in "baby talk"
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    Registered User
    Join Date
    Dec 2008
    Posts
    183
    yah i m still starting to understand some concepts in winsocks anyways thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 30
    Last Post: 06-19-2006, 12:35 AM
  2. Replies: 1
    Last Post: 03-21-2006, 06:52 AM
  3. Updated sound engine code
    By VirtualAce in forum Game Programming
    Replies: 8
    Last Post: 11-18-2004, 11:38 AM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21