• TCP Server – Client

    C Code

    TCP – Server

    defs.h

    #include <stdio.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <arpa/inet.h>
    #define SERV_TCP_PORT 6000

    #define SERV_HOST_ADDR “127.0.0.1”

    char *pname;

    utils.c

    #include “defs.h”

    #define MAXLINE 512

    /* Read n bytes from a descriptor. Use it in place of read() when fd is a stream socket. */
    int readn(fd, ptr, nbytes)
    register int fd;
    register char *ptr;
    register int nbytes;
    {
    int nleft, nread;
    nleft=nbytes;

         while (nleft>0)
    {
    nread=read(fd, ptr, nleft);

            if (nread<0) return(nread); /* error */
    else if (nread==0) break; /* EOF */
    nleft -=nread; ptr+=nread;
    }
    return(nbytes-nleft);
    }

    /* Write n bytes from a descriptor. Use it in place of write() when fd is a stream socket. */
    int writen(fd, ptr, nbytes)
    register int fd;
    register char *ptr;
    register int nbytes;
    {
    int nleft, nwritten;
    nleft=nbytes;
    while (nleft>0)
    {
    nwritten=write(fd,ptr, nleft);
    if (nwritten<=0)
    return(nwritten); /* error */
    nleft -=nwritten;
    ptr+=nwritten;
    }

    return(nbytes-nleft);
    }

    /* Read a line from a descriptor. Read the line one byte at a time,
    looking for the newline. We store the newline in the buffer, then
    follow it with a null. We return the number of characters up to,
    but not including, the null. */

    int readline(fd, ptr, maxlen)
    register int fd;
    register char *ptr;
    register int maxlen;

    {
    int n,rc;
    char c;
    for (n=1; n<maxlen;n++)
    {
    if ((rc=read(fd,&c,1))==1)
    {
    *ptr++=c;
    if(c==’\n’) break;
    }
    else if (rc==0)
    {
    if (n==1) return(0); /* EOF no data read */
    else break; /* EOF, some data was read */
    }
    else return(-1); /* error */
    }
    *ptr=0;

        return(n);
    }

    /* Read a stream socket one line at a time and write each line back to the sender.
    Return when the connection is terminated. */
    str_echo(sockfd) int sockfd;
    {
    int n; char line[MAXLINE];
    for ( ; ; )
    {
    n = readline(sockfd, line, MAXLINE);

            if (n==0) return; /* connection terminated */
    else if (n < 0) printf(“str_echo: readline error\n”);
    if (writen(sockfd, line, n) != n)
    printf(“str_echo: writen error\n”);
    }
    }

    /* read the contents of the FILE *fp, write each line to the stream socket (to the server process),
    then read a line back from the socket and write it to the standard output.
    Return to caller when an EOF is encountered on the input file. */

    str_cli(fp, sockfd)
    register FILE *fp;
    register int sockfd;
    {

        int n;
    char sendline[MAXLINE], recvline[MAXLINE+1];

        while(fgets(sendline, MAXLINE, fp) !=NULL)
    {
    n=strlen(sendline);
    if (writen(sockfd,sendline,n)!=n)
    printf(“str_cli: writen error on socket\n”);
    /* Now read a line from the socket and write it to our standard output */
    n=readline(sockfd,recvline,MAXLINE);
    if (n<0) printf(“str_cli: readline error\n”); recvline[n]=0; /* null terminate */
    fputs(recvline,stdout);
    }
    }

    server.c

    #include “defs.h”
    #include “utils.c”

    main(argc, argv)
    int argc;
    char *argv[];
    {
    FILE *fp;
    char filename[100];
    int sockfd, newsockfd, clilen, childpid,n;
    struct sockaddr_in cli_addr, serv_addr; pname=argv[0];

        if ((sockfd=socket(AF_INET,SOCK_STREAM,0)) <0)
    {
    printf(“server: can’t open stream socket\n”);
    exit(0);
    }

        bzero((char*) &serv_addr, sizeof(serv_addr));

        serv_addr.sin_family=AF_INET;
    serv_addr.sin_addr.s_addr=htonl(INADDR_ANY);
    serv_addr.sin_port=htons(SERV_TCP_PORT);

        if(bind(sockfd,(struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0)
    {
    printf(“server: can’t bind local address\n”);
    exit(0);
    }

        listen(sockfd,5);

        for ( ; ; )
    {
    clilen=sizeof(cli_addr);
    newsockfd=accept(sockfd,(struct sockaddr *) &cli_addr,&clilen);

            if(newsockfd < 0)
    {
    printf(“server: accept error\n”);
    exit(0);

            }

            if((childpid=fork()) <0)
    {
    printf(“server: fork error\n”);
    exit(0);
    }
    else if (childpid==0)
    {
    close(sockfd);
    if((n=readn(newsockfd,filename,sizeof(filename))) <= 0)
    {
    printf(“server: filename read error\n”);
    }
    filename[n]=’\0′;
    if ((fp=fopen(filename,”r”)) == NULL)
    {
    printf(“De mporo na anoikso to arxeio\n”);
    exit(-1);
    }
    str_cli(fp, newsockfd);
    /*str_echo(newsockfd);*/
    fclose(fp);
    exit(0);
    }
    close(newsockfd);
    }
    }

    TCP – Client

    defs.h

    #include <stdio.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <arpa/inet.h>
    #define SERV_TCP_PORT 6000

    #define SERV_HOST_ADDR “127.0.0.1”

    char *pname;

    utils.c

    #include “defs.h”

    #define MAXLINE 512

    /* Read n bytes from a descriptor. Use it in place of read() when fd is a stream socket. */
    int readn(fd, ptr, nbytes)
    register int fd;
    register char *ptr;
    register int nbytes;
    {
    int nleft, nread;
    nleft=nbytes;

         while (nleft>0)
    {
    nread=read(fd, ptr, nleft);

            if (nread<0) return(nread); /* error */
    else if (nread==0) break; /* EOF */
    nleft -=nread; ptr+=nread;
    }
    return(nbytes-nleft);
    }

    /* Write n bytes from a descriptor. Use it in place of write() when fd is a stream socket. */
    int writen(fd, ptr, nbytes)
    register int fd;
    register char *ptr;
    register int nbytes;
    {
    int nleft, nwritten;
    nleft=nbytes;
    while (nleft>0)
    {
    nwritten=write(fd,ptr, nleft);
    if (nwritten<=0)
    return(nwritten); /* error */
    nleft -=nwritten;
    ptr+=nwritten;
    }

    return(nbytes-nleft);
    }

    /* Read a line from a descriptor. Read the line one byte at a time,
    looking for the newline. We store the newline in the buffer, then
    follow it with a null. We return the number of characters up to,
    but not including, the null. */

    int readline(fd, ptr, maxlen)
    register int fd;
    register char *ptr;
    register int maxlen;

    {
    int n,rc;
    char c;
    for (n=1; n<maxlen;n++)
    {
    if ((rc=read(fd,&c,1))==1)
    {
    *ptr++=c;
    if(c==’\n’) break;
    }
    else if (rc==0)
    {
    if (n==1) return(0); /* EOF no data read */
    else break; /* EOF, some data was read */
    }
    else return(-1); /* error */
    }
    *ptr=0;

        return(n);
    }

    /* Read a stream socket one line at a time and write each line back to the sender.
    Return when the connection is terminated. */
    str_echo(sockfd) int sockfd;
    {
    int n; char line[MAXLINE];
    for ( ; ; )
    {
    n = readline(sockfd, line, MAXLINE);

            if (n==0) return; /* connection terminated */
    else if (n < 0) printf(“str_echo: readline error\n”);
    if (writen(sockfd, line, n) != n)
    printf(“str_echo: writen error\n”);
    }
    }

    /* read the contents of the FILE *fp, write each line to the stream socket (to the server process),
    then read a line back from the socket and write it to the standard output.
    Return to caller when an EOF is encountered on the input file. */

    str_cli(fp, sockfd)
    register FILE *fp;
    register int sockfd;
    {

        int n;
    char sendline[MAXLINE], recvline[MAXLINE+1];

        while(fgets(sendline, MAXLINE, fp) !=NULL)
    {
    n=strlen(sendline);
    if (writen(sockfd,sendline,n)!=n)
    printf(“str_cli: writen error on socket\n”);
    /* Now read a line from the socket and write it to our standard output */
    n=readline(sockfd,recvline,MAXLINE);
    if (n<0) printf(“str_cli: readline error\n”); recvline[n]=0; /* null terminate */
    fputs(recvline,stdout);
    }
    }

    client.c

    #include “defs.h”
    #include “utils.c”

    main(argc, argv)
    int argc;
    char *argv[];
    {
    char filename[100];
    int sockfd;
    struct sockaddr_in serv_addr;
    pname=argv[0];
    bzero((char*) &serv_addr, sizeof(serv_addr));
    serv_addr.sin_family=AF_INET;
    serv_addr.sin_addr.s_addr=inet_addr(SERV_HOST_ADDR);
    serv_addr.sin_port=htons(SERV_TCP_PORT);

        if((sockfd=socket(AF_INET,SOCK_STREAM,0)) < 0)
    {
    printf(“client: can’t open stream socket\n”);
    exit(0);
    }

        if (connect(sockfd,(struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0)
    {
    printf(“client: can’t connect to server\n”);
    exit(0);
    }

        /*////////////////////////*/
    printf(“Dose to onoma tou arxeiou:\n”);
    scanf(“%s”,&filename);
    writen(sockfd,filename,sizeof(filename));

        /*str_cli(stdin, sockfd);*/
    str_echo(sockfd);
    /*////////////////////////*/

        close(sockfd);
    exit(0);
    }

  • Projects

    Project Title: OMNI.Net: Computational and Storage Resource Sharing under the .NET Framework.
    The explosion of Internet during the last decade has mostly been build around the exchange of information. However, lots of other computational resources, such as computing cycles, disk storage and other networked devices seem to be under-utilized due to various reasons; mainly due to lack of universally acceptable architectures, APIs, protocols, programming models and guaranties to do it in a satisfactory way. This proposal suggests building the core of a platform that will allow the seamless sharing of resources among end-user’s computers, building on existing standards, the P2P paradigm and the .NET framework. Fault tolerance, security and development APIs will be covered in order to provided the desired capability to share processing and storage capacity of end-user PCs and server nodes.

    Project Title: Smart Earth
    The scope of this application has been the design, deployment and integration of a navigation system which will provide location based services with a personalized way proportionally the preferences and the interests of each user. The system combines the geographical position of user with the behavior, the energies and his profile so as to provide services of added value in the user. The location based services are provided via web services and the personalization is provided via fuzzy logic decisions. The use of web services constitutes alternative way in the growth of navigation systems. It will not be necessary to have applications, which stored the maps of regions, that interest the user or any other geographic information (points of interesting etc). Any information the application needs, will be received by using web services. The determination of place of user becomes via connection with GPS Receiver. Furthermore, the system will provide a personalized way of the information given to the user according to his interests in combination with the analysis of user’s actions on the system, the history and the analysis of the others users’ preferences.

    clip_image002
    http://www.codeplex.com/smartearth

    Project Title: Facebook Earth
    This application uses WPF, Virtual Earth, Facebook Developer Toolkit and Yahoo Map Service
    in order to place your Facebook Friends on a map. Also you can select a point of meeting
    and send driving directions with notification to each of your friends.
    clip_image002[4]

    http://www.codeplex.com/FacebookEarth

    Project Title: iEnergis
    iENERGIS application provides remote monitoring and control of the electrical appliances in a building. Through our web site every user can send his data to our live database, and so, with the use of virtual earth, we can monitor the energy consumption in a whole area. These could be used from environmental organisations and energy companies so that to optimize the energy supply in an area and to export statistical data.

    clip_image002[6]
    http://www.codeplex.com/iEnergis


  • Welcome…

    The Microsoft Visual Studio .NET logo.

    My new Site! (My other one: http://kxalv.spaces.live.com)
    Topics about: .Net Programming, Visual Studio , WPF, WCF, Silverlight, Expression Blend, My Life, etc…