-SIMPLE- C Server
Results 1 to 2 of 2

Thread: -SIMPLE- C Server

  1. #1
    Member
    Join Date
    Nov 2003
    Posts
    71

    -SIMPLE- C Server

    This is a simple little server program... my first socket program and pretty much my first program so don't bash me too badly on the formatting. You'll see that the program accepts input from the client and displays it in a message box... but what I don't understand is when I send the command quit... why does it not quit? Maybe I am doing something wrong with the while loops.. i don't know. If anyone could point me in the right direction that would be awesome. Thank you.



    #include <stdio.h>
    #include <winsock.h>
    #include <windows.h>
    #define PORT 5531

    int sockfd7, testa, sockfd8, newsock, sin_size, num_bytes;
    struct sockaddr_in localhost, client;
    char buf[50];

    int WINAPI
    WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
    {

    WSADATA wsda;
    WSAStartup (0x0101, &wsda);

    MessageBox(NULL, "Cannot Load Picture!", "Error!", 0);

    if((sockfd7 = socket(AF_INET, SOCK_STREAM, 0))<0)
    {
    exit(1);
    }

    localhost.sin_family =AF_INET;
    localhost.sin_port =htons(PORT);
    localhost.sin_addr.s_addr =htonl(INADDR_ANY);

    if((testa=bind(sockfd7, (struct sockaddr *) &localhost, sizeof(localhost)))<0)
    {
    closesocket(sockfd7);
    exit(1);
    }

    listen (sockfd7, 10);


    sin_size=sizeof(struct sockaddr_in);
    if((newsock = accept(sockfd7, (struct sockaddr *)&client, &sin_size))==-1){
    perror("accept error ");}

    while(buf!="quit")
    {
    if((num_bytes=recv(newsock, buf, 49, 0))==-1)
    {
    return 0;
    }
    buf[ num_bytes ] = '\0';
    MessageBox(NULL, buf, "Message", 0);
    }


    WSACleanup();
    closesocket(sockfd7);
    closesocket(newsock);
    return 0;

    }

  2. #2
    Senior Member
    Join Date
    Jul 2002
    Posts
    339
    Hi, I can't get your code to compile so I maybe wrong.

    But this line

    while(buf!="quit")

    did catch my eye.

    1. While it's syntactically correct, if you want to compare the string contained in variable buf with string "quit", it won't work. It will compare the address of variable buf with the address of string "quit", which is always different. To compare strings, use strcmp function. Try running this piece of code:

    #include <stdio.h>
    #include <string.h>

    int main()
    {
    char buf[10] = "quit";

    printf("buf = %s\n", buf);
    if (buf == "quit") printf("%s\n", "Using '==' : match");
    if (!strcmp(buf, "quit")) printf("%s\n", "Using 'strcmp' : match");
    }

    So you need to change that particular line to

    while (strcmp(buf, "quit"))

    2. I'm assuming you're using a telnet client to connect to your "server" at port 5531. I'm not so sure about this one. But I think when you type "quit" (w/o quotes) and press Enter, the client will send the string "quit" followed with the characters CR and LF (0x0d and 0x0a) to the "server".

    So you may need to concatenate "quit" with CRLF (using strcat) before comparing it with buf.

    Hope this helps.

    Peace always,
    <jdenny>
    Always listen to experts. They\'ll tell you what can\'t be done and why. Then go and do it. -- Robert Heinlein
    I\'m basically a very lazy person who likes to get credit for things other people actually do. -- Linus Torvalds


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

 Security News

     Patches

       Security Trends

         How-To

           Buying Guides