Help with Unix socket programing


 
Thread Tools Search this Thread
Special Forums IP Networking Help with Unix socket programing
# 1  
Old 05-06-2010
Help with Unix socket programing

hi

I am strucked in a client server program

client need to login to server
client logins if only username and password are correct

i have written a program

username is stored as file and password is smilar to username whic is stored in that file

when server asks for username ,client provides
it opens the file and check string for password if it matches rest operations are allowed otherwise client s disconnected

Code:
//#include "unp.h"
#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,*choice;

    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,(struct sockaddr *)&serv_addr,sizeof(serv_addr)) < 0) 
        error("ERROR connecting");
bzero(buffer,256);// clears buffer
    n = read(sockfd,buffer,255);//reading
    if (n < 0) 
         error("ERROR readin from socket");
printf("%s", buffer);
 
    fgets(buffer,255,stdin);

//scanf("%d",choice);//readin mesg from terminal
    n = write(sockfd,buffer,255);//writes
    if (n < 0) 
         error("ERROR writing to socket");

if(*buffer == 'r'){
printf("enter user name :  ");

bzero(buffer,256);
fgets(buffer,255,stdin);//readin mesg from terminal
    n = write(sockfd,buffer,strlen(buffer));//writes
    if (n < 0) 
         error("ERROR writing to socket");
n = read(sockfd,buffer,255);//reading
    if (n < 0) 
         error("ERROR readin from socket");
printf("%s\n",buffer);// printing
bzero(buffer,256);

}

if(*buffer == 'l')//login
{
printf("enter your user name: ");

bzero(buffer,256);
fgets(buffer,255,stdin);//readin mesg from terminal
    n = write(sockfd,buffer,strlen(buffer));//writes
    if (n < 0) 
         error("ERROR writing to socket");
printf("\n enter your password");
bzero(buffer,256);
fgets(buffer,256,stdin);//readin mesg from terminal
    n = write(sockfd,buffer,strlen(buffer));//writes
n = read(sockfd,buffer,255);//reading
    if (n < 0) 
         error("ERROR readin from socket");
printf("%s\n",buffer);// printing

}
    while(1)
{
    printf("type  message: ");
    bzero(buffer,256);

    fgets(buffer,255,stdin);//readin mesg from terminal
    n = write(sockfd,buffer,strlen(buffer));//writes
    if (n < 0) 
         error("ERROR writing to socket");
    bzero(buffer,256);// clears buffer
    n = read(sockfd,buffer,255);//reading
    if (n < 0) 
         error("ERROR reading from socket");
    printf("%s\n",buffer);// printing
}
    return 0;
}

Code:
#include <stdio.h>
#include <sys/types.h> 
#include <sys/socket.h>
#include <netinet/in.h>
//#include "unp.h"

void readwrite(int, int, int); /* function prototype */
void error(char *msg)
{
    perror(msg);
    exit(1);
}

int main(int argc, char *argv[])
{
     int sockfd, newsockfd, portno, clilen, pid;
     struct sockaddr_in serv_addr, cli_addr;

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

     while (1) {
         newsockfd = accept(sockfd, 
               (struct sockaddr *) &cli_addr, &clilen);
         if (newsockfd < 0) 
             error("ERROR on accept");
         pid = fork();
         if (pid < 0)
             error("ERROR on fork");
         if (pid == 0)  {
             close(sockfd);
              int x,y,x1,y1,i=0,n,choice;
              char buffer[256],ex[256];
               FILE *fp;
           printf("Incoming connection from %s - sending welcome\n", inet_ntoa(cli_addr));
          n = write(newsockfd," welcome \n - select one \n r for registration \n l for login",145);
             if (n < 0) error("ERROR writin to socket");
 
 bzero(buffer,256);
                n = read(newsockfd,buffer,255);
               if (n < 0) error("ERROR reading from socket");

if(*buffer == 'r')
{
   
// n = write(newsockfd," welcome \n - please Enter a user name:",45);
            // if (n < 0) error("ERROR writing to socket");
             bzero(buffer,256);
                n = read(newsockfd,buffer,255);
               if (n < 0) error("ERROR reading from socket");
             fp=fopen(buffer,"a+");
             fwrite(buffer,1,strlen(buffer),fp);
n = write(newsockfd,"\n your user name is your password:",45);
             if (n < 0) error("ERROR writing to socket");
                            
              fclose(fp);
exit(0);

             
}
if(*buffer == 'l')
{
   char key[256];

bzero(buffer,256);
                n = read(newsockfd,buffer,255);
               if (n < 0) error("ERROR reading from socket");
       
  fp=fopen(buffer,"r");

  if(fp=='\0')
{
  n = write(newsockfd,"\n No user exist:",45);
             if (n < 0) error("ERROR writing to socket");
 // n = write(newsockfd,"\n please register :",45);
           //  if (n < 0) error("ERROR writing to socket");
}
else
{


bzero(buffer,256);
 n = read(newsockfd,buffer,255);
               

fscanf(fp,"%s",key);

if(strcmp(buffer,key)!=0)
{
n = write(newsockfd,"\nunsucessful login:",45);
            // if (n < 0) error("ERROR writing to socket");
break;
}

   
    n = write(newsockfd,"\n loged:",45);
             if (n < 0) 
          error("ERROR writing to socket");


              
}

}
         
  
              srand((unsigned)time(3));
              x=rand()%4;
              y=rand()%4;
             
             readwrite(newsockfd,x,y);
             exit(0);
         }
         else close(newsockfd);
     } /* end of while */
     return 0; /* we never get here */
}


