Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Linux BCB Sockets

Status
Not open for further replies.

cyprus106

Programmer
Apr 30, 2001
654
Alright, the Linux board is ABSOLUTELY no help at all. Now the way I see it: with the release of Kylix 3 including C++, it's pretty BCBish so asking a BCB_LINUX question here's alright... :-D

Included with all versions of Kylix are the components (under the 'internet' tab) TcpClient, TcpServer and UdpSocket... Now I can connect to a regular TServerSocket (on a different Windows machine) with TcpClient, and I can send data just fine to the ServerSocket, but when the ServerSocket sends data back to the TcpClient, it won't get it. It just refuses to launch the OnReceive() function. I'm now officially fed up. Does anybody have any idea if I could transport the windows sockets to linux? or how I could get a socket component for lunux? Even better, maybe somebodyy could tell me how to receive that stupid data.

Thanks! Cyprus
 
The FAQ101-993 describes a client socket which was originally written for Unix.
If you remove the Windows specific part (DLL-Calls) it should run with Linux too.

hnd
hasso55@yahoo.com

 
All the function which are initialized in the Header of the routines.

hnd
hasso55@yahoo.com

 
Alright, I'm getting errors off the wall here. I've tried for 2 days to modify the code and I'm still getting as many errors as I started with. Can you give me exactly what I'm supposed to remove here?

Thanks lots Cyprus
 
Ok. But you will have to Wait until tommorow in the evening

hnd
hasso55@yahoo.com

 
Sorry Cyprus,
It took some time to get access to a Unix Machine but this code should work:



/*******************************************************************

Modul Name: tcp_open.c

Titel: Open a Socket as a Client


*********************************************************************/
/*============================================================
Oeffnen eines Netzwerkknotens
==============================================================*/
/*------- Define OS -------*/

#define UNIX 1 // 0 = Windows

/*---------------------------------------------------------------------------*/
#if UNIX
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <errno.h>
#include <stdio.h>

#include <sys/io.h>



//struct servent tcp_serv_info;

#else

#include <vcl.h>
#pragma hdrstop
#include <winsock2.h>
#include <mem.h>
#include <errno.h>
#include <stdio.h>
#include <io.h>
#endif

/****************** Define Error Codes *******************/

#define DLL_ERROR -1
#define UNKNOWN_SERVICE -2
#define SERVICE_MISSING -3
#define UNKNOWN_HOST -4
#define CONNECT_ERROR -5
#define SOCKET_CREATE_ERROR -6
/**********************************************************/

#ifndef INADDR_NONE
#define INADDR_NONE 0xffffffff
#endif

#if UNIX

int
tcp_open( host, service, port, emsg ) // there is no need to handle DLL functions

char *host ;
char *service ;
int port;
char *emsg;

#else
int tcp_open(char *host,char *service,int port,char *emsg,unsigned int dllhandle)
#endif

/***********************************************************
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;
struct servent *sp;
int is;
struct hostent *hp;
unsigned long iadr;
struct sockaddr_in tcp_srv_addr;
int fd;
unsigned char hostname[20],buffer[4];

#if !UNIX
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);


/*============================================================

Init phase

===========================================================*/

// Define DLL Procedures
(FARPROC)gsbyname = GetProcAddress((HINSTANCE)dllhandle,&quot;getservbyname&quot;);
if(gsbyname==NULL)
return DLL_ERROR;
(FARPROC)htns = GetProcAddress((HINSTANCE)dllhandle,&quot;htons&quot;);
if(htns==NULL)
return DLL_ERROR;
(FARPROC)htnl = GetProcAddress((HINSTANCE)dllhandle,&quot;htonl&quot;);
if(htnl==NULL)
return DLL_ERROR;
(FARPROC)ghbyname = GetProcAddress((HINSTANCE)dllhandle,&quot;gethostbyname&quot;);
if(ghbyname==NULL)
return DLL_ERROR;
(FARPROC)sckt = GetProcAddress((HINSTANCE)dllhandle,&quot;socket&quot;);
if(sckt==NULL)
return DLL_ERROR;
(FARPROC)conct = GetProcAddress((HINSTANCE)dllhandle,&quot;connect&quot;);
if(conct==NULL)
return DLL_ERROR;
#endif
// Clear tcp Buffer

