473,396 Members | 1,923 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 6158

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Didatus | last post by:
i have a problem to send data over an unix domain socket after the socket is created i am reading data from stdin und want to send it over socket to a listener but i get an error. He can't write...
0
by: Danny Jensen | last post by:
I need to test if certain processes on a unix box were running. I wanted to use whatsup gold to do the testing. First I needed to go to the whatsup configure>monitors & services menu to add this...
6
by: Dan | last post by:
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 =...
4
by: Alex Martinoff | last post by:
Using the org.postgresql.Driver JDBC driver is it possible to connect to Postgres using a unix domain socket instead of a TCP socket (so you don't have to start the postmaster with -i)? Using a TCP...
9
by: craig.overton | last post by:
All, I am currently developing an FTP class in VB.NET. It's kid tested, mother approved when trying to access an FTP Server on a Windows box meaning I can connect, run commands, upload and...
135
by: Xah Lee | last post by:
Tabs versus Spaces in Source Code Xah Lee, 2006-05-13 In coding a computer program, there's often the choices of tabs or spaces for code indentation. There is a large amount of confusion about...
0
by: Holly | last post by:
I copied this code that works to connect into Unix. I am looking for a way to get it to work with a secure Unix box. Anyone have any insights on how to do this? I am trying to build an sftp...
1
by: wsguglielmetti | last post by:
Hi, How are you? I'm completly new in python but I know a little of C. I developed a small application in python which generate and receive some data from times in times (question of...
23
by: =?GB2312?B?0rvK18qr?= | last post by:
Hi all, Recently I had a new coworker. There is some dispute between us. The last company he worked for has a special networking programming model. They split the business logic into...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.