Re: Read/Write socket Problem

From: Xarky (bernardpace_at_yahoo.com)
Date: 05/12/05


Date: 12 May 2005 06:04:28 -0700


"David Schwartz" <davids@webmaster.com> wrote in message news:<d5fscf$1m8$1@nntp.webmaster.com>...
> "Xarky" <bernardpace@yahoo.com> wrote in message
> news:bc42e1a.0505060444.94a7150@posting.google.com...
>
> The problem is that you do not understand what you are sending and
> receiving at the byte level. You need to make sure your receiver expects to
> get and understands the same *bytes* the sender is supposed to send.
>
> DS

Hi,

Regarding the same problem again.
I am always writing 3 bytes from client, reading 3 bytes from server,
writing 1 byte from server and reading one byte from client.

The problem appears when the number entered is greater or equal to 10.
The bytes that I am sending should be '10\0'. 10 was taken as an
example
Now when I send a number smaller than 10, 2 bytes seems to be
transfered (9\0), while I am reading 3 bytes.

Can you please give me further explanation of the solution, because I
can't understand. Below is the main code that is being used for
communication.

Thanks in Advance

*** Server side ***
char givenNo[3];
char option;

do
{
  bzero(&givenNo, sizeof(givenNo));
  data = read(client_socket, &givenNo, sizeof(givenNo));
  ...
  bzero(&option, sizeof(option));
  option = 'W'; or option = 'G'; or option = 'S';
  ...
  data = write(client_socket, &option, sizeof(option));
} while(...);

*** Client side ***
char guess[3];
char reply;

do
{
  bzero(&guess, sizeof(guess));
  getline(guess, 3);

  data = write(socket_id, &guess, sizeof(guess));
  bzero(&reply, sizeof(reply));
  data = read(socket_id, &reply, sizeof(reply));
}
while(...);



Relevant Pages

  • Re: XNEWS posting problem
    ... > as Xnews thinks there is still an open connection... ... I tried all combinations --- reading a message just ... Xnews seems to be misreporting the message length, and then receiving the ... one because the server is waiting for more. ...
    (news.software.readers)
  • Re: Reading a Binary File....
    ... I have a problem in reading a binary file. ... int acountnum; ... char phone; ...
    (comp.lang.c)
  • Re: CAsyncSocket and Send
    ... The sending side sends an 'f' char immediately before a file is sent. ... > Are you using TCP/IP or UDP? ... > Are you making sure that, if you are handling strings, that you insert a terminating NUL ... >>in C and the receiving side is MFC. ...
    (microsoft.public.vc.mfc)
  • Re: typecasting
    ... there are n number of options in buffer type char* ... have created a char array of pointers to point to all these n ... struct authProt auth; ... Adjust for the endianness of the incoming data (you do not need to worry about the endianness of the receiving machine doing it this way). ...
    (comp.lang.c)
  • Re: Clarification required about select vs wake_up race condition
    ... Our char driverdoes a ... In our poll(), ... it looks like it can set the state of the receiving ... any) or just getting back (iow, the task doesn't lose a cpu). ...
    (Linux-Kernel)