Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

C Socket

  • 12-01-2004 06:33PM
    #1
    Registered Users, Registered Users 2 Posts: 4,882 ✭✭✭


    Hi,
    Does anyone know where to get a simple socket server in C that will recieve and display messages sent to it from a client.
    I need it to test a J2ME client application I made.
    I'v searched the web, but I can ones which are used as chat servers or for sending images etc.
    Thanks,
    Bacchus


Comments

  • Closed Accounts Posts: 36 DaemÈn


    heres a simple one i was working with a while back ... u might have to make a few changes ... i havent time to compile it at d mo but as far as i can remember its working.....

    /****************SERVER********************************/
    /*Server*/
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <arpa/inet.h>
    #include <netdb.h>
    #include <stdio.h>
    #include <unistd.h>
    #include <string.h>

    #define LOCAL_SERVER_PORT 1500
    #define MAX_MSG 100

    int main(int argc, char *argv[])
    {

    int soc, rc, n, cliLen;
    struct sockaddr_in cliAddr, servAddr;
    char msg[MAX_MSG];

    /* create socket */
    soc=socket(AF_INET, SOCK_DGRAM, 0);
    if(soc<0)
    {
    perror("Socket Failure\n");
    exit(1);
    }

    /* bind local server port */
    servAddr.sin_family = AF_INET;
    servAddr.sin_addr.s_addr = htonl(INADDR_ANY);
    servAddr.sin_port = htons(LOCAL_SERVER_PORT);

    rc = bind (soc, (struct sockaddr *) &servAddr,sizeof(servAddr));

    if(rc<0)
    {
    printf("Cannot bind port number %d", LOCAL_SERVER_PORT);
    exit(1);
    }
    /**************************************************************************/

    printf("%s: waiting for data on port UDP %u\n", argv[0],LOCAL_SERVER_PORT);

    /* server loop */
    while(1)
    {
    memset(msg,0x0,MAX_MSG); /* init buffer */


    cliLen = sizeof(cliAddr); /* receive data */
    n = recvfrom(soc, msg, MAX_MSG, 0, (struct sockaddr *) &cliAddr, &cliLen);

    if(n<0)
    {
    perror("\ncannot receive data");
    continue;
    }

    /* print received message */
    printf("%s: from %s:UDP%u : %s \n",
    argv[0],inet_ntoa(cliAddr.sin_addr),
    ntohs(cliAddr.sin_port),msg);

    }/* end of server loop */

    /****************************************************************************/

    return 0;

    }


    /****************SERVER********************************/





    /****************CLIENT********************************/

    /*Client*/
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <arpa/inet.h>
    #include <netdb.h>
    #include <stdio.h>
    #include <unistd.h>
    #include <string.h>
    #include <sys/time.h>

    #define REMOTE_SERVER_PORT 1500
    #define MAX_MSG 100


    int main(int argc, char *argv[])
    {

    int soc, rc, i;
    struct sockaddr_in cliAddr, remoteServAddr;
    struct hostent *h;

    /* check command line args */
    if(argc<3)
    {
    printf("usage : %s <server> <data1> ... <dataN> \n", argv[0]);
    exit(1);
    }

    /* get server IP address (no check if input is IP address or DNS name */
    h = gethostbyname(argv[1]);
    if(h==NULL)
    {
    printf("%s: unknown host '%s' \n", argv[0], argv[1]);
    exit(1);
    }
    /************************************************************************/
    printf("%s: sending data to '%s' (IP : %s) \n", argv[0], h->h_name,
    inet_ntoa(*(struct in_addr *)h->h_addr_list[0]));

    remoteServAddr.sin_family = h->h_addrtype;
    memcpy((char *) &remoteServAddr.sin_addr.s_addr,
    h->h_addr_list[0], h->h_length);
    remoteServAddr.sin_port = htons(REMOTE_SERVER_PORT);

    /* socket creation */
    soc = socket(AF_INET,SOCK_DGRAM,0);
    if(soc<0)
    {
    printf("%s: cannot open socket \n",argv[0]);
    exit(1);
    }

    /* bind any port */
    cliAddr.sin_family = AF_INET;
    cliAddr.sin_addr.s_addr = htonl(INADDR_ANY);
    cliAddr.sin_port = htons(0);

    rc = bind(soc, (struct sockaddr *) &cliAddr, sizeof(cliAddr));
    if(rc<0)
    {
    printf("%s: cannot bind port\n", argv[0]);
    exit(1);
    }


    /* send data */
    for(i=2;i<argc;i++)
    {
    rc = sendto(soc, argv, strlen(argv)+1, 0,
    (struct sockaddr *) &remoteServAddr,
    sizeof(remoteServAddr));

    if(rc<0)
    {
    printf("%s: cannot send data %d \n",argv[0],i-1);
    close(soc);
    exit(1);
    }

    }

    return 1;

    }


    /****************CLIENT********************************/


  • Registered Users, Registered Users 2 Posts: 4,882 ✭✭✭Bacchus


    Thanks DaemÈn, got it going.


Advertisement