Problem transport endpoint is not connected | Unix Linux Forums | Programming

  Go Back    


Programming Post questions about C, C++, Java, SQL, and other programming languages here.

Problem transport endpoint is not connected

Programming


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 01-06-2012
tafazzi87 tafazzi87 is offline
Registered User
 
Join Date: Oct 2011
Last Activity: 2 May 2012, 10:38 AM EDT
Posts: 30
Thanks: 3
Thanked 0 Times in 0 Posts
[SOLVED]Problem transport endpoint is not connected

i've made a simple program that change a string from lowercase to uppercase and from uppercase to lowercase. Server works until start client, after client run server give this error: "recv server fallita: Transport endpoint is not connected" why? i think that stream closed too soon or not? below there is code of 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 <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]);
                }
                if (strcmp(inputline,"fine")==0){
                    break;
                }
                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){
                    break;
                }
                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(sock,&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");
                        send(fd,"Benvenuto al maiuscolatore, minuscolatore\n",27,0);
                        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");
                        send(fd,"Benvenuto al maiuscolatore, minuscolatore\n",27,0);
                        minuscolatore(fd,fd);
                        printf("Chiusa connessione\n");
                }   
                }
            }
            close(fd);
        }

and the client:

#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)
buf[i++]=c;

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

Last edited by tafazzi87; 01-06-2012 at 11:46 AM..
Sponsored Links
    #2  
Old 01-06-2012
Corona688 Corona688 is offline Forum Staff  
Mead Rotor
 
Join Date: Aug 2005
Last Activity: 24 July 2014, 12:41 AM EDT
Location: Saskatchewan
Posts: 18,972
Thanks: 747
Thanked 3,137 Times in 2,949 Posts
You're reading from the wrong socket. After you do fd=accept(sock, ...) you should be reading from fd, not sock.
The Following User Says Thank You to Corona688 For This Useful Post:
tafazzi87 (01-06-2012)
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
What is the difference between softerrors,harderrors,transport errors? tv.praveenkumar Solaris 3 06-03-2010 07:16 PM
SC3.2 issue - cluster transport configuration not right - resulting fail frustin Solaris 19 01-11-2010 03:22 AM
grep error: range endpoint too large Timmää Shell Programming and Scripting 2 12-16-2009 02:40 PM
Transport Error solaris5.10 UNIX for Advanced & Expert Users 1 11-18-2009 05:49 AM
Transport to another server within a script. DeCoTwc Shell Programming and Scripting 2 09-30-2008 12:45 AM



All times are GMT -4. The time now is 06:40 AM.