void readwrite(int sock,int x ,int y)
{
   int n;
   char buffer[256];
   
  printf("x=%d y=%d",x,y);
    while(1){
   bzero(buffer,256);
   n = read(sock,buffer,255);
   if (n < 0) error("ERROR reading from socket");
   printf("Client: %s\n",buffer);
   n = write(sock,"Server :msg reached",25);
   if (n < 0) error("ERROR writing to socket");
}
 
}


Last edited by pludi; 05-06-2010 at 03:04 AM.. Reason: code tags, please...
# 2  
Old 05-06-2010
Please tell us what the problem is.
# 3  
Old 09-27-2010
Kartik,

1. Can you put what the arguments for the module are ?
2. What kind of errors are you getting ?

It will help to resolve the errors efficiently.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Requirement of Spliting a text file in UNIX Programing

Hi, There is a requirement, needs to split the text file based on RC code present in text file. For this, needs to write a unix shell programing script for the above requirement. For example in text file, if there are distinct RC codes, then we needs to split into multiple text files. In... (1 Reply)
Discussion started by: Chandra2678
1 Replies

2. Programming

Unix Socket

Hai every one ,,, I am runinng one tcp socket in non-blocking mode. In the connect state(operation in progress) if the server is not running ,client socket select will give success. FD_ISSET(socketfd,&wfd) will give me succes always and am getting wfd value as 16. I tried with blocking... (6 Replies)
Discussion started by: andrew.paul
6 Replies

3. Programming

unix socket programming

Hi all, my name is anjali, working as an intern, doing masters i have an assignment from my school, which i am not able to solve, can someone please help me in finding the solution for this problem thanking you in advance....lol Write a C socket program on UNIX where a TCP client... (1 Reply)
Discussion started by: anjali.symantec
1 Replies

4. IP Networking

socket programing-problem with server

hi, i am new to socket programming.i have a problem in server.requirement is it should continuosly read the requests from client(sends requests continuously) and after certain delay(i kept der sleep) should send response. for this i used msgqueues so that after reading it is sending into... (1 Reply)
Discussion started by: chandinisree
1 Replies

5. Post Here to Contact Site Administrators and Moderators

socket programing-problem with server

hi, i am new to socket programming.i have a problem in server.requirement is it should continuosly read the requests from client(sends requests continuously) and after certain delay(i kept der sleep) should send response. for this i used msgqueues so that after reading it is sending into... (1 Reply)
Discussion started by: chandinisree
1 Replies

6. Programming

socket programing in client server

hei, i want to enter name and read it by client server socket program after checking name validity put the password and check.if ok than server clirnt say correct other wise incorrect. below my code: char name; printf ("Welcome to the server \n"); printf ("Enter user name: \n"); scanf... (1 Reply)
Discussion started by: saiful_911
1 Replies

7. Programming

how can I send and receive data in client server socket programing

char name; printf ("Welcome to the server \n"); printf ("Enter user name: \n"); scanf ("%c", &name); how can client send name to server:what should be the code? int send ( int sid , const char ∗buffer Ptr , int len , int f l a g ) how can client receive ack from... (1 Reply)
Discussion started by: saiful_911
1 Replies

8. UNIX for Advanced & Expert Users

Unix: socket & Co

Hello, I need help to replace the ................. of client.c that request the server implemented by server.c ------------------ Listing 1 - server.c /* Inclusion des différentes librairies nécessaires */ #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <string.h> ... (0 Replies)
Discussion started by: bounkolh
0 Replies

9. Programming

creating GUI using C programing in unix server

I want to write GUI using C programing in Unix server. But I can not use windows.h header file. I want to create some text box using C programing. Please suggest me any sample code or tutorial for this purpose. I do not know any thing about GUI programing in C language. Thanks in... (5 Replies)
Discussion started by: rinku
5 Replies

10. Programming

How can I program socket in unix?

Excuse me . I'm a beginner . In windows , MFC can be used , but how to do in Unix ? And does unix support c++like VC++ ? How can I get developing tools in Unix ? (7 Replies)
Discussion started by: sanjohn
7 Replies
Login or Register to Ask a Question