Hi,
i have a client which reads from the socket:

for (index=0;index<SCALERS;index++)
	{
		read(my_socket,vectorScalerPointer,BUFLEN);
		read(my_socket,resultScalerPointer,BUFLEN);
		sum+= atoi(vectorScaler)*atoi(resultScaler);
	}

and a server program which writes to it.

for (index=0;index<SCALERS;index++)
	{
		sprintf(ascii,"%d",(*matrixStruct)._vector[0][index]);
		write(fd,ascii,BUFLEN);
		sprintf(ascii,"%d",(*matrixStruct)._mat[atoi(whichRow)][index]);
		write(fd,ascii,BUFLEN);

	}

now the problem is the programs are in a "race condition"
usualy the server writes into the buffer all the words before the client
starts reading them.
is there anyway (NOT SLEEPING) that the server writes into buffer
and then waits until someone READS from the buffer?
something like wait() or isempty(socket buffer)
?

THANK YOU.
config :

socket(PF_INET, SOCK_STREAM, 0);
bind(main_socket,(struct sockaddr *)&my_addr, sizeof(my_addr));
listen(main_socket, QUEUE_SIZE);

Recommended Answers

All 2 Replies

Well, I don't know what BUFLEN is but if you set your reads and writes to a specific size (i.e. sizeof(data_type) ) then you don't need to worry about how much is written and when since the SOCK_STREAM guarantees proper order and the sizes are fixed.
If you can not reliably guarantee that each side will see sizeof (data_type) as the same size you can place headers on your data to indicate how much to read. For instance:

Send: [4,X] // where sizeof(X) == 4
Recv: 
   read 1 byte: => 4 // indicates the remainder of the buffer
   read 4 bytes: => X

In both of these cases it is not necessary to convert the number to text before sending and the reverse the process on the receive side. Just send the number using the methods above as a means to ensure reading the proper amount of data on the remote side.

thank you. finaly i used ctl function to check whenever the socket is empty
and only than continue. probably not the best solution but it works

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.