在mysql shell上,我正在运行" SELECT socketCall();"返回5.
我正在尝试从mysql中的UDF函数连接到unix套接字服务器(本地运行)。 UDF函数中使用的connect()函数以某种方式失败,并立即从那里返回。
请帮我找出我在代码中做错了什么。我正在研究树莓派。
UDF功能代码如下:
#define SOCK_PATH "/root/mysock"
longlong socketCall(UDF_INIT *initid, UDF_ARGS *args, char *is_null, char *error)
{
int s,len,n;
struct sockaddr_un remote;
char str[100] = "Hello Saggu calling from MySql";
if ((s = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
return 2;
}
bzero((char *) &remote, sizeof(remote));
remote.sun_family = AF_UNIX;
strcpy(remote.sun_path, SOCK_PATH);
len = strlen(remote.sun_path) + sizeof(remote.sun_family);
if (connect(s, (struct sockaddr *)&remote, len) == -1) {
return 5;
}
n = send(s, str, strlen(str), 0);
close(s);
return n;
}
服务器代码如下:
#define SOCK_PATH "/root/mysock"
int main(void)
{
int s, s2, t, len;
struct sockaddr_un local, remote;
char str[100];
if ((s = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
perror("socket");
exit(1);
}
local.sun_family = AF_UNIX;
strcpy(local.sun_path, SOCK_PATH);
unlink(local.sun_path);
len = strlen(local.sun_path) + sizeof(local.sun_family);
if (bind(s, (struct sockaddr *)&local, len) == -1) {
perror("bind");
exit(1);
}
if (listen(s, 5) == -1) {
perror("listen");
exit(1);
}
for(;;) {
int done, n;
printf("Waiting for a connection...\n");
t = sizeof(remote);
if ((s2 = accept(s, (struct sockaddr *)&remote, &t)) == -1) {
perror("accept");
exit(1);
}
printf("Connected.\n");
n = recv(s2, str, 100, 0);
printf("\n %s\n",str);
close(s2);
}
return 0;
}