Network

I read a lot of thing concerning netwrok support in the SDL mailing list. Some
people seems have some Network library.
But I would like to know if someone has a crossplateform library that just
implement basic TCP/IP network support in C
( howto send data, to receive data, to create connetcion and to close
connection)

Thanks,

Best regards,–
Stephane Magnenat
Stephane.magnenat at urbanet.ch

BSD sockets is AFAIK supported on all SDL supported platforms. (Well, if you
count winsock as BSD sockets…)

Although, with a network API, one platform that doesn’t support it could make
the API messy… Anybody know how close Apple’s API is to BSD sockets?On Tue, Oct 13, 1998 at 05:57:49PM +0200, Stephane Magnenat wrote:

I read a lot of thing concerning netwrok support in the SDL mailing list. Some
people seems have some Network library.
But I would like to know if someone has a crossplateform library that just
implement basic TCP/IP network support in C
( howto send data, to receive data, to create connetcion and to close
connection)


– Michael Samuel

Although, with a network API, one platform that doesn’t support it could make
the API messy… Anybody know how close Apple’s API is to BSD sockets?

Apple has two TCP/IP APIs. OpenTransport is based on XTI/STREAMS, but was
heavily modified to make the async stuff robust on the cooperative MacOS.
MacTCP is an older network stack, but OT provides a MacTCP compatibility
service.

There is no native BSD Sockets API, but GUSI is a free library that provides
BSD wrappers on MacTCP (but it’s rather slow).

GUSI: ftp://sunsite.cnlab-switch.ch/software/platform/macos/src/mw_c/

My question is… how thin do we want to make the networking layer on top
of BSD Sockets? What benefits will it have over just using sockets directly?

Just curious,

Matt

/* Matt Slot, Bitwise Operator * One box, two box, yellow box, blue box. *

Although, with a network API, one platform that doesn’t support it could make
the API messy… Anybody know how close Apple’s API is to BSD sockets?

Apple has two TCP/IP APIs. OpenTransport is based on XTI/STREAMS, but was
heavily modified to make the async stuff robust on the cooperative MacOS.
MacTCP is an older network stack, but OT provides a MacTCP compatibility
service.

There is no native BSD Sockets API, but GUSI is a free library that provides
BSD wrappers on MacTCP (but it’s rather slow).

GUSI: ftp://sunsite.cnlab-switch.ch/software/platform/macos/src/mw_c/

My question is… how thin do we want to make the networking layer on top
of BSD Sockets? What benefits will it have over just using sockets directly?

I say get the BSD socket stuff going first, so that something works,
then we can come back to it later when there’s more time, after more
important things (like MIDI and maybe 3D) have been implemented.On Wed, 14 Oct 1998, Matt Slot wrote:

Just curious,

Matt

/* Matt Slot, Bitwise Operator * One box, two box, yellow box, blue box. *


Scott M. Stone <sstone at pht.com, sstone at turbolinux.com>

Head of TurboLinux Development/Systems Administrator
Pacific HiTech, Inc (USA) / Pacific HiTech, KK (Japan)
http://www.pht.com http://armadillo.pht.co.jp
http://www.pht.co.jp http://www.turbolinux.com

Le Wed, 14 Oct 1998, vous avez ?crit :

I read a lot of thing concerning netwrok support in the SDL mailing list. Some
people seems have some Network library.
But I would like to know if someone has a crossplateform library that just
implement basic TCP/IP network support in C
( howto send data, to receive data, to create connetcion and to close
connection)

BSD sockets is AFAIK supported on all SDL supported platforms. (Well, if you
count winsock as BSD sockets…)

So, winsock is completly compatible with BSD sockets ?>On Tue, Oct 13, 1998 at 05:57:49PM +0200, Stephane Magnenat wrote:

Although, with a network API, one platform that doesn’t support it could make
the API messy… Anybody know how close Apple’s API is to BSD sockets?


– Michael Samuel

Stephane Magnenat
Stephane.magnenat at urbanet.ch

Well, depending on the Mac API (and if anybody can port it to the Mac), it will
probably be just macros around the sockets API, and maybe a couple of functions
that do tasks for you, like this:

int connect_to_host(char *hostname, int port, int protocol, int socktype);

Which will do most of the ‘dirty work’ for you. (although, the return type will
depend on whether all systems can handle file descriptors…)On Wed, Oct 14, 1998 at 05:11:36AM -0400, Matt Slot wrote:

My question is… how thin do we want to make the networking layer on top
of BSD Sockets? What benefits will it have over just using sockets directly?


– Michael Samuel

I’ve never actually played with Winsock before, but apparently the only
difference is that on winsock you need to initialize it.On Wed, Oct 14, 1998 at 01:07:27PM +0200, Stephane Magnenat wrote:

So, winsock is completly compatible with BSD sockets ?


– Michael Samuel

Le Wed, 14 Oct 1998, vous avez ?crit :>On Wed, 14 Oct 1998, Matt Slot wrote:

Although, with a network API, one platform that doesn’t support it could make
the API messy… Anybody know how close Apple’s API is to BSD sockets?

Apple has two TCP/IP APIs. OpenTransport is based on XTI/STREAMS, but was
heavily modified to make the async stuff robust on the cooperative MacOS.
MacTCP is an older network stack, but OT provides a MacTCP compatibility
service.

There is no native BSD Sockets API, but GUSI is a free library that provides
BSD wrappers on MacTCP (but it’s rather slow).

GUSI: ftp://sunsite.cnlab-switch.ch/software/platform/macos/src/mw_c/

My question is… how thin do we want to make the networking layer on top
of BSD Sockets? What benefits will it have over just using sockets directly?

I say get the BSD socket stuff going first, so that something works,
then we can come back to it later when there’s more time, after more
important things (like MIDI and maybe 3D) have been implemented.

I agree, so does someone have a quite good implementation of BSD socket so I
can include someting like that in my program :

#include <BSD_Socket.h>

And have something like that :

opensocket
sendsocket
getsocket
closesocket

???

Does BSD socket support bufer in input on a socket ?

Stephane Magnenat
Stephane.magnenat at urbanet.ch

I have a dumb little library I started for a game I’m working on.
Here it is. Not much to it yet, but it might be a place to start.

I’ve compiled it on the following platforms with the compile options:
IRIX62: gcc -o network -DTEST network.c
Linux2.0.34: gcc -o network -DTEST network.c
SunOS5.5: gcc -o network -DTEST network.c -lsocket -lnsl
Haven’t had a chance to try to compile it on other platforms.

Kent Mein
------------------------network.c-----------------------------------
/* #include <stream.h> */
#include <stdio.h>

