Thread: write() and read() of int type to sockets returning wrong values.

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    4

    write() and read() of int type to sockets returning wrong values.

    I have tried writing to a socket using write() command. The data I need to write is of structure type and it contains both char and int datatype values. But when I write the data to the server socket, the value of the int variable is getting corrupted. I am receiving a value as 16777216 always.
    I am writing from a Solaris machine to a Linux machine. Solaris is a 32 bit machine and Linux is a 64 bit machine. Do this have any impact on my problem.
    I used htonl/ntohl and vice versa for passing the integer values from and to the server. But how can I use this if I am writing the whole structure type into the socket?? Can anyone help me to sort this out??

    PCHAR data = NULL;
    int size = sizeof(WSMsg);
    int retry = 10;
    data = new char [sizeof(WSMsg)];
    memcpy(data, msg, sizeof(WSMsg));
    int n = write(fd, &data[0], size);

    This is my code part. Here WSMsg is of structure type and has int and char type values.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. write() and read() int values from socket returning wrong values.
    By Premjith P S in forum Linux Programming
    Replies: 8
    Last Post: 11-29-2012, 02:59 PM
  2. Using writev() instead of write() on sockets?
    By cpjust in forum C Programming
    Replies: 0
    Last Post: 11-22-2007, 02:11 PM
  3. FFT returning values in wrong order
    By DavidP in forum C# Programming
    Replies: 3
    Last Post: 10-25-2007, 01:15 PM
  4. Read and Write, not getting same values.
    By Wraith-Lunati in forum C++ Programming
    Replies: 9
    Last Post: 09-13-2006, 10:41 AM
  5. returning functions w/ sockets
    By JagWire in forum Windows Programming
    Replies: 4
    Last Post: 03-11-2002, 05:00 PM