0

The question asked me to:

  1. modify the original server code so that it accepts any port number instead of the predefined one (i.e., port number 8989). Port number will be entered in the command line when testing the Server1.exe as follows:
  • Networking 1>start server 8989 (Hint given: the main function parameters holds the port the command line arguments)
  1. modify the original server code so that it closes the connection and terminate if the client input message is “exit server”. Moreover the server should modify the client message to be in uppercase and it should print it accordingly.

I tried doing these 2 parts individually using normal C coding already but I'm unsure on how to combine it into the original server code/base code T.T

The base code/original server code:

//////////////////////////  Server.c ////////////////

#include<io.h>
#include<stdio.h>
#include<winsock2.h>

#define MY_PORT     8989
#define MAXBUF      256

int main(int argc , char *argv[])
{
    WSADATA wsa;
    SOCKET sockfd , clientfd;
        struct sockaddr_in self;
    char buffer[MAXBUF];

    printf("\nInitialising Winsock...");
    if (WSAStartup(MAKEWORD(2,2),&wsa) != 0)
    {
        printf("Failed. Error Code : %d",WSAGetLastError());
        return 1;
    }
     
    printf("Initialised.\n");

    /*---create streaming socket---*/
    if ( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0 )
    {
        perror("Socket");
        exit(errno);
    }

        printf("Socket created.\n");

    /*---initialize address/port structure---*/
    /* bzero(&self, sizeof(self));*/
    self.sin_family = AF_INET;
    self.sin_port = htons(MY_PORT);   // Host to Network Short (16-bit)
    self.sin_addr.s_addr = INADDR_ANY;  

    /*---assign a port number to the socket---*/
    if ( bind(sockfd, (struct sockaddr*)&self, sizeof(self)) != 0 )
    {
        perror("socket--bind");
        exit(errno);
    }

        puts("Bind done");

    /*---make it a "listening socket"---*/
    if ( listen(sockfd, 20) != 0 )
    {
        perror("socket--listen");
        exit(errno);
    }
        
        puts("Waiting for incoming connections...");

    /*---forever... ---*/
    while (1)
    {   struct sockaddr_in client_addr;
        int addrlen=sizeof(client_addr);

        /*---accept a connection (creating a data pipe)---*/
        clientfd = accept(sockfd, (struct sockaddr*)&client_addr, &addrlen);
        
        send(clientfd, buffer, recv(clientfd, buffer, MAXBUF, 0), 0);

        /*---close connection---*/
        close(clientfd);
    }

    /*---clean up (should never get here!)---*/
    close(sockfd);
        WSACleanup();
    return 0;
}

The code I created using normal c programming to address part 1) of the question:

#include<stdio.h>
int main (void){
    int port_no;
    printf("Enter a port number:\n");
    scanf("%d", &port_no);
}

The code I created using normal c programming to address part 2) of the question:

#include<stdio.h>
#include<ctype.h>
#define MAX 100
void conToupper(char *aPtr);
int main (void){
int port_no; //changed
    char exit[MAX];
    char a[MAX] = "exit server";


    printf("If you'd like to end, input 'exit server':");
    scanf("%s", &exit);

    for(int i=0; i<MAX; i++){
        if (exit[i] == a[i]){
            conToupper(a);
            printf("%s\n",a);
        break;
        }
    }
}

void conToupper(char *aPtr){
        while(*aPtr != '\0'){
            *aPtr = toupper(*aPtr);
            ++aPtr;
        }
    }

The problem I have is, right now how can I possibly combine these parts 1) & parts 2) in the base code provided? Please bear with me I just got into socket programming :") Also, I'm not so sure if I'm approaching this question in the right way..

  • 2
    The requirements state you need to extract the port from [command line arguments](https://stackoverflow.com/questions/3371886/command-line-arguments-in-c), that's different than `scanf` – yano Dec 02 '21 at 15:35
  • On part 2: The exit command comes from the client of the server, not from the user at the terminal. And you cannot compare strings by `==`. Grab your beginner's C book and read the chapter on strings, please. – the busybee Dec 02 '21 at 15:49
  • @thebusybee But you can compare them using '!=' ? https://stackoverflow.com/questions/33793052/how-to-compare-two-arrays-in-c-programming-language I'm confused.. I understand that you can't directly compare arrays like that but still some resources show that you can, so how should I do this then? –  Dec 03 '21 at 03:32
  • No, `!=` just "inverts" the comparison. Would you mind to provide the links to such resources that claim so? You need to learn the difference between pointers/addresses and the contents (single value or array) they refer to. – the busybee Dec 03 '21 at 07:13
  • @thebusybee oh in that case then I'll try & use the other way round by using '!=' then. Thanks. –  Dec 03 '21 at 08:57
  • You did not understand: neither `==` nor `!=` can be used to compare two strings whether they contain equal sequences of characters. -- See my first comment: Learn about strings. As this is apparently homework, I will not tell the the solution. -- And you did not tell us where you found that resources that claim that you could do it. – the busybee Dec 03 '21 at 09:06
  • 'send(clientfd, buffer, recv(clientfd, buffer, MAXBUF, 0), 0);' - just irretrievably bad:( – Martin James Dec 03 '21 at 18:56

0 Answers0