Unix Programming - Read/Write socket Problem

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > May 2005 > Read/Write socket Problem





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author Read/Write socket Problem
Xarky

2005-05-06, 7:48 am

Hi,

My getline() method is below. I am using sockets;

void getline(char s[], int lim)
{
int c, i;

for (i=0; ((i<lim-1) && ((c=getchar()) != '\n')); i++)
s[i] = c;

s[i] = '\0';
}

The problem is that if I send data of 1 character(example 1), the
server replies with a 'G' with no problem. But if I send 9, and the
number randomized is smaller, problem described is occuring.


Xarky wrote:
> Hi,
> I am writing a client/server program with the use of sockets. I am
> encountering a strange problem. Below is given part of the code I am
> using, where the problem is occuring.
>
>
> **** Server Part ****
> // at this time client has already been connected, and made some
> communication between them succesfully.
> void myMethod()
> {
> char givenNo[3];
> int randomized;
> int gameNumber;
> int toNumber;
> ssize_t data;
> char option;
>
> bzero(&givenNo, sizeof(givenNo));
> data = read(client_socket, &givenNo, sizeof(givenNo));
> if (data == -1)
> close_due_to_error();
>
> toNumber = convert_to_number(givenNo); // this converts to numbers
> perfectly
> randomized = random_generator(toNumber); // this generates a
> randomized number
>
> do
> {
> bzero(&givenNo, sizeof(givenNo));
> data = read(client_socket, &givenNo, sizeof(givenNo));
> if (data == -1)
> close_due_to_error();
>
> gameNumber = convert_to_number(givenNo);
>
> bzero(&option, sizeof(option));
> if (gameNumber == randomized) // Won
> option = 'W';
> else if (gameNumber < randomized) // number is greater
> option = 'G';
> else if (gameNumber > randomized) // number is smaller
> option = 'S';
>
> if ((option == 'W') || (option == 'G') || (option == 'S'))
> {
> data = write(client_socket, &option, sizeof(option));
> if (data == -1)
> close_due_to_error();
> } // end if
> } while(option != 'W');
> }
>
> **** Client Part ****
> void clientMethod
> {
> char guess[3];
> char range[3];
> ssize_t data;
> int attempts;
> char reply;
>
> system("clear");
> printf("Enter the number for the range you want to play\n>> ");
> bzero(&range, sizeof(range));
> getline(range, 3);
>
> data = write(socket_id, &range, sizeof(range));
> if (data == -1)
> terminate_client();
>
> attempts = 0;
> printf("\n");
> getchar();
> do
> {
> reply = NULL;
> attempts++;
>
> // step 1
>
> printf("Try to guess randomized number\n>>");
> bzero(&guess, sizeof(guess));
> getline(guess, 3); // user must enter data here
>
> data = write(socket_id, &guess, sizeof(guess));
> if (data == -1)
> terminate_client();
>
> do
> {
> bzero(&reply, sizeof(reply));
> data = read(socket_id, &reply, sizeof(reply));
> if (data == -1)
> terminate_client();
> } while(!((reply == 'G') || (reply == 'S') || (reply == 'W')));
>
> // step 2
>
> if (reply == 'G')
> printf("Number to be guessed is greater.\n");
> else if (reply == 'S')
> printf("Number to be guessed is smaller.\n");
> else if (reply == 'W')
> printf("You won in %d attempts.\n", attempts);
> } while(reply != 'W'); getchar();
> }
>
> Now the problem that I am having is the following. When the server
> sends a reply of an 'S' message, the program is looping again, where
> step 1 and step 2 are executing immediately without the user
> intervention, and the reply being sent is then. After that program
> waits again for user intervention, and if 'S' reply is sent problem
> persits.
>
> When server sends a reply of 'G' or 'W', solution works perfect. Now
> in the server I am using the same write statement for any type of
> reply.
>
> A sample screenshot of what I am seeing is below:
> *********
> Enter the number for the range you want to play
>
>
>
> Try to guess randomized number
>
>
> Number to be guessed is smaller.
> Try to guess randomized number
>
>
> Try to guess randomized number
>
>
> Try to guess randomized number
>
> **********
>
>
> I hope someone understands my problem, and can help me solve my
> problem out.
> Thanks in Advance



In your code above, you're sending 3 characters whether or not
you have 3 characters to send. What effect, do you suppose
that garbage will have on your server?

--

Fletcher Glenn
David Schwartz

2005-05-06, 6:02 pm


"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


Xarky

2005-05-12, 7:56 am

"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(...);
James Antill

2005-05-12, 5:52 pm

On Thu, 12 May 2005 06:04:28 -0700, Xarky wrote:

> "David Schwartz" <davids@webmaster.com> wrote in message news:<d5fscf$1m8$1@nntp.webmaster.com>...
>
> 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.


Read my message from the 4th again. I specifically say what the problem
is in getline() (without even having seen the code!)

> 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.


Again, saying "smaller than 10" you are misleading yourself. It's all
about the number of bytes, you only have room to store 2 but you are
getting "10\n" ... this would work just as badly if you input "01\n".

--
James Antill -- james@and.org
http://www.and.org/vstr/httpd

Wayne C. Morris

2005-05-12, 8:48 pm

In article <bc42e1a.0505120504.41b546a2@posting.google.com>,
bernardpace@yahoo.com (Xarky) wrote:

> 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.


Forget the sockets and the server. As James said on the 4th, the problem
lies in your 'getline' function and the size of 'guess'.

Try adding a 'printf' after the call to 'getline'. Make it print all 3
bytes of 'guess' in hex, like so:

printf("guess contains %#x %#x %#x\n", guess[0], guess[1], guess[2]);

See what it prints when you type a 2-digit number. Then take a closer look
at your 'getline' function. And think about why you needed an extra call
to 'getchar' just before the client's outer 'do-while' loop.
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com