Click here to Skip to main content
15,921,622 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: Process Creation Notification Pin
_Magnus_23-Apr-04 3:53
_Magnus_23-Apr-04 3:53 
GeneralLightweight HTML rendering Pin
antareus23-Apr-04 2:55
antareus23-Apr-04 2:55 
GeneralRe: Lightweight HTML rendering Pin
Garth J Lancaster23-Apr-04 16:22
professionalGarth J Lancaster23-Apr-04 16:22 
GeneralSend to txctt and mailMonty Pin
ChuThaiDuong23-Apr-04 2:25
ChuThaiDuong23-Apr-04 2:25 
GeneralRe: Send to txctt and mailMonty Pin
jmkhael23-Apr-04 2:27
jmkhael23-Apr-04 2:27 
QuestionHow to write a simple socket application(HELP) Pin
Nodoordonotthereisnotry23-Apr-04 2:24
Nodoordonotthereisnotry23-Apr-04 2:24 
AnswerRe: How to write a simple socket application(HELP) Pin
jmkhael23-Apr-04 2:28
jmkhael23-Apr-04 2:28 
AnswerRe: How to write a simple socket application (long reply) Pin
David Crow23-Apr-04 4:02
David Crow23-Apr-04 4:02 
Here is some server code that listens on port 13:

void main( void )
{
    WSADATA wsaData;
    int rVal;
    SOCKET client;
 
    WSAStartup(MAKEWORD(1, 1), &wsaData);
 
    //create socket
    SOCKET s = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
 
    if (s == INVALID_SOCKET)
    {
        fprintf(stderr, "Failed socket()\n");
        WSACleanup();
        return;
    }
 
    SOCKADDR_IN sin, clientaddr;
    sin.sin_family = PF_INET;
    sin.sin_port = htons(13);
    sin.sin_addr.s_addr = INADDR_ANY;
 
    //bind the socket
    rVal = bind(s, (LPSOCKADDR) &sin, sizeof(sin));
    if (rVal == SOCKET_ERROR)
    {
        fprintf(stderr, "Failed bind()\n");
        WSACleanup();
        return;
    }
 
    //get socket to listen 
    rVal = listen(s, 2);
    if (rVal == SOCKET_ERROR)
    {
        fprintf(stderr, "Failed listen()\n");
        WSACleanup();
        return;
    }
 
    for (;; )
    {
        int addrlen = sizeof(clientaddr);
 
        //wait for a client
        client = accept(s, (struct sockaddr *) &clientaddr, &addrlen);
 
        if (client < 0)
            continue;
 
        char *clienthost = inet_ntoa(clientaddr.sin_addr);
        int port = ntohs(clientaddr.sin_port);
 
        fprintf(stderr, "Received request from [%s] on port [%d]\n", clienthost, port);
 
        rVal = send(client, "Have a nice day!", 16, 0);
 
        closesocket(client);
    }
    
    closesocket(s);
}
Here is some client code that requests data from port 13:

