Socket client....
************************Start************************
// EventSocketClient.cpp : Defines the entry point for
the console application.
//

#include <winsock2.h>


#define DEFAULT_PORT            5150
#define DEFAULT_COUNT           25
#define DEFAULT_CHAR            'a'
#define DEFAULT_BUFFER_LENGTH   64
#define DEFAULT_BUFFER  256
#define SERVER_SIZE 25

DWORD WINAPI ClientThread(LPVOID lpParam);

int main(int argc, char* argv[])
{
        WSADATA       wsd;
    SOCKET        sClient;
   // char          szBuffer[DEFAULT_BUFFER];

//      char szBuffer[256];
        char szServer[SERVER_SIZE];
//    int           ret;

    struct sockaddr_in server;
    struct hostent    *host = NULL;
        //Each connect creates new thread and pass client
socket as paramter to thread function.This variable is
Id of the new thread.
        DWORD dwClientSocketThreadId;
        //Each connect creates new thread and pass client
socket as paramter to thread function.This variable is
the handle of new thread.
        HANDLE  hClientSocketThread = NULL;

        //Load socket library
    if (WSAStartup(MAKEWORD(2,2), &wsd) != 0)
    {
        printf("Failed to load Winsock library!\n");
        return 1;
    }
    //strcpy(szMessage, "Hi\0");
        //strcpy(szServer,"127.0.0.1");
        strcpy(szServer,"127.0.0.1");
    //
    // Create the socket, and attempt to connect to
the server
    //
    sClient = socket(AF_INET, SOCK_STREAM, 0);
    if (sClient == INVALID_SOCKET)
    {
        printf("socket() failed: %d\n",
WSAGetLastError());
        return 1;
    }
    server.sin_family = AF_INET;
    server.sin_port = htons(DEFAULT_PORT);
    server.sin_addr.s_addr = inet_addr(szServer);
    //
    // If the supplied server address wasn�t in the
form 
    // �aaa.bbb.ccc.ddd,� it�s a host name, so try to
resolve it
    //
    if (server.sin_addr.s_addr == INADDR_NONE)
    {
        host = gethostbyname(szServer);
        if (host == NULL)
        {
            printf("Unable to resolve server: %s\n",
szServer);
            return 1;
        }
        CopyMemory(&server.sin_addr,
host->h_addr_list[0],
            host->h_length);
    }
    if (connect(sClient, (struct sockaddr *)&server, 
        sizeof(server)) == SOCKET_ERROR)
    {
        printf("connect() failed: %d\n",
WSAGetLastError());
        return 1;
    }

        //Create new thread  and pass Client socket to this
thread....Any further events on this client socket
will be taken care in this thread.
        hClientSocketThread = CreateThread(NULL, 0,
ClientThread, 
                  (LPVOID)sClient, 0,
&dwClientSocketThreadId);
        if (hClientSocketThread == NULL)
        {
                printf("CreateThread() failed: %d\n",
GetLastError());
        }

        Sleep(100000);
        return 0;
}