#if UNIX
memset(&tcp_srv_addr,0,sizeof(tcp_srv_addr)) ;
#else
_fmemset(&tcp_srv_addr,0,sizeof(tcp_srv_addr)) ;
#endif
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 UNIX
if((sp = getservbyname(service,&quot;tcp&quot;)) == NULL)
#else
if((sp = (*gsbyname)(service,&quot;tcp&quot;)) == NULL)
#endif
{
sprintf(emsg,&quot;tcp_open: unknown service: %s /tcp\n&quot;,*service);
return UNKNOWN_SERVICE;
}

// Setup Structure for socket

if(port>0)
{
#if UNIX
tcp_srv_addr.sin_port=htons(port);
#else
tcp_srv_addr.sin_port=(*htns)(port);
#endif
}
else
{
tcp_srv_addr.sin_port=sp->s_port;
}
}
else

// If there is a Service number

{
if(port<=0)
{
sprintf(emsg,&quot;tcp_open: must specify either service or port\n&quot;);
return SERVICE_MISSING;
}
#if UNIX
tcp_srv_addr.sin_port=htons(port);
#else
tcp_srv_addr.sin_port=(*htns)(port);
#endif
}
// Check Host address
if((inaddr = inet_addr(host)) != INADDR_NONE)
{
memmove((char *)&tcp_srv_addr.sin_addr,(char *) &inaddr,sizeof(inaddr));
}
else

// if host name used get host data from Tables

{
#if UNIX
if((hp=gethostbyname(host)) == NULL)
#else
if((hp=(*ghbyname)(host)) == NULL)
#endif
{
sprintf(emsg,&quot;tcp_open: Host Name Error: %s\n&quot;,*host);
return UNKNOWN_HOST;
}
buffer[0]=*hp->h_addr++ ;
buffer[1]=*hp->h_addr++ ;
buffer[2]=*hp->h_addr++ ;
buffer[3]=*hp->h_addr++ ;
sprintf(hostname,&quot;%d.%d.%d.%d\0&quot;,buffer[0],buffer[1],buffer[2],buffer[3]);
inaddr=inet_addr(hostname);

// Copy data into structure
#if UNIX
iadr=htonl((u_long)*hp->h_addr);
#else
iadr=(*htnl)((u_long)*hp->h_addr);
#endif
memmove(&tcp_srv_addr.sin_addr,&inaddr,sizeof(inaddr));
}

// Create Networksocket(AF_INET)

if (port>=0)
{
#if UNIX
if((fd = socket(AF_INET,SOCK_STREAM,0))==SOCKET_CREATE_ERROR)
#else
if((fd=(*sckt)(AF_INET,SOCK_STREAM,0))==SOCKET_CREATE_ERROR)
#endif
{
is=(int)errno;
sprintf(emsg,&quot;tcp_open: can't create TCP socket fd=%d\n&quot;,fd);
return SOCKET_CREATE_ERROR;
}
}

// connect socket to Host

#if UNIX
if((connect(fd,(struct sockaddr *) &tcp_srv_addr,sizeof(tcp_srv_addr)))<0)
#else
if(((*conct)(fd,(struct sockaddr *) &tcp_srv_addr,sizeof(tcp_srv_addr)))<0)
#endif
{
sprintf(emsg,&quot;tcp_open: can't connect to server\n&quot;);
close(fd);
return CONNECT_ERROR;
}

// everything ok. Now you can use fd as an regular File descriptor.
return fd;
}
hnd
hasso55@yahoo.com

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top