473,396 Members | 1,843 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

socket returning gibbarish

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,si zeof(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
Nov 14 '05 #1
10 1487
balzano_1 <ba*******@yahoo.com> scribbled the following:
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 (pa*****@cc.helsinki.fi) ------------- 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
Nov 14 '05 #2
"balzano_1" <ba*******@yahoo.com> wrote in message
news:60**************************@posting.google.c om...
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.
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.


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

--
Stephen Sprunk "Stupid people surround themselves with smart
CCIE #3723 people. Smart people surround themselves with
K5SSS smart people who disagree with them." --Aaron Sorkin

Nov 14 '05 #3
balzano_1 wrote:

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)
Nov 14 '05 #4
balzano_1 wrote:

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

--
+-------------------------+--------------------+-----------------------------+
| Kenneth J. Brody | www.hvcomputer.com | |
| kenbrody at spamcop.net | www.fptech.com | #include <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------------+

Nov 14 '05 #5
CBFalconer <cb********@yahoo.com> wrote:

<snip>
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 ab***@worldnet.att.net (violation of RFC 1855).
Nov 14 '05 #6
On 2 May 2004 05:28:56 -0700, in comp.lang.c , so***********@yahoo.com
(Simon) wrote:
CBFalconer <cb********@yahoo.com> wrote:
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 ab***@worldnet.att.net (violation of RFC 1855).


Prat.
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html>
----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
Nov 14 '05 #7
Simon <so***********@yahoo.com> spoke thus:
Your signature is too long. It should be four lines at most.
Abuse report sent to ab***@worldnet.att.net (violation of RFC 1855).


Amazing.

*plonk*

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Nov 14 '05 #8
Simon wrote:
CBFalconer <cb********@yahoo.com> wrote:

<snip>
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 ab***@worldnet.att.net (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
--
Ro*************@rbdev.net |) |)
http://rb.rbdev.net |\.|).
Nov 14 '05 #9

In article <82**************************@posting.google.com >, so***********@yahoo.com (Simon) writes:

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

Abuse report sent to ab***@worldnet.att.net (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.

--
Michael Wojcik mi************@microfocus.com

Dude, it helps to be smart if you're gonna be mean. -- Darby Conley
Nov 14 '05 #10
mw*****@newsguy.com (Michael Wojcik) writes:
- 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."
Nov 14 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Bryan Olson | last post by:
Here's the problem: Suppose we use: import socket f = some_socket.makefile() Then: f.read() is efficient, but verbose, and incorrect (or at least does not play will with others);
9
by: Phil Jenson | last post by:
I am try to evaluate the most efficient method of handling thousands of simultaneous TCP connects each of which remain connected to the server for hours and pass a small amount of data usually once...
3
by: ferbar | last post by:
Hello all, This may sound pretty basic stuff.. but I'm working on a socket example whose client seems to work fine, but the server doesn't send to the client the expected result. The problem is...
13
by: coloradowebdev | last post by:
i am working on basically a proxy server that handles requests via remoting from clients and executes transactions against a third-party server via TCP. the remoting site works like a champ. my...
2
by: djc | last post by:
I read a network programming book (based on framework 1.1) which indicated that you should 'never' use the RecieveTimeout or the SendTimeout 'socket options' on TCP sockets or you may loose data. I...
0
by: phplasma | last post by:
Hey, I am currently attempting to implement a multi-threaded C# socket, using SSL (.pem file/certification/private key combo) server using Visual Studio C# Express. I have successfully made...
1
by: mirandacascade | last post by:
I noticed the following lines from the connect() method of the HTTPConnection class within httplib: for res in socket.getaddrinfo(self.host, self.port, 0, socket.SOCK_STREAM): af, socktype,...
6
by: billiejoex | last post by:
Hi there. I'm setting up test suite for a project of mine. situations, if the socket is closed on the other end or not. I noticed that I can "detect" such state if a call to socket.read() returns...
10
by: Hendrik van Rooyen | last post by:
While doing a netstring implementation I noticed that if you build a record up using socket's recv(1), then when you close the remote end down, the recv(1) hangs, despite having a short time out...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.