9fans - fans of the OS Plan 9 from Bell Labs
 help / color / mirror / Atom feed
* Re: [9fans] please help(network programing)
@ 2000-12-20  3:46 Russ Cox
  0 siblings, 0 replies; 6+ messages in thread
From: Russ Cox @ 2000-12-20  3:46 UTC (permalink / raw)
  To: 9fans

Have a look at http://plan9.bell-labs.com/magic/man2html/2/dial
for more on writing the equivalent code in Plan 9.

Russ


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [9fans] please help(network programing)
  2000-12-18 12:07 ` Boyd Roberts
@ 2000-12-18 12:40   ` Wladimir Mutel
  0 siblings, 0 replies; 6+ messages in thread
From: Wladimir Mutel @ 2000-12-18 12:40 UTC (permalink / raw)
  To: 9fans

Boyd Roberts <boyd@planete.net> wrote:

> toss it and rewrite it.  plan 9 networking is a bunch easier,
> not that i'm convinved that it's 100% correct, but it sure
> beats the shit out of that horrible BSD crap.

	Is there any implementation of Plan9 networking API that can work
	as a layer over typical BSD-sockets API on typical UNIX system ?

-- 
mwg@orphanage.alkar.net - Владимир Мутель, сам за себя :>


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [9fans] please help(network programing)
@ 2000-12-18 12:30 rog
  0 siblings, 0 replies; 6+ messages in thread
From: rog @ 2000-12-18 12:30 UTC (permalink / raw)
  To: 9fans

[-- Attachment #1: Type: text/plain, Size: 1148 bytes --]

> This is a code example of the BeeJ's Guide.
>
> I wanna port it to plan9.

the following code should be just about equivalent.  note that it
compiled and ran perfectly first time, an unknown phenomenon
when writing unix sockets code...

BTW, try the manual pages (e.g.  dial(2));  you might find that
they're quite useful for understanding the system.

  cheers,
    rog.

#include <u.h>
#include <libc.h>

#define MYPORT 3490    /* the port users will be connecting to */

void
main(void)
{
	int listenctl, conctl, condata;
	char addr[20], dir[40], newdir[40];

	sprint(addr, "tcp!*!%d", MYPORT);
	listenctl = announce(addr, dir);
	if (listenctl == -1) {
		fprint(2, "cannot announce: %r\n");
		exits("announce");
	}
	for (;;) {
		conctl = listen(dir, newdir);
		if (conctl == -1) {
			fprint(2, "listen failed: %r\n");
			exits("listen");
		}
		condata = accept(conctl, newdir);
		if (rfork(RFFDG|RFPROC|RFNOWAIT) == 0) {
			if (fprint(condata, "Hello, world\n") == -1) {
				fprint(2, "write failed: %r\n");
				exits("write");
			}
			exits(nil);
		}
		close(condata);
		close(conctl);
	}
}


[-- Attachment #2: Type: message/rfc822, Size: 4355 bytes --]

To: cse.psu.edu!9fans
Subject: [9fans] please help(network programing)
Date: Mon, 18 Dec 2000 10:02:08 GMT
Message-ID: <91ga11$esr$1@news.netple.com>

hi~

I'm a newbie of Plan9.

This is a code example of the BeeJ's Guide.

I wanna port it to plan9.

However, Plan9 is so strange to me.

Anyone can help me?

    #include <stdio.h> 
    #include <stdlib.h> 
    #include <errno.h> 
    #include <string.h> 
    #include <sys/types.h> 
    #include <netinet/in.h> 
    #include <sys/socket.h> 
    #include <sys/wait.h> 

    #define MYPORT 3490    /* the port users will be connecting to */

    #define BACKLOG 10     /* how many pending connections queue will hold */

    main()
    {
        int sockfd, new_fd;  /* listen on sock_fd, new connection on new_fd */
        struct sockaddr_in my_addr;    /* my address information */
        struct sockaddr_in their_addr; /* connector's address information */
        int sin_size;

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

        my_addr.sin_family = AF_INET;         /* host byte order */
        my_addr.sin_port = htons(MYPORT);     /* short, network byte order */
        my_addr.sin_addr.s_addr = INADDR_ANY; /* auto-fill with my IP */
        bzero(&(my_addr.sin_zero), 8);        /* zero the rest of the struct */

        if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr)) \
                                                                      == -1) {
            perror("bind");
            exit(1);
        }

        if (listen(sockfd, BACKLOG) == -1) {
            perror("listen");
            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()) { /* this is the child process */
                if (send(new_fd, "Hello, world!\n", 14, 0) == -1)
                    perror("send");
                close(new_fd);
                exit(0);
            }
            close(new_fd);  /* parent doesn't need this */

            while(waitpid(-1,NULL,WNOHANG) > 0); /* clean up child processes */
        }
}    


-----------------===== Posted via NetPle Usenet Service =====-----------------
     http://news.NetPle.com     .

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [9fans] please help(network programing)
@ 2000-12-18 12:09 forsyth
  0 siblings, 0 replies; 6+ messages in thread
From: forsyth @ 2000-12-18 12:09 UTC (permalink / raw)
  To: 9fans

it's worth noting that if you change the start of the example
as simplified by rog for plan9 to something like the following:

