Tag: TCP Server – Client

  • 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);
    }