Hi,

I need to create a user name and password login client server. Attached is my attempt. This is my first try on socket and network programming in C. I have reached to a certain level but just a little problem is stopping me because of less C knowledge. I need to compare my inbuilt user names with the user cin strings.

like

user name = one
password = 1

user name = two
password = 2

How do I modify my code to do that.

Attached is my client and server.

/* A simple server in the internet domain using TCP
   The port number is passed as an argument */
#include <stdio.h>
#include <string.h>
#include <sys/types.h> 
#include <sys/socket.h>
#include <netinet/in.h>


void error(char *msg)
{
    perror(msg);
    exit(1);
}

int main(int argc, char *argv[])
{
     int sockfd, newsockfd, portno, clilen;
     char buffer[256];
     //const char* const CONST_STRING = "somename";



     struct sockaddr_in serv_addr, cli_addr;
     int n;
     if (argc < 2) {
         fprintf(stderr,"ERROR, no port provided\n");
         exit(1);
     }
     sockfd = socket(AF_INET, SOCK_STREAM, 0);
     if (sockfd < 0) 
        error("ERROR opening socket");
     bzero((char *) &serv_addr, sizeof(serv_addr));
     portno = atoi(argv[1]);
     serv_addr.sin_family = AF_INET;
     serv_addr.sin_addr.s_addr = INADDR_ANY;
     serv_addr.sin_port = htons(portno);
     if (bind(sockfd, (struct sockaddr *) &serv_addr,
              sizeof(serv_addr)) < 0) 
              error("ERROR on binding");
     listen(sockfd,5);
     clilen = sizeof(cli_addr);
     newsockfd = accept(sockfd, 
                 (struct sockaddr *) &cli_addr, 
                 &clilen);
     if (newsockfd < 0) 
          error("ERROR on accept");
     bzero(buffer,256);
     n = read(newsockfd,buffer,255);
     if (n < 0) error("ERROR reading from socket");
     //char inter[] = buffer;
     //if (strcmp(buffer,CONST_STRING) == 0){
     printf("Here is the message: %s\n",buffer);
     //puts("Correct\n");
     //} else { printf("NOT EQUAL\n"); }
     n = write(newsockfd,"I got your message",18);
     if (n < 0) error("ERROR writing to socket");
     return 0; 
}
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h> 

void error(char *msg)
{
    perror(msg);
    exit(0);
}

int main(int argc, char *argv[])
{
    int sockfd, portno, n;
    struct sockaddr_in serv_addr;
    struct hostent *server;

    char buffer[256];
    if (argc < 3) {
       fprintf(stderr,"usage %s hostname port\n", argv[0]);
       exit(0);
    }
    portno = atoi(argv[2]);
    sockfd = socket(AF_INET, SOCK_STREAM, 0);
    if (sockfd < 0) 
        error("ERROR opening socket");
    server = gethostbyname(argv[1]);
    if (server == NULL) {
        fprintf(stderr,"ERROR, no such host\n");
        exit(0);
    }
    bzero((char *) &serv_addr, sizeof(serv_addr));
    serv_addr.sin_family = AF_INET;
    bcopy((char *)server->h_addr, 
         (char *)&serv_addr.sin_addr.s_addr,
         server->h_length);
    serv_addr.sin_port = htons(portno);
    if (connect(sockfd,&serv_addr,sizeof(serv_addr)) < 0) 
        error("ERROR connecting");
    printf("Please enter the message: ");
    bzero(buffer,256);
    fgets(buffer,255,stdin);
    n = write(sockfd,buffer,strlen(buffer));
    if (n < 0) 
         error("ERROR writing to socket");
    bzero(buffer,256);
    n = read(sockfd,buffer,255);
    if (n < 0) 
         error("ERROR reading from socket");
    printf("%s\n",buffer);
    return 0;
}

Thanks

Suppose you did not have a client server program and you had to make a simple username/ password application on that, how would you go about that ?
I have given you some hints in the code below. If you can fill out and complete this program then the extending it to client server is not tough

int main()
{
  char username[50]= "username";
  char password[50]= "password";

  char inputUserName[50];
  char inputPassword[50];

  // Make the user input the user name and password. Store it in the variables inputUserName and inputPassword respectively.

  //Now use strcmp to check if the entries entered by the user match the stored entries

   return 0;
}

Hi,
I'm new to socket programming in c. I just want to know how to run the server.c code. I have compiled correctly. But the when I tried to run, It gave an error msg which is related to the argv. I added the port number as an argument. But still getting an error.

Any format example for the run ?

Any suggestion????

In Linux, something like:
prompt $ gcc -g server.c -o server
prompt $ server 12345

If you're using VS on Windows, you need to add the argument somewhere in the settings. Or after you build server.exe, open a Command Prompt window, cd to where server.exe was placed, and then:
prompt $ ./server.exe 12345

If that isn't working, what error are you getting? Maybe the problem isn't with the argument.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.