void
main(int argc, char **argv)
{
	int listenctl, conctl, condata;
	char *addr, tcpaddr[20], dir[40], newdir[40];

	if(argc <= 1){
		sprint(tcpaddr, "tcp!*!%d", MYPORT);
		addr = tcpaddr;
	}else
		addr = argv[1];

you can use the same program to listen on a non-IP network.
it is rare to do the same with sockets because so many IP-specific
assumptions end up being written in to the code.
you not have got a non-IP network but you could use
a protocol other than TCP:
connect to
	8.out 'il!*!9999'
with
	telnet 'il!*!9999'



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [9fans] please help(network programing)
  2000-12-18 10:02 bawei
@ 2000-12-18 12:07 ` Boyd Roberts
  2000-12-18 12:40   ` Wladimir Mutel
  0 siblings, 1 reply; 6+ messages in thread
From: Boyd Roberts @ 2000-12-18 12:07 UTC (permalink / raw)
  To: 9fans

toss it and rewrite it.  plan 9 networking is a bunch easier,
not that i'm convinved that it's 100% correct, but it sure
beats the shit out of that horrible BSD crap.

use the code you have as a base.  i used the structure of
a unix pop 3 client to write it in limbo, but it was just
so easy to do.

----- Original Message -----
From: <bawei@yahoo.com>
To: <9fans@cse.psu.edu>
Sent: Monday, December 18, 2000 9:02 PM
Subject: [9fans] please help(network programing)


> hi~
>
> I'm a newbie of Plan9.
>
> This is a code example of the BeeJ's Guide.
>
> I wanna port it to plan9.
>
> However, Plan9 is so strange to me.
>
> Anyone can help me?
>
>     #include <stdio.h>
>     #include <stdlib.h>
>     #include <errno.h>
>     #include <string.h>
>     #include <sys/types.h>
>     #include <netinet/in.h>
>     #include <sys/socket.h>
>     #include <sys/wait.h>
>
>     #define MYPORT 3490    /* the port users will be connecting to */
>
>     #define BACKLOG 10     /* how many pending connections queue will hold */
>
>     main()
>     {
>         int sockfd, new_fd;  /* listen on sock_fd, new connection on new_fd */
>         struct sockaddr_in my_addr;    /* my address information */
>         struct sockaddr_in their_addr; /* connector's address information */
>         int sin_size;
>
>         if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
>             perror("socket");
>             exit(1);
>         }
>
>         my_addr.sin_family = AF_INET;         /* host byte order */
>         my_addr.sin_port = htons(MYPORT);     /* short, network byte order */
>         my_addr.sin_addr.s_addr = INADDR_ANY; /* auto-fill with my IP */
>         bzero(&(my_addr.sin_zero), 8);        /* zero the rest of the struct
*/
>
>         if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr))
\
>                                                                       == -1) {
>             perror("bind");
>             exit(1);
>         }
>
>         if (listen(sockfd, BACKLOG) == -1) {
>             perror("listen");
>             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()) { /* this is the child process */
>                 if (send(new_fd, "Hello, world!\n", 14, 0) == -1)
>                     perror("send");
>                 close(new_fd);
>                 exit(0);
>             }
>             close(new_fd);  /* parent doesn't need this */
>
>             while(waitpid(-1,NULL,WNOHANG) > 0); /* clean up child processes
*/
>         }
> }
>
>
> -----------------===== Posted via NetPle Usenet Service =====-----------------
>      http://news.NetPle.com     .
>



^ permalink raw reply	[flat|nested] 6+ messages in thread

* [9fans] please help(network programing)
@ 2000-12-18 10:02 bawei
  2000-12-18 12:07 ` Boyd Roberts
  0 siblings, 1 reply; 6+ messages in thread
From: bawei @ 2000-12-18 10:02 UTC (permalink / raw)
  To: 9fans

hi~

I'm a newbie of Plan9.

This is a code example of the BeeJ's Guide.

I wanna port it to plan9.

However, Plan9 is so strange to me.

Anyone can help me?

    #include <stdio.h> 
    #include <stdlib.h> 
    #include <errno.h> 
    #include <string.h> 
    #include <sys/types.h> 
    #include <netinet/in.h> 
    #include <sys/socket.h> 
    #include <sys/wait.h> 

    #define MYPORT 3490    /* the port users will be connecting to */

    #define BACKLOG 10     /* how many pending connections queue will hold */

    main()
    {
        int sockfd, new_fd;  /* listen on sock_fd, new connection on new_fd */
        struct sockaddr_in my_addr;    /* my address information */
        struct sockaddr_in their_addr; /* connector's address information */
        int sin_size;

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

        my_addr.sin_family = AF_INET;         /* host byte order */
        my_addr.sin_port = htons(MYPORT);     /* short, network byte order */
        my_addr.sin_addr.s_addr = INADDR_ANY; /* auto-fill with my IP */
        bzero(&(my_addr.sin_zero), 8);        /* zero the rest of the struct */

        if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr)) \
                                                                      == -1) {
            perror("bind");
            exit(1);
        }

        if (listen(sockfd, BACKLOG) == -1) {
            perror("listen");
            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()) { /* this is the child process */
                if (send(new_fd, "Hello, world!\n", 14, 0) == -1)
                    perror("send");
                close(new_fd);
                exit(0);
            }
            close(new_fd);  /* parent doesn't need this */

            while(waitpid(-1,NULL,WNOHANG) > 0); /* clean up child processes */
        }
}    


-----------------===== Posted via NetPle Usenet Service =====-----------------
     http://news.NetPle.com     .


^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2000-12-20  3:46 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2000-12-20  3:46 [9fans] please help(network programing) Russ Cox
  -- strict thread matches above, loose matches on Subject: below --
2000-12-18 12:30 rog
2000-12-18 12:09 forsyth
2000-12-18 10:02 bawei
2000-12-18 12:07 ` Boyd Roberts
2000-12-18 12:40   ` Wladimir Mutel

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).