/*
Thread Function
*/
DWORD WINAPI ClientThread(LPVOID lpParam)
{
        //Client Socket ...Socket through which server and
client will talk.
    SOCKET        sock=(SOCKET)lpParam;
        //Buffer send to cliet
    char          szBuff[256];
        char          szBuffTest[356] =
"utuatduatgduaidtasiudtasidtasiudtsaiudtgsaiudtsadigasdigasiudgysaiudgusagdysagdsaygdsauhdgasjghdasjkdhgasjkdgasjdgsajkdgasjgdsajkgdasjkgdasjkgdjsakgdkjasgdjsakgdsajkgdajskgdsakjgdsajkgdsakjgddsjgdaajskgdjkasgdkjasgdkjasgdjaksgdajskgdjkasgdkjsagdaskjgdaskjgdsakjdgaskjdgsa";
    int           ret;
        int nReturnCode;
        WSAEVENT hEventClientThread = WSA_INVALID_EVENT;
        hEventClientThread = WSACreateEvent();

        
        ret = send(sock,szBuffTest , 356, 0);
        ::WSAEventSelect(sock, hEventClientThread, FD_READ );

    while(1)
    {
        // Perform a blocking recv() call
        //
                
                nReturnCode = ::WSAWaitForMultipleEvents(1,
&hEventClientThread, 
                FALSE, INFINITE, FALSE); 

                WSANETWORKEVENTS wsaConnectEvents ;

                ::WSAEnumNetworkEvents(sock, hEventClientThread,
&wsaConnectEvents);

                if(
                //checks to see if this was the event that occurred
                (wsaConnectEvents.lNetworkEvents & FD_READ) &&  
                //ensures that there was no error
           (wsaConnectEvents.iErrorCode[FD_READ_BIT] == 0) )    
                {




                        ret = recv(sock, szBuff, 256, 0);
                        if (ret == 0)        // Graceful close
                                break;
                        else if (ret == SOCKET_ERROR)
                        {
                                printf("recv() failed: %d\n", WSAGetLastError());
                                break;
                        }
                        szBuff[ret] = '\0';
                        printf("Data received from Server: �%s�\n",
szBuff);

                        printf( "Input messages you want to send to server
" );
                        memset(szBuff,'\0',256);
                        gets( szBuff );
                        strcat(szBuff,"\0");
                        if(strcmpi(szBuff,"no"))
                        {
                                ret = send(sock,&szBuff[0] , strlen(szBuff), 0);
                        }
                        
                }
    }
    return 0;
}
*******************Socket Client End****************


***********************Socket Server Start***********
// EventSocketServer.cpp : Defines the entry point for
the console application.
//


#include <winsock2.h>
#define DEFAULT_PORT            5150
DWORD WINAPI ClientThread(LPVOID lpParam);

int main(int argc, char* argv[])
{
        //Required for loading socket library
        WSADATA       wsd;
        //Socket
        SOCKET         s,Client;
        //address required for binding ..required for binding
sercer socket
        struct sockaddr_in   tcpaddr;
        //When any client connect to server socket then
client address information comes in this variable
        struct sockaddr_in client;
        //Flag required in Connect functions ..it holds the
size of client address
        int iAddrSize ;
        //Each connect creates new thread and pass client
socket as paramter to thread function.This variable is
Id of the new thread.
        DWORD dwClientSocketThreadId;
        //Each connect creates new thread and pass client
socket as paramter to thread function.This variable is
the handle of new thread.
        HANDLE  hClientSocketThread = NULL;
        //Socket Event Handle
        WSAEVENT hEvent = WSA_INVALID_EVENT;
        //This contains information about the event that
occured on server socket
        WSANETWORKEVENTS wsaConnectEvents ;

        
        //Load Winsock library.Its must call for any Winsock
program
        if (WSAStartup(MAKEWORD(2,2), &wsd) != 0)
    {
        printf("Failed to load Winsock library!\n");
        return 1;
    }
        
        //Create socket event handle
        hEvent = WSACreateEvent();
        
        // Create the Stream type socket 
    s = socket(AF_INET, SOCK_STREAM, 0);
        if (s == INVALID_SOCKET)
    {
        printf("socket() failed; %d\n",
WSAGetLastError());
        return 1;
    }
        //bind this server socket
        tcpaddr.sin_family = AF_INET;
        //Assign port number on which server socket will be
listening
        tcpaddr.sin_port = htons(DEFAULT_PORT);    
        tcpaddr.sin_addr.s_addr = htonl(INADDR_ANY);
        if(bind(s, (SOCKADDR *)&tcpaddr, sizeof(tcpaddr)) ==
SOCKET_ERROR)
        {

                printf("Couldn't bind\n");
        }

        //listen server socket ...set maximum number of
connection that server can accept
        listen(s, SOMAXCONN);
        
        //Now Select event Correspondin to this
socket...after this if this event(ACCEPT) occurs on
this socket then wake up...like if some one tries to
connect to this socket then it will wake up.
        //At a time multiple events can be  bind to a
particular socket but binding for any event requires
new event handle
        ::WSAEventSelect(s, hEvent, FD_ACCEPT); 
        while(1)
        {
                //Wait against this event handle ... 1 becasue
currently only one event handle is bind to socket. ...
in case of multiple events pass arraye of events
handle
                int nReturnCode = ::WSAWaitForMultipleEvents(1,
&hEvent, 
                FALSE, INFINITE, FALSE); 
                //Wake up ...now find out what event occured server
socket
                ::WSAEnumNetworkEvents(s, hEvent,
&wsaConnectEvents);

                if(
                //checks to see if this was the event that occurred
                (wsaConnectEvents.lNetworkEvents & FD_ACCEPT) &&        
                //ensures that there was no error
           (wsaConnectEvents.iErrorCode[FD_ACCEPT_BIT] == 0)
)       
                {
                        /*
                        ....
                        //Handle Processing here
                        ....
                         */
                        iAddrSize = sizeof(client);
                        //Accpet the call made by client
                        Client = accept(s, (struct sockaddr *)&client,
                                                        &iAddrSize);   
                        if (Client == INVALID_SOCKET)
                        {        
                                printf("accept() failed: %d\n",
WSAGetLastError());
                        }
                        else
                        {
                                //Create new thread  and pass Client socket to
this thread....Any further events on this client
socket will be taken care in this thread.
                                hClientSocketThread = CreateThread(NULL, 0,
ClientThread, 
                    (LPVOID)Client, 0,
&dwClientSocketThreadId);
                                if (hClientSocketThread == NULL)
                                {
                                        printf("CreateThread() failed: %d\n",
GetLastError());
                                }

                        }
                }
        }

        return 0;
}

/*
Thread Function
*/
DWORD WINAPI ClientThread(LPVOID lpParam)
{
        //Client Socket ...Socket through which server and
client will talk.
    SOCKET        sock=(SOCKET)lpParam;
        //Buffer send to cliet
    char          szBuff[256];
    int           ret;
        WSAEVENT hEventClientThread = WSA_INVALID_EVENT;
        hEventClientThread = WSACreateEvent();




    while(1)
    {
        // Perform a blocking recv() call
        //
                ::WSAEventSelect(sock, hEventClientThread, FD_READ
);
                int nReturnCode = ::WSAWaitForMultipleEvents(1,
&hEventClientThread, 
                FALSE, INFINITE, FALSE); 

                WSANETWORKEVENTS wsaConnectEvents ;

                ::WSAEnumNetworkEvents(sock, hEventClientThread,
&wsaConnectEvents);

                if(
                //checks to see if this was the event that occurred
                (wsaConnectEvents.lNetworkEvents & FD_READ) &&  
                //ensures that there was no error
           (wsaConnectEvents.iErrorCode[FD_READ_BIT] == 0) )    
                {



                        ret = recv(sock, szBuff, 256, 0);
                        if (ret == 0)        // Graceful close
                                break;
                        else if (ret == SOCKET_ERROR)
                        {
                                printf("recv() failed: %d\n", WSAGetLastError());
                                break;
                        }
                        szBuff[ret] = '\0';
                        printf("Data received from client: �%s�\n",
szBuff);
                        printf( "Input messages you want to send to Client
" );
                        memset(szBuff,'\0',256);
                        gets( szBuff );
                        strcat(szBuff,"\0");
                        if(strcmpi(szBuff,"no"))
                        {
                                        //strcpy(szBuff,"from server");
                                ret = send(sock,&szBuff[0] , strlen(szBuff), 0);
                        }
                }
    }
    return 0;
}


**************************Socket Server End***********

regds
vinay
--- Nick Hird <[EMAIL PROTECTED]> wrote:

> 
> Does anyone know a good place to get examples and
> tutorials for
> creating socket connections and listeners? I am
> still sorta new to
> C++, but i guess i'll jump right in and see what i
> can do. Thanks!
> -Nick
> 
> 
> 
> 



                
__________________________________ 
Do you Yahoo!? 
Check out the new Yahoo! Front Page. 
www.yahoo.com 
 






------------------------ Yahoo! Groups Sponsor --------------------~--> 
Make a clean sweep of pop-up ads. Yahoo! Companion Toolbar.
Now with Pop-Up Blocker. Get it for free!
http://us.click.yahoo.com/L5YrjA/eSIIAA/yQLSAA/EbFolB/TM
--------------------------------------------------------------------~-> 

To unsubscribe : [EMAIL PROTECTED]

 
Yahoo! Groups Links

<*> To reply to this message, go to:
    http://groups.yahoo.com/group/Programmers-Town/post?act=reply&messageNum=3771
    Please do not reply to this message via email. More information here:
    http://help.yahoo.com/help/us/groups/messages/messages-23.html

<*> To visit your group on the web, go to:  
    http://groups.yahoo.com/group/Programmers-Town/

<*> To unsubscribe from this group, send an email to:
    [EMAIL PROTECTED]

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.com/info/terms/
 



Reply via email to