0

I am really new to networking, I am working with an echo server and echo client. In my lab I have two computers connected on the network so I can ping them after configuring them via "ifconfig eth0 10.1.1.n (n is the port number) netmask 255.255.255.0".

echo_server.c

 /* A simple echo server using TCP */
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <sys/signal.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <strings.h>
#include <unistd.h>
#include <errno.h>
#include <arpa/inet.h>

#define SERVER_TCP_PORT 3000    /* well-known port */
// #define BUFLEN       256 /* buffer length */

int echod(int);
void reaper(int);

int main(int argc, char **argv)
{
    int     sd, new_sd, client_len, port, confd = 0,b,tot;
    struct  sockaddr_in server, client;
    char buff[1025];

    switch(argc){
    case 1:
        port = SERVER_TCP_PORT;
        break;
    case 2:
        port = atoi(argv[1]);
        break;
    default:
        fprintf(stderr, "Usage: %d [port]\n", argv[0]);
        exit(1);
    }

    /* Create a stream socket   */  
    if ((sd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
        fprintf(stderr, "Can't creat a socket\n");
        exit(1);
    }

    /* Bind an address to the socket    */
    bzero((char *)&server, sizeof(struct sockaddr_in));
    server.sin_family = AF_INET;
    server.sin_port = htons(port);
    server.sin_addr.s_addr = htonl(INADDR_ANY);
    if (bind(sd, (struct sockaddr *)&server, sizeof(server)) == -1){
        fprintf(stderr, "Can't bind name to socket\n");
        exit(1);
    }



    while(1) {
      confd = accept(sd, (struct sockaddr*)NULL, NULL);
        if (confd==-1) {
            perror("Accept");
            continue;
        }
        //Should create a text file.
        FILE* fp = fopen( "test.txt", "wb");
        tot=0;
        if(fp != NULL){
            while( (b = recv(confd, buff, 1024,0))> 0 ) {
                tot+=b;
                fwrite(buff, 1, b, fp);
            }

            printf("Received byte: %d\n",tot);
            if (b<0)
               perror("Error recieving the file");

            fclose(fp);
        } else {
            perror("File doesnt exist");
        }
        close(confd);
    }
}

echo_client.c

/* A simple echo client using TCP */
#include <stdio.h>
#include <netdb.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdlib.h>
#include <strings.h>


#define SERVER_TCP_PORT 3000    /* well-known port */
#define BUFLEN      256 /* buffer length */

int main(int argc, char **argv)
{
    int     n, i, bytes_to_read, b;
    int     sd, port;
    struct  hostent     *hp;
    struct  sockaddr_in server;
    char    *host, *bp, rbuf[BUFLEN], sbuf[BUFLEN], sendbuffer[100];

    switch(argc){
    case 2:
        host = argv[1];
        port = SERVER_TCP_PORT;
        break;
    case 3:
        host = argv[1];
        port = atoi(argv[2]);
        break;
    default:
        fprintf(stderr, "Usage: %s host [port]\n", argv[0]);
        exit(1);
    }

    /* Create a stream socket   */  
    if ((sd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
        fprintf(stderr, "Can't creat a socket\n");
        exit(1);
    }

    bzero((char *)&server, sizeof(struct sockaddr_in));
    server.sin_family = AF_INET;
    server.sin_port = htons(port);
    if (hp = gethostbyname(host)) 
      bcopy(hp->h_addr, (char *)&server.sin_addr, hp->h_length);
    else if ( inet_aton(host, (struct in_addr *) &server.sin_addr) ){
      fprintf(stderr, "Can't get server's address\n");
      exit(1);
    }

    /* Connecting to the server */
    if (connect(sd, (struct sockaddr *)&server, sizeof(server)) == -1){
      fprintf(stderr, "Can't connect \n");
      exit(1);
    }

    FILE *fp = fopen("test.txt", "rb");

    if(fp == NULL){
        perror("File doesnt exist");
        return 2;
    }

    while( (b = fread(sendbuffer, 1, sizeof(sendbuffer), fp))>0 ){
        send(sd, sendbuffer, b, 0);
    }

    fclose(fp);

    close(sd);
    return(0);
}

I am trying to send a text file from the client to the server when the script is run. To run the script I specify a port number.

When I execute the server script I get Accept: Invalid argument. It seems to be on loop so I am getting over a hundred of those output. I have to exit the terminal at that point.

Any points or tips as to how I can send a file from the client to the server when the script is executed?

2
  • 1
    @tadman select()'s only needed when using non-blocking I/O, which is extra complexity that's not needed at this stage. A simple single-threaded socket program doesn't need it. Commented Sep 23, 2019 at 16:37
  • That book (UNIX Network Programming) is the de-facto reference for how to do this. If you can get your hands on a copy it can explain a lot of the intricacies involved here. It's hard to evaluate this code at a glance. Commented Sep 23, 2019 at 16:38

1 Answer 1

1

Before you call accept(2) you need to call listen(2) to mark the socket as server socket that will be used to accept incoming connection requests.

Sign up to request clarification or add additional context in comments.

3 Comments

Before the loop. Only call listen() once.
where would I call it in my server code? sorry I am a beginner.
Thank you, I was able to get past that issue (I believe) now I am on the client side and it doesnt seem to be able to connect.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.