Click here to Skip to main content
15,888,454 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello everyone,

I'm a beginner's programmer and I don't understand why my write() function doesn't work with these arguments. That why i'm here.

When I execute my program, I got une error with my write function and with my read function. it's normal for my read function because it depends on the write function. I searched on Google for my arguments but I think they are good. So I don't understand why it doesn't work.

My script is on C language.

What I have tried:

C++
int main(int argc,char **argv)
{
	int sockfd,n;
	char sendline[100];
	char recvline[100];
	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(sendline,0, 100);
		memset( recvline,0, 100);

		fgets(sendline,100,stdin);

		if(write(sockfd,sendline,strlen(sendline)+1) < 0)
		{
			printf("The write function doesn't work !\n");
   		}
		if(read(sockfd,recvline,100) < 0)
		{
			printf("The read function doesn't work !\n");
		}
		printf("%s",recvline);
	}
}
Posted
Updated 2-Jan-19 23:21pm
v3
Comments
jsc42 2-Jan-19 16:56pm    
Capture the actual error code from the 'write' and include it in your error message. Then look up the code - that should give you a clue as to what the error is.
e.g.

int errcode;
if ((errcode = write(sockfd, sendline, strlen(sendline) + 1)) < 0)
printf("The write function returned %d\n", errcode);

Also, I suspect that you might need an '&' before the recvline argument in the read function (happy to be corrected if I am wrong).
Rick York 2-Jan-19 17:43pm    
No, the & is not necessary. An array without a subscript is the same as a pointer to the array so the & is not needed.
Member 14106234 2-Jan-19 17:48pm    
My error code for my read and write function is -1 and the & before the recvline doesn't change anything. For me, the problem in the read function is the "sockfd" argument because we already have problem with it on the write function.

In addition to what jsc42 wrote, call strerror() to get a textual description of the error and display that. Here is an example:
C++
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 :
C++
#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.
 
Share this answer
 
Comments
Member 14106234 2-Jan-19 17:58pm    
First of all, thanx for your answer. With strlen(errorCode) I get "Unknowm error". And thanx, I had not thought about the define.
Member 14106234 2-Jan-19 18:00pm    
And the -1 in the fgets and the +1 on the write call is for the terminating null character.
Rick York 2-Jan-19 20:55pm    
Are the write and read functions your own implementation? I believe the socket library functions are send and recv. Make sure you return the same values they return from your functions or strerror will not work correctly.

One more thing - with the last stuff I wrote I found that reading immediately following the write always failed. If I put a 1 millisecond delay in between the calls then it worked so you might want to give that a try. In the Win32 API the function is Sleep. I don't know what it's called in other OSs.
Member 14106234 3-Jan-19 5:55am    
The write and read function are on stdio.h, I tried with send and recv but I get the same error. And the Sleep function, it doesn't change anything
When you write to a socket file descriptor, it is similar to using the sendto function | Microsoft Docs[^] function. So you need to use the WSAGetLastError function to find out why it failed.
 
Share this answer
 
Comments
Member 14106234 3-Jan-19 9:59am    
With WSAGetLastError, I get, for the 3 error : 10093. So I used WSAStartup() and WSACleanup() because i forget that and i remplace write and read by send and recv. With that, my program work.
Thanks you all !

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900