In addition to what jsc42 wrote, call strerror() to get a textual description of the error and display that. Here is an example:
int errorCode;
errorCode = write( sockfd, sendline, strlen( sendline ) + 1 );
if( errorCode < 0 )
{
fprintf( stderr, "write function failed %d : %s\n", errorCode, strerror( errorCode ) );
}
One more thing - I noticed you have used the "magic number" 100 many places. That is not a good idea. What happens if you decide to support up to 128 characters? You have a whole bunch of places where it has to change and there is a possibility of missing one. It is much better to use a definition in C or a constant value in C++. Here is the code using a macro definition :
#define BUFFER_SIZE 100
int main(int argc,char **argv)
{
int sockfd,n;
char sendline[BUFFER_SIZE];
char recvline[BUFFER_SIZE];
struct sockaddr_in servaddr;
sockfd=socket(AF_INET,SOCK_STREAM,0);
memset(&servaddr, 0, sizeof (servaddr));
servaddr.sin_family=AF_INET;
servaddr.sin_port=htons(22000);
inet_pton(AF_INET,"127.0.0.1",&(servaddr.sin_addr));
printf("%d\n", inet_pton(AF_INET,"198.168.1.19",&(servaddr.sin_addr)));
connect(sockfd,(struct sockaddr *)&servaddr,sizeof(servaddr));
while(1)
{
memset( recvline, 0, sizeof( recvline ) );
memset( sendline, 0, sizeof( sendline ) );
fgets( sendline, sizeof( sendline )-1, stdin );
if( write( sockfd, sendline, strlen(sendline) + 1 ) < 0 )
{
printf("The write function doesn't work !\n");
}
if( read( sockfd, recvline, sizeof( sendline ) - 1 ) < 0 )
{
printf("The read function doesn't work !\n");
}
printf( "%s",recvline );
}
}
Now if you want to have different maximum size you change only one thing and nothing else.
Did you notice the -1 in the fgets and read calls? They are there for the same reason you have a +1 on the write call - to account for the terminating null character.