|
Home > Archive > Unix Programming > April 2004 > help with socket code
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 |
help with socket code
|
|
| Robert Smith 2004-04-27, 9:34 pm |
| Here is my first attemped with sockets. All i want my programs to do is to
transfere Hello from one program to another. My code doesn't seem to work.
It looks like it just hangs. Any help would be appreciated.
"server"
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#define MYPORT 3490
int main()
{
int socket_id, newsocket;
int listen_return;
struct sockaddr_in my_addr;
struct sockaddr_in their_addr;
char *message = "Hello";
int len_of_message;
int bytes_sent;
socket_id =socket(AF_INET, SOCK_STREAM, 0);
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);
bind(socket_id, (struct sockaddr *)&my_addr,sizeof(struct sockaddr));
while(1)
{
if(listen(socket_id, 10)==-1)
{
printf("Connected\n");
newsocket=accept(socket_id, (struct sockaddr*)&their_addr,
sizeof(struct sockaddr_in));
len_of_message=strlen(message);
bytes_sent=send(newsocket,message,len_of
_message,0);
printf("Message sent!\n ");
printf("Number if bytes sent is%d",bytes_sent);
}
else
{
// printf("not connected\n");
}
usleep(1);
}
return 0;
}
********************************
client
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
//#define DEST_IP = 127.0.0.1;
//#define DEST_PORT = 3490;
int main()
{
struct sockaddr_in dest_addr;
int sock_fd;
int bytes_read;
char *message;
dest_addr.sin_family = AF_INET;
dest_addr.sin_port = htons(3490);
dest_addr.sin_addr.s_addr = htonl(INADDR_ANY);
memset(&(dest_addr.sin_zero), '\0', 8);
sock_fd = socket(AF_INET, SOCK_STREAM, 0);
connect(sock_fd, (struct sockaddr *) &dest_addr,sizeof(struct
sockaddr));
bytes_read=recv(sock_fd, message, 100, 0);
printf("bytes read is: %d",bytes_read);
return 0;
}
| |
| Eric Enright 2004-04-27, 11:34 pm |
| On Wed, 28 Apr 2004 01:42:30 +0000, Robert Smith wrote:
From the client:
> dest_addr.sin_addr.s_addr = htonl(INADDR_ANY);
You need to fill this is with information about the host you are trying to
connect to.
eg,
struct hostent *he;
if (NULL == (he = gethostbyname(host))) {
perror("gethostbyname");
return 1;
}
...
dest_addr.sin_addr = *((struct in_addr *)he->h_addr);
HTH,
--
Eric Enright /"\
sauronAtiptsoftDcom \ / ASCII Ribbon Campaign
X Against HTML E-Mail
Public Key: 0xBEDF636F / \
| |
| Nils O. Selåsdal 2004-04-28, 5:34 am |
| On Wed, 28 Apr 2004 01:42:30 +0000, Robert Smith wrote:
> Here is my first attemped with sockets. All i want my programs to do is to
> transfere Hello from one program to another. My code doesn't seem to work.
> It looks like it just hangs. Any help would be appreciated.
> //#define DEST_IP = 127.0.0.1;
> //#define DEST_PORT = 3490;
>
>
> int main()
> {
> struct sockaddr_in dest_addr;
> int sock_fd;
> int bytes_read;
> char *message;
>
> dest_addr.sin_family = AF_INET;
> dest_addr.sin_port = htons(3490);
> dest_addr.sin_addr.s_addr = htonl(INADDR_ANY);
You must tell which host to connect to.
The inet_addr can help you convert a standard IP address string
an in_addr_t.
Also, most calls return value indicate error or success.
If test the return value of each call, e.g.
ret = connect(...);
if(ret == -1){
perror("Connect failed");
exit(1);
}
You will much easier catch errors, and even get a descrition of
what's wrong.
> memset(&(dest_addr.sin_zero), '\0', 8);
Using sizeof() is usually better to describe sizes instead of your
hardcoded 8.
--
Nils Olav Selåsdal
System Engineer
w w w . u t e l s y s t e m s . c o m
| |
| Robert Smith 2004-04-28, 6:34 pm |
| doesn't this line tell it to connect to my computers IP address?
dest_addr.sin_addr.s_addr = htonl(INADDR_ANY);
thanks
"Nils O. Selåsdal" <NOS@Utel.no> wrote in message
news:pan.2004.04.28.09.06.39.90381@Utel.no...
> On Wed, 28 Apr 2004 01:42:30 +0000, Robert Smith wrote:
>
to[vbcol=seagreen]
work.[vbcol=seagreen]
>
> You must tell which host to connect to.
> The inet_addr can help you convert a standard IP address string
> an in_addr_t.
> Also, most calls return value indicate error or success.
> If test the return value of each call, e.g.
> ret = connect(...);
> if(ret == -1){
> perror("Connect failed");
> exit(1);
> }
> You will much easier catch errors, and even get a descrition of
> what's wrong.
>
> Using sizeof() is usually better to describe sizes instead of your
> hardcoded 8.
>
>
> --
> Nils Olav Selåsdal
> System Engineer
> w w w . u t e l s y s t e m s . c o m
>
>
| |
| Robert Smith 2004-04-28, 6:34 pm |
| doesn't this line tell it to connect to my computers IP address?
dest_addr.sin_addr.s_addr = htonl(INADDR_ANY);
thanks
"Eric Enright" <sauron@tiptsoft.com> wrote in message
news:pan.2004.04.28.03.05.23.624956@tiptsoft.com...
> On Wed, 28 Apr 2004 01:42:30 +0000, Robert Smith wrote:
>
> From the client:
>
> You need to fill this is with information about the host you are trying to
> connect to.
>
> eg,
>
> struct hostent *he;
>
> if (NULL == (he = gethostbyname(host))) {
> perror("gethostbyname");
> return 1;
> }
>
> ...
>
> dest_addr.sin_addr = *((struct in_addr *)he->h_addr);
>
>
> HTH,
> --
> Eric Enright /"\
> sauronAtiptsoftDcom \ / ASCII Ribbon Campaign
> X Against HTML E-Mail
> Public Key: 0xBEDF636F / \
>
| |
| Andrew Falanga 2004-04-28, 9:34 pm |
| On Wed, 28 Apr 2004 01:42:30 GMT
"Robert Smith" <laclac01@yahoo.com> wrote:
Replies are in-line (mostly):
> Here is my first attemped with sockets. All i want my programs to do
> is to transfere Hello from one program to another. My code doesn't
> seem to work. It looks like it just hangs. Any help would be
> appreciated.
>
> "server"
>
> #include <stdio.h>
> #include <stdlib.h>
> #include <sys/types.h>
> #include <sys/socket.h>
> #include <arpa/inet.h>
> #include <netinet/in.h>
>
> #define MYPORT 3490
>
> int main()
> {
> int socket_id, newsocket;
> int listen_return;
> struct sockaddr_in my_addr;
> struct sockaddr_in their_addr;
> char *message = "Hello";
> int len_of_message;
> int bytes_sent;
>
>
> socket_id =socket(AF_INET, SOCK_STREAM, 0);
> 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);
>
> bind(socket_id, (struct sockaddr *)&my_addr,sizeof(struct
> sockaddr));
>
> while(1)
> {
>
> if(listen(socket_id, 10)==-1)
> {
>
Improper use of return codes above. You're saying if returned value is
equal to -1 do the below. Only problem is, if the return value is -1
that means listen failed.
> printf("Connected\n");
>
> newsocket=accept(socket_id, (struct sockaddr*)&their_addr,
> sizeof(struct sockaddr_in));
>
> len_of_message=strlen(message);
>
> bytes_sent=send(newsocket,message,len_of
_message,0);
>
> printf("Message sent!\n ");
> printf("Number if bytes sent is%d",bytes_sent);
>
> }
> else
> {
> // printf("not connected\n");
> }
> usleep(1);
Why are you sleeping for 1 millisecond?
>
> }
>
> return 0;
> }
>
> ********************************
> client
>
> #include <stdio.h>
> #include <stdlib.h>
> #include <sys/types.h>
> #include <sys/socket.h>
> #include <netinet/in.h>
> #include <string.h>
>
> //#define DEST_IP = 127.0.0.1;
> //#define DEST_PORT = 3490;
>
>
> int main()
> {
> struct sockaddr_in dest_addr;
> int sock_fd;
> int bytes_read;
> char *message;
>
> dest_addr.sin_family = AF_INET;
> dest_addr.sin_port = htons(3490);
> dest_addr.sin_addr.s_addr = htonl(INADDR_ANY);
Just to call your attention to this. Here in the client you transfer
the value of INADDR_ANY to network byte order, but you don't in the
server code. Ordinarily, this is a problem, but since you're trying to
assign it the value of INADDR_ANY (which apparently is defined as NULL,
or zero) this isn't the problem.
I see above that you have commented the 127.0.0.1 address. Why not
#define it in both server and client code and see what happens.
> memset(&(dest_addr.sin_zero), '\0', 8);
> sock_fd = socket(AF_INET, SOCK_STREAM, 0);
>
>
> connect(sock_fd, (struct sockaddr *) &dest_addr,sizeof(struct
> sockaddr));
> bytes_read=recv(sock_fd, message, 100, 0);
You will have a problem here. Above you've declared message as a
pointer to type char. However, it's uninitialized effectively pointing
nowhere. You need to set aside memory for it with malloc(). Don't
forget the corresponding free() either.
> printf("bytes read is: %d",bytes_read);
> return 0;
>
> }
>
>
>
You need to apply some good'ol debugging techniques. Put in some
printf() or puts() statements to see how far your code actually gets.
Then, go from there. I must admit to being a little out of my element
in that I've not every tried using INADDR_ANY before. I've found David
Schwartz to be a good resource, however, for network programming. What
he wrote should be good, though again I must admit I'm ignorant of the
SuS letters. Don't know what they mean.
HTH,
Andy
-----------
A common mistake that people make when trying to design something
completely foolproof is to underestimate the ingenuity of complete
fools. -Douglas Adams
-----= Posted via mcse.ms, Uncensored Usenet News =-----
http://www.mcse.ms - The #1 Newsgroup Service in the World!
-----== Over 100,000 Newsgroups - 19 Different Servers! =-----
| |
| Robert Smith 2004-04-28, 10:34 pm |
| htons in the client was my biggest problem. I am not making anything with
the program I just want to see how the stuff works. I was sleeping for I
millisecond because it would suck up like 8% of cpu if I didn't. is there
another way so it doesn't suck up so much cpu ?
thanks
"Andrew Falanga" <andy@spam.me.not> wrote in message
news:20040428125902.4042690b.andy@spam.me.not...
> On Wed, 28 Apr 2004 01:42:30 GMT
> "Robert Smith" <laclac01@yahoo.com> wrote:
>
> Replies are in-line (mostly):
>
> Improper use of return codes above. You're saying if returned value is
> equal to -1 do the below. Only problem is, if the return value is -1
> that means listen failed.
>
>
>
>
> Why are you sleeping for 1 millisecond?
>
> Just to call your attention to this. Here in the client you transfer
> the value of INADDR_ANY to network byte order, but you don't in the
> server code. Ordinarily, this is a problem, but since you're trying to
> assign it the value of INADDR_ANY (which apparently is defined as NULL,
> or zero) this isn't the problem.
>
> I see above that you have commented the 127.0.0.1 address. Why not
> #define it in both server and client code and see what happens.
>
>
> You will have a problem here. Above you've declared message as a
> pointer to type char. However, it's uninitialized effectively pointing
> nowhere. You need to set aside memory for it with malloc(). Don't
> forget the corresponding free() either.
>
>
>
> You need to apply some good'ol debugging techniques. Put in some
> printf() or puts() statements to see how far your code actually gets.
> Then, go from there. I must admit to being a little out of my element
> in that I've not every tried using INADDR_ANY before. I've found David
> Schwartz to be a good resource, however, for network programming. What
> he wrote should be good, though again I must admit I'm ignorant of the
> SuS letters. Don't know what they mean.
>
> HTH,
> Andy
>
>
>
> -----------
> A common mistake that people make when trying to design something
> completely foolproof is to underestimate the ingenuity of complete
> fools. -Douglas Adams
>
>
> -----= Posted via mcse.ms, Uncensored Usenet News =-----
> http://www.mcse.ms - The #1 Newsgroup Service in the World!
> -----== Over 100,000 Newsgroups - 19 Different Servers! =-----
| |
| David Schwartz 2004-04-28, 11:34 pm |
|
"Robert Smith" <laclac01@yahoo.com> wrote in message
news:lUZjc.123185$M3.110743@twister.nyroc.rr.com...
> htons in the client was my biggest problem. I am not making anything with
> the program I just want to see how the stuff works. I was sleeping for I
> millisecond because it would suck up like 8% of cpu if I didn't. is there
> another way so it doesn't suck up so much cpu ?
> thanks
Why are you calling 'listen' more than once?
What does your code look like now?
DS
|
|
|
|
|