Thread: simple hello world SERVER/CLIENT

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    5

    simple hello world SERVER/CLIENT

    im trying to send the string "hello world"
    from one program to the other - via loopback
    but the client is printing garbage...
    please help!

    ps: conio.h and getch() are just to hold the window open in borland compiler

    CLIENT
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include<winsock.h>
    #include<conio.h>
    
    #define MAXRCVLEN 500
    #define PORTNUM 2343
    
    int main(int argc, char *argv[])
    {
        char buffer[MAXRCVLEN + 1]; /* +1 so we can add null terminator */
        int len, mysocket;
        struct sockaddr_in dest; 
    
        mysocket = socket(AF_INET, SOCK_STREAM, 0);
     
        memset(&dest, 0, sizeof(dest));                /* zero the struct */
        dest.sin_family = AF_INET;
        dest.sin_addr.s_addr = inet_addr("127.0.0.1"); /* set destination IP number */ 
        dest.sin_port = htons(PORTNUM);                /* set destination port number */
    
        connect(mysocket, (struct sockaddr *)&dest, sizeof(struct sockaddr));
     
        len = recv(mysocket, buffer, MAXRCVLEN, 0);
    
        /* We have to null terminate the received data ourselves */
        buffer[len] = '\0';
    
        printf("Received %s (%d bytes).\n", buffer, len);
    
        closesocket(mysocket);
        getch();
        return EXIT_SUCCESS;
    }
    SERVER
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include<winsock.h>
    
    #define PORTNUM 2343
    
    int main(int argc, char *argv[])
    {
        char msg[15] = "Hello World !\n";
     
        struct sockaddr_in dest; /* socket info about the machine connecting to us */
        struct sockaddr_in serv; /* socket info about our server */
        int mysocket;            /* socket used to listen for incoming connections */
        int socksize = sizeof(struct sockaddr_in);
     
        memset(&dest, 0, sizeof(dest));    /* zero the struct before filling the fields */
        serv.sin_family = AF_INET;         /* set the type of connection to TCP/IP */
        serv.sin_addr.s_addr = INADDR_ANY; /* set our address to any interface */
        serv.sin_port = htons(PORTNUM);    /* set the server port number */    
     
        mysocket = socket(AF_INET, SOCK_STREAM, 0);
     
        /* bind serv information to mysocket */
        bind(mysocket, (struct sockaddr *)&serv, sizeof(struct sockaddr));
     
        /* start listening, allowing a queue of up to 1 pending connection */
        listen(mysocket, 1);
        int consocket = accept(mysocket, (struct sockaddr *)&dest, &socksize);
     
        while(consocket)
        {
            
     
            printf("Incoming connection from %s - sending welcome\n", inet_ntoa(dest.sin_addr));
            send(consocket, msg, strlen(msg), 0);
     
            
        }
        closesocket(consocket);
        closesocket(mysocket);
        return EXIT_SUCCESS;
    }
    Last edited by goonmaster; 11-25-2009 at 06:15 PM.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Did you have a question in your post some place?


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Nov 2009
    Posts
    5
    Quote Originally Posted by quzah View Post
    Did you have a question in your post some place?


    Quzah.

    at the top
    the bit that says

    im trying to sent the string "hello world" from one program to the other via loopback but the client is printing garbage...
    please help!

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You mean the part that you edited in there? What sort of "garbage"? Does the string make it there at all? Is it just random info? What are you getting for len? You might consider actually checking the return values of all of those handy functions...


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Nov 2009
    Posts
    5
    Quote Originally Posted by quzah View Post
    You mean the part that you edited in there? What sort of "garbage"? Does the string make it there at all? Is it just random info? What are you getting for len? You might consider actually checking the return values of all of those handy functions...


    Quzah.
    it was edited for you honey haha

    its just random ........e that changes every time i run the client
    i got it off a tutorial and modified it for windows:
    C Programming/Networking in UNIX - Wikibooks, collection of open-content textbooks

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    So start checking the return values of your functions against what they're supposed to return to see where it's failing.


    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User
    Join Date
    Nov 2009
    Posts
    5
    is anyone out there able to help?

  8. #8
    Registered User
    Join Date
    Jan 2009
    Posts
    103
    Quote Originally Posted by goonmaster View Post
    im trying to send the string "hello world"
    from one program to the other - via loopback
    but the client is printing garbage...
    please help!

    ps: conio.h and getch() are just to hold the window open in borland compiler

    CLIENT
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include<winsock.h>
    #include<conio.h>
    
    #define MAXRCVLEN 500
    #define PORTNUM 2343
    
    int main(int argc, char *argv[])
    {
        char buffer[MAXRCVLEN + 1]; /* +1 so we can add null terminator */
        int len, mysocket;
        struct sockaddr_in dest; 
    
        mysocket = socket(AF_INET, SOCK_STREAM, 0);
     
        memset(&dest, 0, sizeof(dest));                /* zero the struct */
        dest.sin_family = AF_INET;
        dest.sin_addr.s_addr = inet_addr("127.0.0.1"); /* set destination IP number */ 
        dest.sin_port = htons(PORTNUM);                /* set destination port number */
    
        connect(mysocket, (struct sockaddr *)&dest, sizeof(struct sockaddr));
     
        len = recv(mysocket, buffer, MAXRCVLEN, 0);
    
        /* We have to null terminate the received data ourselves */
        buffer[len] = '\0';
    
        printf("Received %s (%d bytes).\n", buffer, len);
    
        closesocket(mysocket);
        getch();
        return EXIT_SUCCESS;
    }
    SERVER
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include<winsock.h>
    
    #define PORTNUM 2343
    
    int main(int argc, char *argv[])
    {
        char msg[15] = "Hello World !\n";
     
        struct sockaddr_in dest; /* socket info about the machine connecting to us */
        struct sockaddr_in serv; /* socket info about our server */
        int mysocket;            /* socket used to listen for incoming connections */
        int socksize = sizeof(struct sockaddr_in);
     
        memset(&dest, 0, sizeof(dest));    /* zero the struct before filling the fields */
        serv.sin_family = AF_INET;         /* set the type of connection to TCP/IP */
        serv.sin_addr.s_addr = INADDR_ANY; /* set our address to any interface */
        serv.sin_port = htons(PORTNUM);    /* set the server port number */    
     
        mysocket = socket(AF_INET, SOCK_STREAM, 0);
     
        /* bind serv information to mysocket */
        bind(mysocket, (struct sockaddr *)&serv, sizeof(struct sockaddr));
     
        /* start listening, allowing a queue of up to 1 pending connection */
        listen(mysocket, 1);
        int consocket = accept(mysocket, (struct sockaddr *)&dest, &socksize);
     
        while(consocket)
        {
            
     
            printf("Incoming connection from %s - sending welcome\n", inet_ntoa(dest.sin_addr));
            send(consocket, msg, strlen(msg), 0);
     
            
        }
        closesocket(consocket);
        closesocket(mysocket);
        return EXIT_SUCCESS;
    }
    You seriously need to use getaddrinfo.

  9. #9
    Registered User
    Join Date
    Nov 2009
    Posts
    5
    if any socket programmers understand this code i would appreciate the help.

  10. #10
    Registered User
    Join Date
    Jan 2009
    Posts
    103
    Did you read what I posted? If you write cleaner code, more people will respond to you. I'm not trying to be mean or anything, but this troubled me quite a bit when I was beginning (which I still am).

  11. #11
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,058
    First of all, you have to initialize winsock in your client and server app...

    Code:
    WSADATA wsaData;
        WORD wVersionRequested = MAKEWORD(1,1);
        int nRet;
        	
    	// Init WinSock
        nRet = WSAStartup(wVersionRequested, &wsaData);
        if (nRet)
        {
    		fprintf(stderr,"\nError initializing WinSock\n");
    		return -1;
        }
    
    	// Check version
    	if (wsaData.wVersion != wVersionRequested)
    	{
    		fprintf(stderr,"\nWinSock version not supported\n");
    		return -1;
    	}
    and link your code with ws2_32.lib

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple Socket Library Not so simple
    By jbully in forum C Programming
    Replies: 4
    Last Post: 12-23-2010, 09:23 AM
  2. New terrain engine
    By VirtualAce in forum Game Programming
    Replies: 16
    Last Post: 03-16-2006, 02:47 AM
  3. simple 2d "putpixel" api for windows?
    By spiky in forum C Programming
    Replies: 2
    Last Post: 10-27-2005, 02:44 PM
  4. Replies: 5
    Last Post: 12-03-2003, 05:47 PM
  5. No More Technology After World War Three
    By Unregistered in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 09-20-2001, 07:02 PM