socket returning gibbarish

B

balzano_1

Hi, im trying to send text from one pc to anther using C socket
programming, i used send() to send the text, the text i used to send
is a result of a function, the problem is the other side recieves
gibbarish, here are server.c and client.c
------------
server.c
------------
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/wait.h>
#include <signal.h>
#include <sys/utsname.h>

#define MYPORT 3490

#define BACKLOG 10



char *sys_info() //the function returns version of system
{
char *computer=malloc(255*sizeof(char)), *rv=NULL;
struct utsname uts;

if(gethostname(computer, 255) != 0 || uname(&uts) < 0){
fprintf(stderr,"couldnt get host info");
free(computer);
exit(1);
}

rv=uts.version;
return rv;

}


void sigchld_handler(int s)
{
while(wait(NULL) > 0);
}

int main(void)
{
int sockfd, new_fd;
struct sockaddr_in my_addr;
struct sockaddr_in their_addr;
int sin_size;
struct sigaction sa;
int yes=1;


if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
perror("socket");
exit(1);
}

if (setsockopt(sockfd,SOL_SOCKET,SO_REUSEADDR,&yes,sizeof(int))
== -1) {
perror("setsockopt");
exit(1);
}

my_addr.sin_family = AF_INET;
my_addr.sin_port = htons(MYPORT);
my_addr.sin_addr.s_addr = INADDR_ANY;
memset(&(my_addr.sin_zero), '\0', 8);

if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct
sockaddr))

== -1) {
perror("bind");
exit(1);
}

if (listen(sockfd, BACKLOG) == -1) {
perror("listen");
exit(1);
}

sa.sa_handler = sigchld_handler; // reap all dead processes
sigemptyset(&sa.sa_mask);
sa.sa_flags = SA_RESTART;


if (sigaction(SIGCHLD, &sa, NULL) == -1) {
perror("sigaction");
exit(1);
}

while(1) { // main accept() loop
sin_size = sizeof(struct sockaddr_in);
if ((new_fd = accept(sockfd, (struct sockaddr
*)&their_addr,&sin_size)) == -1) {
perror("accept");
continue;
}
printf("server: got connection from %s\n",

inet_ntoa(their_addr.sin_addr));
if (!fork()) {
close(sockfd);
if (send(new_fd, sys_info(), strlen(sys_info()), 0) ==
-1)// this is where i pass the result of the function sys_info() to
send()
perror("ERROR SENDING THE MESSAGE");
close(new_fd);
exit(0);
}
close(new_fd);
}

return 0;
}

-----------
client.c
-----------
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <netdb.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>

#define PORT 3490

#define MAXDATASIZE 1000

int main(int argc, char *argv[])
{
int sockfd, numbytes;
char buf[MAXDATASIZE];
struct hostent *he;
struct sockaddr_in their_addr;
if (argc != 2) {
fprintf(stderr,"usage: client hostname\n");
exit(1);
}

if ((he=gethostbyname(argv[1])) == NULL) { // get the host
info
perror("gethostbyname");
exit(1);
}

if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
perror("socket");
exit(1);
}

their_addr.sin_family = AF_INET;
their_addr.sin_port = htons(PORT);
their_addr.sin_addr = *((struct in_addr *)he->h_addr);
memset(&(their_addr.sin_zero), '\0', 8);

if (connect(sockfd, (struct sockaddr *)&their_addr,
sizeof(struct sockaddr))
== -1) {
perror("connect");
exit(1);
}

if ((numbytes=recv(sockfd, buf, MAXDATASIZE-1, 0)) == -1) {
perror("recv");
exit(1);
}

buf[numbytes] = '\0';


printf("Received: %s\n",buf);

close(sockfd);

return 0;
}


===
it dosent allways return gibbarish, this is how the result looks like
when sys_info() is passed to send() as a message.
client 127.0.0.1
Received: #2 SM°Ôÿ¿i Feb 20 18:

when i put the code of the function directly into main() and removed
the function sys_info(), it worked fine with these results
client 127.0.0.1
Received: #2 SMP Fri Feb 20 18:11:42 GMT 2004


Im sure its obvious but im not that good in C, so if anyone can help
please do.

thanks
 
J

Joona I Palaste

balzano_1 said:
Hi, im trying to send text from one pc to anther using C socket
programming, i used send() to send the text, the text i used to send
is a result of a function, the problem is the other side recieves
gibbarish, here are server.c and client.c

ISO standard C does not support sockets. Please ask in
comp.unix.programmer.

--
/-- Joona Palaste ([email protected]) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"You can pick your friends, you can pick your nose, but you can't pick your
relatives."
- MAD Magazine
 
S

Stephen Sprunk

balzano_1 said:
char *sys_info() //the function returns version of system
{
char *computer=malloc(255*sizeof(char)), *rv=NULL;
struct utsname uts;

if(gethostname(computer, 255) != 0 || uname(&uts) < 0){
fprintf(stderr,"couldnt get host info");
free(computer);
exit(1);
}

rv=uts.version;
return rv;

} ....
it dosent allways return gibbarish, this is how the result looks like
when sys_info() is passed to send() as a message.
Received: #2 SM°Ôÿ¿i Feb 20 18:

