[C] close server after user input


 
Thread Tools Search this Thread
Top Forums Programming [C] close server after user input
# 1  
Old 01-10-2012
[C] close server after user input

i must close server after a specific user input, such as FINE, if i put a test inside function "maiuscolatore" i receive an error on second recv of client. why?

below there are code of client and server:

CLIENT:
Code:
        #include <stdlib.h>
        #include <stdio.h>
        #include <unistd.h>
        #include <sys/types.h>
        #include <ctype.h>
        #include <sys/socket.h>
        #include <string.h>
        #include <netinet/in.h>
        #define MAXLENGTH 80
        #define SERVERPORT 1313

        int main(){
            int sockfd;
            struct sockaddr_in server={AF_INET,htons(SERVERPORT),INADDR_ANY};
            int i=0, len;
            char buf[MAXLENGTH],c,d;
            if ((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1){
                perror("socket fallita");
                exit(1);
            }
            if (connect(sockfd, (struct sockaddr *) &server,sizeof server)==-1){
                perror("connect fallita");
                exit(2);
            }
            printf("\nDigita una stringa :");
            while((c=getchar())!='\n' && i<MAXLENGTH){
                if(isalpha(c)!=0){
                        buf[i++]=c;}
                else{
                    i++;
                }
            }
            buf[i]='\0';
            len=strlen(buf);
            printf("\nScrivi + se vuoi tutto maiuscolo e - se vuoi tutto minuscolo:");
            d=getchar();
            if (send(sockfd,&d,1,0)==-1){
                perror("send d fallita");
                exit(4);
            }
            printf("\nInvio la stringa al server...\n");
            if(send(sockfd,buf,len,0)==-1){
                perror("send fallita");
                exit(4);
            }
            if(recv(sockfd,buf,len,0)>0){
                printf("Ho ricevuto la risposta: %s\n",buf);
            }
            else{
                perror("seconda receive fallita");
                exit(3);
            }
            close(sockfd);
            exit(0);
        }

SERVER:

Code:
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <ctype.h>
#include <sys/socket.h>
#include <string.h>
#include <signal.h>
#include <netinet/in.h>
#define MAXLENGTH 80
#define SERVERPORT 1313

void minuscolatore (int in,int out){
    char inputline[MAXLENGTH];
    int len,i;
    while((len=recv(in,inputline,MAXLENGTH,0))>0){
        for(i=0;i<len;i++){
            inputline[i]=tolower(inputline[i]);
        }
        send(out,inputline,len,0);
    }
}
void maiuscolatore(int in,int out){
    char inputline[MAXLENGTH];
    int len,i;
    while((len=recv(in,inputline,MAXLENGTH,0))>0){
        for(i=0;i<len;i++){
            inputline[i]=toupper(inputline[i]);
        }
        if (strcmp(inputline,"FINE")==0){
            exit(0);
        }
        send(out,inputline,len,0);
    }
}
int main(){
    int sock,client_len,fd;
    char c;
    struct sockaddr_in client, server = {AF_INET,htons(SERVERPORT),INADDR_ANY};
    if((sock=socket(AF_INET,SOCK_STREAM,0))==-1){
        perror("Socket fallita");
        exit(1);
    }
    if(bind(sock,(struct sockaddr *)&server,sizeof server)==-1){
        perror("Bind fallita");
        exit(2);
    }
    listen(sock,5);
    while(1){
        client_len=sizeof(client);
        if((fd=accept(sock,(struct sockaddr *)&client,&client_len))<0){
            perror("accept fallita");
            exit(3);
        }
        if (recv(fd,&c,1,0)==-1){
            perror("recv server fallita");
            exit(4);
        }
        if (c=='+'){
        switch(fork()){
            case -1:
                perror("Fork fallita");
                exit(4);
            case 0:
                printf("Aperta connessione\n");
                maiuscolatore(fd,fd);
                printf("Chiusa connessione\n");
        }
    }
        else if (c=='-'){
        switch(fork()){
            case -1:
                perror("Fork fallita");
                exit(4);
            case 0:
                printf("Aperta connessione\n");
                minuscolatore(fd,fd);
                printf("Chiusa connessione\n");
        }   
        }
        close(fd);
    }
}

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to scp File from root user in one server to say crt user in another server and avoid password?

Can someone help in writing some script through which I can transfer file (scp) from root user in abc server to crt user in hfg server and can give the crt user password in script itself so that it doesn't prompt me every time for password (4 Replies)
Discussion started by: Moon1234
4 Replies

2. Solaris

Ssh to Solaris 10 server - close connection

Can ssh to server. Asks for password. Then seems to time out and close the connection. Any ideas? ---------- Post updated at 09:30 AM ---------- Previous update was at 07:51 AM ---------- Here is output from ssh -vvv -l user <IPaddress> debug3: packet_send2: adding 64 (len 59 padlen 5... (4 Replies)
Discussion started by: psychocandy
4 Replies

3. Shell Programming and Scripting

Need to delete large set of files (i.e) close to 100K from a directory based on the input file

Hi all, I need a script to delete a large set of files from a directory under / based on an input file and want to redirect errors into separate file. I have already prepared a list of files in the input file. Kndly help me. Thanks, Prash (36 Replies)
Discussion started by: prash358
36 Replies

4. Shell Programming and Scripting

How to get the user input recursively until the user provides valid input

Hi, echo "Enter file name of input file list along with absolute path : " read inputFileList if then for string in `cat inputFileList` do echo $string done else echo " file does not exist" fi From the above code, if the user enters a invalid file... (1 Reply)
Discussion started by: i.srini89
1 Replies

5. Shell Programming and Scripting

NAWK conversion of hexadecimal input to decimal output via printf, I am close I can feel it

I have searched and the answers I have found thus far have led me to this point, so I feel I am just about there. I am trying to convert a column of hexadecimal to decimal values so that I can filter out via grep just the data I want. I was able to pull my original 3 character hex value and... (10 Replies)
Discussion started by: PCGameGuy
10 Replies

6. Programming

Client accidently close when the server crash

The steps to test the problem 1. Open TCP Server 2. Open TCP Client 3. TCP Client sends data to Server. 4. Close TCP Server and the client also crash without any notification Second wonderful test: 1. Comment the following statement in Client.c (at line 168) and compile it Writen(... (1 Reply)
Discussion started by: sehang
1 Replies

7. Programming

when parent process close, how to close the child?

can someone provide an example, where if the parent process quits for any reason, then the child process will also close? (3 Replies)
Discussion started by: omega666
3 Replies

8. Shell Programming and Scripting

Usage of NOHUP - How to keep the child process running even if I close the Server connection

Hi. ! When I use the 'NOHUP' along with the '&', the process will be running in the background. Even when I attempt to close (Meaning 'EXIT') the session (say PUTTY in this case), it wont exit unless the process is completed. But, say when I forcefully terminate the session (SHUT DOWN the... (2 Replies)
Discussion started by: WinBarani
2 Replies

9. AIX

User cant log in (close terminal)

Hello everyone I have a user on my server, Aix 5.3 TL9 sp4. Weeks ago I dont have a problem but today the user cannot log in. let me explain. Me with root user I can change his password. then I log in with the user and I can change the password and the terminal close. Im using ssh. But... (2 Replies)
Discussion started by: lo-lp-kl
2 Replies

10. What is on Your Mind?

Test: can a user close his own thread?

Just a test thread. (0 Replies)
Discussion started by: Perdy
0 Replies
Login or Register to Ask a Question