#include <string.h>
#include <strings.h>
#include <ctype.h>
#include <netdb.h>
#include <unistd.h>
#include <sys/time.h>

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <signal.h>

#include <arpa/inet.h>

#define SERVER 1
#define NETSERVER 2
#define CLIENT 3
#define MAXBUFFSIZE 1024
#define NUM_WAITING_CLIENTS 5
#define MAXUSERS 10

struct sockaddr_in node;
struct hostent *host;
char mybuff[MAXBUFFSIZE];
int done=0;
int port=9999;
char server[200]=“localhost”;

int init_networktcp();
int server_modetcp(int SD);
int client_modetcp(int SD);
int server_modesingle();
int myrecvtcp(int SD, char *mybuff);
int mysendtcp(int SD, char *mybuff);
void handle_userstcp(int *USERS,int size);
struct sockaddr_in get_servertcp(int SD);
void bcastpkttcp(char *pkt, int SD,int *USERS,int num_users);

int init_networktcp() {
int SD,i;

host = (struct hostent *)gethostbyname(server);
if (host ==(struct hostent *)NULL) {
printf(“Error lookingup hostname.\n”);
return -1;
}

bzero(&node, sizeof(node));
node.sin_family =host->h_addrtype;
if (port < 1) node.sin_port = htons(9119);
else node.sin_port = htons(port);
node.sin_addr.s_addr = htonl(INADDR_ANY);

printf(“Creating a socket\n”);
if ((SD=socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) {
printf(“Error in creating a socket.\n”);
return -1;
}
i=1;
if (setsockopt(SD,SOL_SOCKET,SO_REUSEADDR,&i,sizeof(i)) < 0) {
printf(“Error in modifying socket.\n”);
return -1;
}

printf(“Binding to a socket\n”);
if (bind(SD, (struct sockaddr *)&node, sizeof(node)) < 0) {
printf(“Error in binding to a socket.\n”);
close(SD);
return -1;
}
return SD;
}

int mysendtcp(int SD, char *mybuff) {
return send(SD,mybuff,sizeof(char) * MAXBUFFSIZE,0);
}

int myrecvtcp(int SD, char *mybuff) {
return recv(SD,mybuff,sizeof(char) * MAXBUFFSIZE,0);
}

/* send the pkt to everybody except on incoming socket */
void bcastpkttcp(char *pkt, int SD,int *USERS,int num_users) {
int i;

for (i = 0; i < num_users; i++) {
if (SD != USERS[i]) mysendtcp( USERS[i], pkt );
}
}

/* Searches a given subnet for a server and connects to it.
*/
struct sockaddr_in get_servertcp(int SD) {
char *message;
int myaddr[4];

mysendtcp(SD, “ATTACH”);
/* figure out hub and return it I think??? */
}

int client_modetcp(int SD) {
struct sockaddr_in hub;

    hub=get_servertcp(SD);

     if (connect(SD,(struct sockaddr *)&hub,sizeof(hub)) < 0) {
         printf("Error connecting to server.\n");
         return 0;
     }

while (!done) {
}
return 1;
}

void handle_userstcp(int *USERS,int size) {
int x;
struct timeval tv;
fd_set readset;
char mybuff[MAXBUFFSIZE];

tv.tv_sec=3;
tv.tv_usec =0;

for (x=0;x<size;x++) {
FD_SET(USERS[x],&readset);
if (FD_ISSET(USERS[x],&readset)) {
myrecvtcp(USERS[x],mybuff);
printf(“Message comming in: %s\n”, mybuff);
}
}
}

int server_modetcp(int SD) {
int c,
newclient,
num_users=0,
USERS[MAXUSERS];
char mybuff[MAXBUFFSIZE];
fd_set readset;

printf(“Listening to socket\n”);
if (listen(SD,NUM_WAITING_CLIENTS) < 0 ) {
printf(“Maximum number of clients reached.\n”);
close(SD);
return -1;
}
FD_ZERO(&readset);
FD_SET(STDIN_FILENO, &readset);
FD_SET(SD, &readset);
while (!done) {
/* loop through connected users */
handle_userstcp(USERS,num_users);

  /* look for new connections */
  while (FD_ISSET(SD, &readset)) {
     if ((newclient = accept(SD,NULL,NULL)) < 0) {
        printf("Problem connecting\n");
     } else {
        printf("Got a connection\n");
        myrecvtcp(newclient,mybuff);
        printf("buff = %s\n", mybuff);
        if (strncmp(mybuff,"LOOKING",7) == 0) {
           /* add this guy to server */
           if (num_users < MAXUSERS) {
              USERS[num_users]=newclient;
              num_users++;
           } else {
              close(newclient);
           }
        } else if (strncmp(mybuff,"QUIT",4) == 0) {
          close(newclient);
        }
     }
  }

}
printf(“Exiting server loop\n”);
return 1;
}

int server_modesingle() {
while (!done) {

}
return 1;
}

#ifdef TEST

int main() {
int SD= init_networktcp();

if (SD < 0) {
exit(1);
}
if (SERVER) {
printf(“Entering Server_Mode\n”);
server_modetcp(SD);
} else {
printf(“Entering Client_Mode\n”);
client_modetcp(SD);
}
return 1;
}
#endif

Le Wed, 14 Oct 1998, vous avez ?crit :

I have a dumb little library I started for a game I’m working on.
Here it is. Not much to it yet, but it might be a place to start.

I’ve compiled it on the following platforms with the compile options:
IRIX62: gcc -o network -DTEST network.c
Linux2.0.34: gcc -o network -DTEST network.c
SunOS5.5: gcc -o network -DTEST network.c -lsocket -lnsl
Haven’t had a chance to try to compile it on other platforms.

Hello,

This seems very coo. Some questions :

  • Can you comment it a bit (just the procedure, that will be very cool :slight_smile: ) ?
  • Does it have a chance to work under win32 ? I tried to crosscompile it under
    Linux but a lot of .h are not found.
  • When I get data from buffer, how can I know how the buffer width is, because
    I get a pointer on a memory location, and I don’t know where it stops.
  • I don’t understand which which mean SD, it will be very very cool if you just
    could document this, thanks.

Good work, :slight_smile:
best regards,

Stephane Magnenat
Stephane.magnenat at urbanet.ch

Michael Samuel wrote:

So, winsock is completly compatible with BSD sockets ?

I’ve never actually played with Winsock before, but apparently the only
difference is that on winsock you need to initialize it.

And some things are different because the “socket handle” are not
regular file handles (there’s a good part of the normal C library in
there, like select(), that is replicated and will only work with socket
handles). Since you cannot fprintf() or fcntl() to a socket, things like
non-blocking I/O is different also.–
Pierre Phaneuf
Web: http://seventh.ml.org/~pp/
finger: pp at seventh.ml.org

In a time long past, Michael Samuel forced monkeys to write:

BSD sockets is AFAIK supported on all SDL supported platforms. (Well, if you
count winsock as BSD sockets…)

Although, with a network API, one platform that doesn’t support it could make
the API messy… Anybody know how close Apple’s API is to BSD sockets?
They don’t use sockets at all.

It’s a STREAMS based networking system (open transport) – but with MacOS X they are adding sockets. Now, I don’t know when this is being released, but my guess is that it’ll be soon enough so that I wouldn’t bother completely rewriting the SDL networking code just because of macos.–
Josh Huber | @Josh_Huber | http://www.wpi.edu/~huberj/

#include <BSD_Socket.h>

Well, more like this:

#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <errno.h>

/* See socket(2) for a list of types (SOCK_DGRAM, SOCK_STREAM, …)

  • See protocols(5) for a list of protocols
  • Quick and dirty TCP connection: (int this case, POP3)
  • fd = connect_to_host(“mail”, 110, SOCK_STREAM, 0);
    */

int connect_to_host(char *hostname, int port, int type, int protocol)
{
int sockfd;
struct hostent *host;
struct sockaddr_in server;

server.sin_family = AF_INET;

sockfd = socket(AF_INET, type, protocol);
if(sockfd < 0)
{
	fprintf(stderr, "Cannot create a socket: %d\n", errno);
	return(-1);
}
host = gethostbyname(hostname);
if(host == (struct hostent *)NULL)
{
	fprintf(stderr, "Cannot get hostname: %d\n", errno);
	return(-1);
}
memcpy(&server.sin_addr, host->h_addr, host->h_length);

/* Convert the port number into network byte order before putting it
 * into the sockaddr_in structure */
server.sin_port = htons(port);

/* We just return the value of connect, because the return value
 * of this function is the same as the return values to connect */
return(connect(fd, (struct sockaddr *)&server, sizeof(server)));

}

Note that this code was typed directly into my MUA, and probably won’t work. I
hope it served as a quick+dirty tutorial to the BSD sockets API.

Man pages to look at:

(Ignore the numbers if you don’t have a debian system)
socket(2), protocols(5), gethostbyname(3), connect(2), listen(2), bind(2),
accept(2), many others…

Does BSD socket support bufer in input on a socket ?

On Linux it does…On Wed, Oct 14, 1998 at 01:08:32PM +0200, Stephane Magnenat wrote:


– Michael Samuel

Le Thu, 15 Oct 1998, vous avez ?crit :>On Wed, Oct 14, 1998 at 01:08:32PM +0200, Stephane Magnenat wrote:

#include <BSD_Socket.h>

Well, more like this:

#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <errno.h>

/* See socket(2) for a list of types (SOCK_DGRAM, SOCK_STREAM, …)

  • See protocols(5) for a list of protocols
  • Quick and dirty TCP connection: (int this case, POP3)
  • fd = connect_to_host(“mail”, 110, SOCK_STREAM, 0);
    */

int connect_to_host(char *hostname, int port, int type, int protocol)
{
int sockfd;
struct hostent *host;
struct sockaddr_in server;

server.sin_family = AF_INET;

sockfd = socket(AF_INET, type, protocol);
if(sockfd < 0)
{
fprintf(stderr, “Cannot create a socket: %d\n”, errno);
return(-1);
}
host = gethostbyname(hostname);
if(host == (struct hostent *)NULL)
{
fprintf(stderr, “Cannot get hostname: %d\n”, errno);
return(-1);
}
memcpy(&server.sin_addr, host->h_addr, host->h_length);

/* Convert the port number into network byte order before putting it

  • into the sockaddr_in structure */
    server.sin_port = htons(port);

/* We just return the value of connect, because the return value

  • of this function is the same as the return values to connect */
    return(connect(fd, (struct sockaddr *)&server, sizeof(server)));
    }

Note that this code was typed directly into my MUA, and probably won’t work. I
hope it served as a quick+dirty tutorial to the BSD sockets API.

Man pages to look at:

(Ignore the numbers if you don’t have a debian system)
socket(2), protocols(5), gethostbyname(3), connect(2), listen(2), bind(2),
accept(2), many others…

Does BSD socket support bufer in input on a socket ?

On Linux it does…

Your system is not multiplateform ???

Stephane Magnenat
Stephane.magnenat at urbanet.ch