This FAQ describes the Creation of a Client socket. The Functions had been tested with C-Builder and Windows NT, but it should work with WIN 9X too. The presence of winsock.dll is required.
With some small modifications it works in a UNIX environment too.
The required routines are encapsulated in the winsock-Dll
int tcp_open(char *host,char *service,int port,char *emsg,unsigned int dllhandle)
/***********************************************************
host : Target-Hostname or IP-Address
service : Portname required if Port = 0
port : Port number required if service=empty
emsg : Errormessages char[60] should be enough
dllhandle: Handle to winsock DLL (result from netinit)
***********************************************************/
{
unsigned long int inaddr;
servent *sp;
int is;
hostent *hp;
u_long iadr;
sockaddr_in tcp_srv_addr;
int fd;
unsigned char hostname[20],buffer[4];
struct servent FAR * PASCAL (FAR *gsbyname)(const char FAR* ,const char FAR *);
u_short PASCAL (FAR *htns) (u_short FAR);
u_long PASCAL (FAR *htnl) (u_long FAR);
struct hostent FAR * PASCAL (FAR *ghbyname)(const char FAR * name);
SOCKET PASCAL (FAR *sckt) (int FAR, int FAR, int FAR);
int PASCAL (FAR *conct) (SOCKET FAR, const struct sockaddr FAR *, int namelen);
_fmemset(&tcp_srv_addr,0,sizeof(tcp_srv_addr)) ;
tcp_srv_addr.sin_family=AF_INET;
/*============================================================
Get port number from service Table
============================================================*/
// if there is a Service Table to be used
if(*service != 0)
{
// is there a Service Entry
if((sp = (*gsbyname)(service,"tcp")) == NULL)
{
sprintf(emsg,"tcp_open: unknown service: %s /tcp\n",service);
return UNKNOWN_SERVICE;
}
// everything ok. Now you can use fd as an regular
// file descriptor(netread/netwrite(fd,&value,sizeof(value)).....
return fd;
}
============================================================
Probably The functions netread and netwrite have to be modified dependent on appliction
These are Examples for ASCII transmission with <CR> as Terminator.
int netread(int sock,void *val,const int le,int netdll)
/**********************************************************
sock : Socket created by tcp_open or accept funtion in case of a Server Socket
val : readbuffer
le : length of readbuffer
netdll : Handle to winsock Dll
Netread returns the number of bytes read
***********************************************************/
{
int nrbytes,cnt,retcode;
int PASCAL (FAR *rcv) (SOCKET FAR, char FAR *, int FAR, int FAR);
char *p,*p1;
//===================================================================
// Initialize dll function
nrbytes=0;
cnt=NRCYCLES;
// p = pointer to receive buffer
p=(char *)val;
do
{
retcode=(*rcv)(sock,p,le,0);
if(retcode==SOCKET_ERROR)
{
return -1;
}
else
{
p1=p;
p +=retcode;
nrbytes +=retcode;
// did a carriage return arrive? yes then terminate
if(*p1==13)
*p1=0;
}
}
// Repeat until buffer full or there is no transfer
// active for NCYCLE rounds or transfer terminated
while((nrbytes<le)&&(cnt-->0)&&(*p1!=0));
return nrbytes;
}
===========================================================
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.