home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Usenet 1994 January
/
usenetsourcesnewsgroupsinfomagicjanuary1994.iso
/
sources
/
games
/
volume15
/
xtb
/
part02
/
dgram.c
< prev
next >
Wrap
C/C++ Source or Header
|
1993-01-27
|
1KB
|
99 lines
/*
* Comms.
*
* $Header: /morpork/home/bmh/xtest2/RCS/dgram.c,v 1.10 92/10/19 15:34:27 bmh Exp Locker: bmh $
*
* Bernard Hatt
* Camtec Electronics (Ericsson), Leicester, England, LE1 4SA
* bmh@terminus.ericsson.se
*
*/
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>
#include "defs.h"
#include "comms.h"
#define STREAM_TYPE SOCK_DGRAM
struct hostent *gethostbyname();
int
openread(port)
int port;
{
int sd;
struct sockaddr_in name;
sd=socket(AF_INET,STREAM_TYPE,0);
if(sd==-1)
{
perror("openread()/socket()");
return(-1);
}
name.sin_family=AF_INET;
name.sin_addr.s_addr=INADDR_ANY;
name.sin_port=htons(port);
if(bind(sd,&name,sizeof(name)))
{
perror("openread()/bind()");
return(-1);
}
return(sd);
}
int
readdata(sd,data)
int sd;
DATA *data;
{
return(read(sd,data,sizeof(DATA)));
}
int
opensend(host,port)
char *host;
int port;
{
int ret,sd;
struct sockaddr_in name;
struct hostent *hent;
sd=socket(AF_INET,STREAM_TYPE,0);
if(sd==-1)
{
perror("opensend()/socket()");
return(-1);
}
hent=gethostbyname(host);
if(hent==NULL)
return(-1);
bcopy(hent->h_addr,&name.sin_addr,hent->h_length);
name.sin_family=AF_INET;
name.sin_port=htons(port);
/* connect port */
ret=connect(sd,&name,sizeof(name));
if(ret==-1)
{
perror("opensend()/connect()");
return(-1);
}
return(sd);
}
int
senddata(sd,data)
int sd;
DATA *data;
{
return(write(sd,data,sizeof(DATA)));
}