Ask a Question related to UNIX Programming, Design and Development.
-
Dave Hughes #1
newbie to network programming
Hey there,
I'm having trouble with this very basic network program. All it does is wait
for a connection to my machine on port 4000. THen when it gets a connection
it sends the string Hello World to the caller. I am having problems in that
when the program gets to the send function I get the error socket operation
on nonsocket and I cant work out why this is since newsocket is an integer,
given a value by accept.
Here is my source, all one main function. As I said I'm just starting out on
network programming, but have a big background in C.
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
int mysocket, newsocket;
struct sockaddr_in myaddr;
struct sockaddr_in calleraddr;
int sin_size;
close(mysocket);
myaddr.sin_family=AF_INET;
myaddr.sin_port=htons(4000);
myaddr.sin_addr.s_addr=inet_addr("192.168.1.3");
memset(&(myaddr.sin_zero),'\0',8);
mysocket=socket(AF_INET, SOCK_STREAM, 0);
if (bind(mysocket, (struct sockaddr *)&myaddr, sizeof(struct sockaddr))==
-1) {
perror("bind");
exit(1);
}
if (listen(mysocket, 5)==-1)
{
perror("listen");
exit(1);
};
while(1)
{
sin_size = sizeof(struct sockaddr_in);
if (newsocket = accept(mysocket, (struct sockaddr *)&calleraddr,
&sin_size)==-1)
{
perror(accept);
exit(1);
};
if (send(newsocket, "Hello, world!\n", 14, 0) == -1) perror("send");
printf("server: got connection from
%s\n",inet_ntoa(calleraddr.sin_addr));
close(newsocket);
};
close(mysocket);
return EXIT_SUCCESS;
}
Dave Hughes Guest
-
clueless newbie to web programming
I have never coded in perl or mod_perl before and have a project that needs me to do so. It is a web data entry system, using mod_perl to generate... -
Book review: Network Programming With Perl by Lincoln Stein
http://www.amazon.com/exec/obidos/tg/detail/-/0201615711/qid=1074699239/sr=1 -1/ref=sr_1_1/102-8963439-4561724?v=glance&s=books Drieux and others... -
network programming?
On Dec 2, 2003, at 1:22 PM, McMahon, Chris wrote: First off RUN AWAY! JUST SAY NO! DO NOT DO THIS! right, so much for the obligatory... -
Need Perl teacher/school: Network programming
For months (actually years) I have been trying to learn how to write a Perl proxy that can do the following: intercept *all* HTTP and HTTPS traffic... -
Problem with configure script in Unix network programming book by Stevens
After downloading the source from the website, I tried running the configure script. All I got is: : command not found : command not found :... -
Dave Hughes #2
newbie to network programming
Hey there,
I'm having trouble with this very basic network program. All it does is wait
for a connection to my machine on port 4000. THen when it gets a connection
it sends the string Hello World to the caller. I am having problems in that
when the program gets to the send function I get the error socket operation
on nonsocket and I cant work out why this is since newsocket is an integer,
given a value by accept.
Here is my source, all one main function. As I said I'm just starting out on
network programming, but have a big background in C.
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
int mysocket, newsocket;
struct sockaddr_in myaddr;
struct sockaddr_in calleraddr;
int sin_size;
close(mysocket);
myaddr.sin_family=AF_INET;
myaddr.sin_port=htons(4000);
myaddr.sin_addr.s_addr=inet_addr("192.168.1.3");
memset(&(myaddr.sin_zero),'\0',8);
mysocket=socket(AF_INET, SOCK_STREAM, 0);
if (bind(mysocket, (struct sockaddr *)&myaddr, sizeof(struct sockaddr))==
-1) {
perror("bind");
exit(1);
}
if (listen(mysocket, 5)==-1)
{
perror("listen");
exit(1);
};
while(1)
{
sin_size = sizeof(struct sockaddr_in);
if (newsocket = accept(mysocket, (struct sockaddr *)&calleraddr,
&sin_size)==-1)
{
perror(accept);
exit(1);
};
if (send(newsocket, "Hello, world!\n", 14, 0) == -1) perror("send");
printf("server: got connection from
%s\n",inet_ntoa(calleraddr.sin_addr));
close(newsocket);
};
close(mysocket);
return EXIT_SUCCESS;
}
Dave Hughes Guest
-
Barry Margolin #3
Re: newbie to network programming
In article <xneWa.129$Am1.97@newsfep1-gui.server.ntli.net>,
Dave Hughes <donotresus@yahoo.co.uk> wrote:....>Hey there,
>
>I'm having trouble with this very basic network program. All it does is wait
>for a connection to my machine on port 4000. THen when it gets a connection
>it sends the string Hello World to the caller. I am having problems in that
>when the program gets to the send function I get the error socket operation
>on nonsocket and I cant work out why this is since newsocket is an integer,
>given a value by accept.
Classic error.> if (newsocket = accept(mysocket, (struct sockaddr *)&calleraddr,
>&sin_size)==-1)
You're setting newsocket to the result of the == comparison, not to the
socket returned by accept(). You need another set of parentheses around
the assignment.
--
Barry Margolin, [email]barry.margolin@level3.com[/email]
Level(3), Woburn, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.
Barry Margolin Guest
-
Marc Rochkind #4
Re: newbie to network programming
On Thu, 31 Jul 2003 20:41:28 +0100, Dave Hughes <donotresus@yahoo.co.uk>
wrote:
[snip]
Taking a quick glance, I see this:
sizeof(struct sockaddr)
This is surely wrong... stuct sockaddr is really an abstrat type, with the
wrong size. You need the size of the actual address structure you're using -
- struct sockaddr_in.
(There may be other things wrong.)
Also, what is this line doing:
memset(&(myaddr.sin_zero),'\0',8);
--Marc
Marc Rochkind Guest
-
Dave Hughes #5
Re: newbie to network programming
Barry Margolin wrote:
Thanks alot, my code now works> In article <xneWa.129$Am1.97@newsfep1-gui.server.ntli.net>,
Dave
> Dave Hughes <donotresus@yahoo.co.uk> wrote:>>>Hey there,
>>
>>I'm having trouble with this very basic network program. All it does is
>>wait for a connection to my machine on port 4000. THen when it gets a
>>connection it sends the string Hello World to the caller. I am having
>>problems in that when the program gets to the send function I get the
>>error socket operation on nonsocket and I cant work out why this is since
>>newsocket is an integer, given a value by accept.
> ...
>>>> if (newsocket = accept(mysocket, (struct sockaddr *)&calleraddr,
>>&sin_size)==-1)
> Classic error.
>
> You're setting newsocket to the result of the == comparison, not to the
> socket returned by accept(). You need another set of parentheses around
> the assignment.
>Dave Hughes Guest
-
David Schwartz #6
Re: newbie to network programming
"Dave Hughes" <donotresus@yahoo.co.uk> wrote in message
news:jefWa.141$Am1.62@newsfep1-gui.server.ntli.net...
In addition to the other things mentioned, this 'close' uses an> int main(int argc, char *argv[])
> {
> int mysocket, newsocket;
> struct sockaddr_in myaddr;
> struct sockaddr_in calleraddr;
> int sin_size;
> close(mysocket);
initialized variable.
DS
David Schwartz Guest



Reply With Quote

