problem and understanding the client server in c

F

fakessh

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

hello newsgroups

I'm working on a client server in c
the base c is the source of the deposit github
that here

https://github.com/liuxj/client-server-socket

I did check the sources they seem perfectly consistent with
standard of writing in c

compile on my machine and my server s performs perfectly
well

but yet I can not connect the client to the server
hosted on a remote machine

can you help me solve the problem of connection to the server
from the client


cordially
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.5 (GNU/Linux)
Comment: Using GnuPG with Fedora - http://enigmail.mozdev.org/

iD8DBQFM//7FtXI/OwkhZKcRAnWFAJ43iEMjv0/+l3beR1kr1pDaRgvYzACdEaCu
MxkZa88Mn2BLrqENyWM/404=
=AhiY
-----END PGP SIGNATURE-----
 
S

Seebs

I did check the sources they seem perfectly consistent with
standard of writing in c

If you're doing network code, you're using something far outside the
C standard.
can you help me solve the problem of connection to the server
from the client

If you want me to look at a large hunk of code and search through
it for bugs, you'll have to pay me. If you can reduce this to a
small (think twenty or thirty lines tops) example which does not rely
on any extensions such as networking functionality, which behaves in
a way you find surprising, feel free to post it here.

If your problem only exists when you are trying to use networking
functionality, the chances are your problem is about networking, and
unrelated to the language you're writing in.

-s
 
K

Keith Thompson

fakessh said:
hello newsgroups

Plural? How many did you post to?
I'm working on a client server in c
the base c is the source of the deposit github
that here

https://github.com/liuxj/client-server-socket

I did check the sources they seem perfectly consistent with
standard of writing in c

I took a *very* quick look.

You're defining (not just declaring) functions in your common.h file.
This is poor practice unless you have some specific reason to do it
this way. Suggestion: declare the functions in common.h, define them
in common.c, and link common.o together with your main program(s).

Much of what you're doing depends on things defined by POSIX,
not by C ("#include <unistd.h>" is a strong hint). If you can
narrow your problem down to a short self-contained program (say,
20-30 lines), comp.unix.programmer would be a good place to post it.

I suggest not posting PGP-signed messages to Usenet. Not all
newsreaders handle them gracefully, and I don't see any great
need for security for something like this. (And a lot of people
are seriously annoyed by anything other than plain text on Usenet
discussion groups, so apart from being potentially rude it's likely
to limit the number of people willing to help.)
 
J

Jens Thoms Toerring

fakessh said:
I'm working on a client server in c
the base c is the source of the deposit github
that here

I did check the sources they seem perfectly consistent with
standard of writing in c
compile on my machine and my server s performs perfectly
well

How do you know? All I would give it in terms of "performing
prefectly well" might be that it doesn't crash immediately,
but that's a pretty low standard for "perfectly well";-)
but yet I can not connect the client to the server
hosted on a remote machine
can you help me solve the problem of connection to the server
from the client

As others have pointed out it not standard C what you're
using - you use lots of POSIX extensions to C (and you need
to since C has no built-in functions for networking). Thus
it would be reasonable to ask in e.g. comp.unix.programmer
instead.

I also took a short look at the code and there are quite a
number of things that don't make to much sense also from a C
point of view. At least defining functions in header files
already has been mentioned as a no-no. But let's look some-
thing from client.c. You have there
char buf[MAXLINE]; ....
printf("\n\n---input message:\n");
memset(buf, 0, sizeof(buf));

This is completely useless.
scanf("%s",buf);
if(sizeof(buf) > MAXLINE) {

This can never be the case. You defined 'buf' to have
'MAXLINE' chars and that's not going to be changed by
anything you do later, thus sizeof(buf) will always
be 'MAXLINE' (as long as you do it in the same function
you defined the 'buf' array in). Did you perhaps confuse
the sizeof operator with the strlen() function?
printf("\n*** message is too large*** \n\n");

Assuming you meant to use strlen() and not the sizeof ope-
rator, it's actually too late. Once you read in more cha-
racters than 'buf' can hold you already have an evil bug.
What you need to do is restrict the number of characters
you read in in order to avoid a buffer overflow.
close(sockfd);
exit(1);
}
if((write(sockfd, buf, sizeof(buf)) ) == -1) {
{ ....
}

Not a C question, but anyway: why write the whole buffer even
if there is perhaps only a single character in it? A more sane
approach would seem to send first the number of bytes you're
going to send and then the string (use strlen() then instead
of sizeof).

The server part doesn't seem to make much sense. It looks
like a lot of misunderstandings about how select() works
(note that this isn't a C function but a POSIX extension)
and all in all much too complicated. What about boiling it
down to something simple that doesn't do anything but lis-
tening and reading on a socket? As far as I can see you
actually don't ever read on the socket when select() tells
you that there's something to read from it. But that's all
pretty off-topic here, comp.unix.programmer would be way
more suitable for discussions about it.

But even if everything would work perfectly well with regard
to the networking stuff already the few lines for reversing
the string the server received from the client (which it then
obviously is meant to send back) are broken:
x = strlen(buf);
memset(buf_client, 0, MAXLINE);
for (j = 0; x>=0; x--, j++)
buf_client[j] = buf[x]

After this the very first character in 'buf_client' is the
'\0' character (but then there's none at the end)! This sug-
gests strongly that something not very well thought through
is done here. Perhaps it would be instructive to write a
simple reverse() function and test it without all the net-
working stuff before going any further?

But the whole code suffers so much from mixing string lengths
with buffer lengths etc. that one at best can roughly guess
at what the intensions might have been. There's not much I can
see that would make too much sense - at least nothing that I
would call to "work perfectly well".

BTW, the code was uploaded nearly 9 months ago. If it's by
you why come here that much later? And if it's not by you
(as I guess and hope) I would recommend strongly to find
something better than this for trying to learn how to do
network programming. There are a number of good books on
this topic or, if you can't get your hands on them, also a
number of useful tutorials on the net.

Regards, Jens
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top