Click here to Skip to main content
15,886,018 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
i need help to send unsigned character over tcp in unix
but it case error in this line
C++
void Write(char * a,int size)
{
  unsigned char dt []=a;// erorr
  write(Socket,a,2); // error
 
}
Posted
Updated 21-Jun-15 21:50pm
v5
Comments
[no name] 26-Oct-14 10:38am    
Do you really think tcp does bother about data types?
Mahmoud_Gamal 27-Oct-14 4:02am    
not tcp but the data in accept in write function
CPallini 26-Oct-14 10:43am    
Provided Socket is a file descriptor (that is a int) the line
write(Socket, dt, 2);
should compile fine. What is the exact error message you are getting?
Mahmoud_Gamal 27-Oct-14 4:31am    
it compile fine but i need to send unsigned char
i will update question
sorry for this
CPallini 27-Oct-14 10:11am    
unsigned char dt []=a;
Is of course wrong. What are trying to do?

Just pass the file descriptor, arr and size to the write function. The type of the buffer pointer does not care as far as the count parameter specifies a valid number of bytes contained in the buffer. See also man 2 write[^].

The write function just transfers data. It does not care what kind of data are transferred. For that reason the type of the second parameter is void *.
 
Share this answer
 
Internally, a signed and an unsigned type are the same. The char you send is just 8 bits of data, just like an unsigned char. Therefore,
C
void Write(char * arr,int size)
{
  unsigned char* dt = (unsigned char*)a; // Cast to the type you want
  write(Socket,dt,size);
}

will send exactly the same data as
C
void Write(char * arr,int size)
{
  write(Socket,arr,size);
}
 
Share this answer
 
Comments
Mahmoud_Gamal 23-Jun-15 3:50am    
but i see unlike when i send data in range unsigned not signed and i receive wrong data i do like your solution but doesn't work well
i solve it temporary like this
C++
void WriteU(unsigned   char buff[], int size)
{
    int h ;

        h = write( m_Client->Socket,buff,size);
//          printf("\n data write \n %i",h);

}

why i tired my self if there is another way
 
Share this answer
 
Your Write function takes a character pointer as input, so trying to use that as an unsigned pointer makes no sense. You should just use the standard socket write function as described in http://man7.org/linux/man-pages/man2/write.2.html[^].
 
Share this answer
 

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