470,841 Members | 1,065 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,841 developers. It's quick & easy.

TCP/IP versus UNIX socket (maybe)

Dan
I am working with the C-API to MYSQL Ver 12.16 Distrib 4.0.5a-beta,
for pc-linux (i686).

I have noticed the following problem. Within my C program I issue
---
conn = mysql_init(NULL);
conn = mysql_real_connect(conn,"localhost",...etc...);

if (conn == NULL)
{
printf("Connection unsuccessful...\n");
}
else
{
printf("Connection successful...\n");
}

---

conn is thankfully non-NULL and I can verify a successful connection
using
mysql_ping(conn) for example. However, if instead I do (using
loopback TCP/IP)

conn = mysql_init(NULL);
conn = mysql_real_connect(conn,"127.0.0.1",...etc...);

no connection is made (conn is NULL)! and of course the ping results
in a segmentation fault. :(

I have also tried this using the IP address of the machine.

Question 1: Why does "localhost" work but "127.0.0.1 (the loopback
interface) or IP address etc not work?? NULL also works so I assume it
has something to do with the differences between a UNIX socket and
TCP/IP connection. Is there a way to verify??

I should mention that I am using mysql on a server that I use ssh to
get to.
I ssh to the server compile C programs etc all on the same server
where the mysql is I just use ssh to get there. However, running
mysql from the prompt and from within Bourne shell scripts has worked
without any problems.

Question 2: When using the "localhost" designation (UNIX socket) I
can issue a mysql_init and mysql_real_connect as mentioned above in a
sub-function called from my main() program. This connection is
verified using mysql_ping within the subfunction, however the conn
(MYSQL connection). However, after the call to the subfunction, the
conn is lost. Why does the conn fall out of scope?? Is there
something about a UNIX socket that falls out of scope??

Similarly, if I issue mysql_init in main() but not in the subfunction
the mysql_real_connect call in the subfunction fails. Thus, it seems
mysql_init and mysql_real_connect must be issued together and that
conn somehow falls out of scope when switching between subfunction or
main program. Obviously this has drastic consequences in terms how I
can program so I'm hoping there is a way to have conn not fall out of
scope.

-------------Here is full code for reference -------------

#include <stdio.h>
#include <mysql.h>
void MYSQL_OPEN(MYSQL *conn)
{

int chk;
char *ERR_MSG;
printf("In subfunction MYSQL_OPEN");
printf("Opening connection to MYSQL server....\n");

conn = mysql_init(NULL);
conn = mysql_real_connect(conn,"localhost","blah","blah_b lah","blah_blah_blah",0,NULL,0);

if (conn == NULL)
{
printf("Connection unsuccessful...\n");
}
else
{
printf("Connection successful...\n");
}
if( mysql_ping(conn) == 0)
{
printf("Ping of server successful...\n");
}
else
{
printf("Ping of server unsuccessful...\n");
}

if (mysql_errno(conn) == 0)
{
printf("Connection made to MYSQL server.\n");
}
else
{
ERR_MSG=mysql_error(conn);
printf("%s\n",ERR_MSG);
}

chk = mysql_select_db(conn,"dpdoughe");

if (chk != 0)
{
printf("Unsuccessful database select\n");
}
else
{
printf("Successful database select\n");
}

chk=mysql_query(conn,"SHOW TABLES");

if (chk != 0)
{
printf("Unsuccessful query\n");
}
else
{
printf("Successful query\n");
}

}

void MYSQL_CLOSE(MYSQL *conn)
{
printf("Closing connection to MYSQL server...\n");
mysql_close(conn);
}
int main(int argc, char *argv[])
{

int chk=0;
MYSQL *conn;
char *ERR_MSG;
MYSQL_RES *STACK;
MYSQL_ROW ROW;
MYSQL_OPEN(conn);

chk = mysql_select_db(conn,"dpdoughe");

if (chk != 0)
{
printf("Unsuccessful database select\n");
}
conn = mysql_init(NULL);
chk=mysql_query(conn,"SHOW TABLES");
if (chk != 0)
{
printf("Unsuccessful query\n");
}

conn = mysql_init(NULL);
STACK=mysql_use_result(conn);

if (STACK==NULL)
{
conn = mysql_init(NULL);
ERR_MSG=mysql_error(conn);
printf("Query result is NULL...\n");
printf("%s\n",ERR_MSG);
}
else
{
printf("Sucessful query...\n");
}

/*conn = mysql_init(conn);
ROW=mysql_fetch_row(STACK);
*/
/*(if (chk!=0)
{
printf("Unsuccessful query.\n");
}
*/

MYSQL_CLOSE(conn);
return(0);
}
Jul 19 '05 #1
0 5964

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Didatus | last post: by
reply views Thread by Danny Jensen | last post: by
6 posts views Thread by Dan | last post: by
135 posts views Thread by Xah Lee | last post: by
reply views Thread by Holly | last post: by
1 post views Thread by wsguglielmetti | last post: by
23 posts views Thread by =?GB2312?B?0rvK18qr?= | last post: by
reply views Thread by mihailmihai484 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.