when i put the code of the function directly into main() and removed
the function sys_info(), it worked fine with these results
Received: #2 SMP Fri Feb 20 18:11:42 GMT 2004


Im sure its obvious but im not that good in C, so if anyone can help
please do.

sys_info() returns the address of a variable on its local stack; using this
address after the function returns is undefined behavior.

Also, you have a memory leak -- you malloc memory for char *computer but
it's never freed. In fact, it doesn't appear to even be used.

I'd suggest:

char *sys_info(char *computer, size_t namelen) {
struct utsname uts;
if (gethostname(computer, namelen)) {
if (uname(&uts) < 0) {
fprintf(stderr,"couldnt get host info");
exit(1);
} else {
strncpy(computer, uts.version, namelen);
}
}
return computer;
}

This puts the burden of memory allocation on the caller. To use it, change:

if (send(new_fd, sys_info(), strlen(sys_info()), 0) == -1)

to:

char buffer[255];
...
sys_info(buffer, sizeof(buffer));
if (send(new_fd, buffer, strlen(buffer), 0) == -1)


S
 
C

CBFalconer

balzano_1 said:
Hi, im trying to send text from one pc to anther using C socket
programming, i used send() to send the text, the text i used to
send is a result of a function, the problem is the other side
recieves gibbarish, here are server.c and client.c
.... snip ...

Didn't you already post this, and get told it was off-topic here?
At any rate you will find some useful references below.

BTW, when you find a suitable group, try capitalizing the pronoun
'I'.

--
Some useful references:
<http://www.ungerhu.com/jxh/clc.welcome.txt>
<http://www.eskimo.com/~scs/C-faq/top.html>
<http://benpfaff.org/writings/clc/off-topic.html>
<http://anubis.dkuug.dk/jtc1/sc22/wg14/www/docs/n869/> (C99)
 
K

Kenneth Brody

balzano_1 said:
Hi, im trying to send text from one pc to anther using C socket
programming, i used send() to send the text, the text i used to send
is a result of a function, the problem is the other side recieves
gibbarish, here are server.c and client.c

Although the use of sockets is off-topic for c.l.c, the problem is
unrelated to the use of sockets.

[...]
char *sys_info() //the function returns version of system
{
char *computer=malloc(255*sizeof(char)), *rv=NULL;
struct utsname uts;

if(gethostname(computer, 255) != 0 || uname(&uts) < 0){
fprintf(stderr,"couldnt get host info");
free(computer);
exit(1);
}

rv=uts.version;
return rv;

}

You are returning a pointer to a local variable, so using that pointer
results in undefined behavior.

That, and you don't free(computer) on success, so you have a memory
leak.

[...]
if (send(new_fd, sys_info(), strlen(sys_info()), 0) ==
-1)// this is where i pass the result of the function sys_info() to
send()
[...]

Also, once you fix sys_info(), this should be changed to not call it
twice. (What happens on the off chance that different strings are
returned?)

Call it once and save the pointer in a variable. Then pass that variable
to send() and strlen().
 
S

Simon

BTW, when you find a suitable group, try capitalizing the pronoun
'I'.

Your signature is too long. It should be four lines at most.

Abuse report sent to (e-mail address removed) (violation of RFC 1855).
 
C

Christopher Benson-Manica

Simon said:
Your signature is too long. It should be four lines at most.
Abuse report sent to (e-mail address removed) (violation of RFC 1855).

Amazing.

*plonk*
 
R

Robert Bachmann

Simon said:
Your signature is too long. It should be four lines at most.

Abuse report sent to (e-mail address removed) (violation of RFC 1855).

CBFalconer's signature is 5 lines long, and RFC 1855 just says:
| - If you include a signature keep it short. Rule of thumb
| is no longer than 4 lines. Remember that many people pay for
| connectivity by the minute, and the longer your message is,
| the more they pay.

Best regards,
Robert Bachmann
 
M

Michael Wojcik

Your signature is too long. It should be four lines at most.

Abuse report sent to (e-mail address removed) (violation of RFC 1855).

Network Working Group
Request For Comments: 1855
FYI: 28
Category: Informational
...
This memo does not specify an Internet standard of any kind.

RFC 1855 is not a standard, nor on the standards track. As such
it cannot be "violated". (I suggest you read FYI 1, RFC 1150,
before accusing people of "violating" FYIs.)

Further:

- The signature length statement in 1855 is qualified as a "rule of
thumb". If 1855 specified compliance conditions, which it does not,
it wouldn't even be a conditional-compliance condition.

- The signature length statement in 1855 appears in section 2.1.1,
covering one-to-one email messages. There is no equivalent
statement in section 3.1.1, covering Usenet.
 
B

Ben Pfaff

- The signature length statement in 1855 is qualified as a "rule of
thumb". If 1855 specified compliance conditions, which it does not,
it wouldn't even be a conditional-compliance condition.

This is true.
- The signature length statement in 1855 appears in section 2.1.1,
covering one-to-one email messages. There is no equivalent
statement in section 3.1.1, covering Usenet.

This is bogus. You failed to read the introduction to section
3.0, which says, in part: "Any time you engage in One-to-Many
communications, all the rules for mail should also apply."
 

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,962
Messages
2,570,134
Members
46,690
Latest member
MacGyver

Latest Threads

Top