void main( void )
{
    WSADATA     rWSAData;
    int         nIndex = 0,
                nResult,
                nBytes;
    SOCKET      rSocket;
    SOCKADDR_IN rSocketAddr;
    char        sBuffer[128];
 
    if (WSAStartup(MAKEWORD(1, 1), &rWSAData) == 0)
    {
        rSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
        if (INVALID_SOCKET != rSocket)
        {
            unsigned long ul = 1;
            nResult = ioctlsocket(rSocket, FIONBIO, (unsigned long *) &ul);
 
            rSocketAddr.sin_family      = AF_INET;
            rSocketAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
            rSocketAddr.sin_port        = htons(13);
 
            nResult = connect(rSocket, (LPSOCKADDR) &rSocketAddr, sizeof(rSocketAddr));
            if (SOCKET_ERROR == nResult && WSAGetLastError() == WSAEWOULDBLOCK)
            {
                timeval tv = {0};
                tv.tv_sec = 5;
                fd_set fdread;
                FD_ZERO(&fdread);
                FD_SET(rSocket, &fdread);
                nResult = select(0, &fdread, NULL, NULL, &tv);
                if (nResult > 0)
                {
                    if (FD_ISSET(rSocket, &fdread) != 0)
                    {
                        nBytes = recv(rSocket, sBuffer, sizeof(sBuffer), 0);
                        if (nBytes > 0)
                        {
                            sBuffer[nBytes] = '\0';
 
                            fprintf(stderr, "Data from %s is ]%s[", inet_ntoa(rSocketAddr.sin_addr), sBuffer);
                        }
                        else
                            fprintf(stderr, "recv() failed.  Error = %lu\n", WSAGetLastError());
                    }
                    else
                        fprintf(stderr, "FD_ISSET() failed.\n");
                }
                else
                {
                    if (0 == nResult)
                        fprintf(stderr, "select() failed.  Timeout\n");
                    else
                        fprintf(stderr, "select() failed.  Error = %lu\n", WSAGetLastError());
                }
            }
            else
                fprintf(stderr, "connect() failed.  Error = %lu\n", WSAGetLastError());
 
            if (closesocket(rSocket) == SOCKET_ERROR)
                fprintf(stderr, "closesocket() failed.  Error = %lu\n", WSAGetLastError());
        }
        else
            fprintf(stderr, "socket() failed.  Error = %lu\n", WSAGetLastError());
 
        nIndex++;
 
        if (WSACleanup() == SOCKET_ERROR)
            fprintf(stderr, "WSACleanup() failed.  Error = %lu\n", WSAGetLastError());
    }
    else
        fprintf(stderr, "WSAStartup() failed.  Error = %lu\n", WSAGetLastError());
}



"The pointy end goes in the other man." - Antonio Banderas (Zorro, 1998)


AnswerRe: How to write a simple socket application(HELP) Pin
Mike Dimmick23-Apr-04 5:59
Mike Dimmick23-Apr-04 5:59 
GeneralRe: How to write a simple socket application(HELP) Pin
Nodoordonotthereisnotry25-Apr-04 20:43
Nodoordonotthereisnotry25-Apr-04 20:43 
Generaldetecting CTRL+C Pin
Anonymous23-Apr-04 1:41
Anonymous23-Apr-04 1:41 
GeneralRe: detecting CTRL+C Pin
Mike Dimmick23-Apr-04 1:47
Mike Dimmick23-Apr-04 1:47 
QuestionHow create a member variable of type (a class) Pin
ChuThaiDuong23-Apr-04 1:36
ChuThaiDuong23-Apr-04 1:36 
AnswerRe: How create a member variable of type (a class) Pin
Monty223-Apr-04 2:02
Monty223-Apr-04 2:02 
AnswerRe: How create a member variable of type (a class) Pin
toxcct23-Apr-04 2:06
toxcct23-Apr-04 2:06 
AnswerRe: How create a member variable of type (a class) Pin
ChuThaiDuong23-Apr-04 2:23
ChuThaiDuong23-Apr-04 2:23 
GeneralRe: How create a member variable of type (a class) Pin
toxcct23-Apr-04 2:35
toxcct23-Apr-04 2:35 
GeneralRe: How create a member variable of type (a class) Pin
toxcct23-Apr-04 2:39
toxcct23-Apr-04 2:39 
GeneralRe: How create a member variable of type (a class) Pin
Alton Williams23-Apr-04 4:39
Alton Williams23-Apr-04 4:39 
GeneralRe: How create a member variable of type (a class) Pin
ChuThaiDuong23-Apr-04 15:06
ChuThaiDuong23-Apr-04 15:06 
GeneralCustomizing Scrollbar of clistctrl Pin
rajandpayal23-Apr-04 1:29
rajandpayal23-Apr-04 1:29 
GeneralRe: Customizing Scrollbar of clistctrl Pin
ohadp23-Apr-04 2:25
ohadp23-Apr-04 2:25 
GeneralRe: Customizing Scrollbar of clistctrl Pin
rajandpayal24-Apr-04 3:42
rajandpayal24-Apr-04 3:42 
GeneralRe: Customizing Scrollbar of clistctrl Pin
ohadp24-Apr-04 19:08
ohadp24-Apr-04 19:08 
QuestionTwo overlapping windows, one flickers, howto solve ? Pin
ohadp23-Apr-04 1:16
ohadp23-Apr-04 1:16 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.