Page 1 of 1
client-server
Posted: Thu Mar 12, 2009 3:19 pm
by lemaire
Greetings,
i have a php client side that is trying to connect to a server written in c. When i try to connect, i receive the following warning message: Warning: socket_connect() [function.socket-connect]: unable to connect [111]: Connection refused.
Here is the code in question and i would really appreciate it if someone could tell me what is wrong with it:
<?php
error_reporting(E_ALL);
echo "<h2>TCP/IP Connection</h2>\n";
/* Get the port for the WWW service. */
$service = "10022";
/* Get the IP address for the target host. */
$address = gethostbyname('pdc-amd01.poly.edu');
/* Create a TCP/IP socket. */
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ($socket == false) {
echo "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "\n";
} else {
echo "OK.\n";
}
echo "Attempting to connect to '$address' on port '$service_port'...";
$result = socket_connect($socket, $address, $service);
if ($result == false) {
echo "socket_connect() failed.\nReason: ($result) " . socket_strerror(socket_last_error($socket)) . "\n";
} else {
echo "OK.\n";
}
?>
I am trying to connect to port 10022.
Re: client-server
Posted: Thu Mar 12, 2009 5:18 pm
by VladSun
There is no service listening on the remote host/port, so it's not your PHP code fault.
Re: client-server
Posted: Thu Mar 12, 2009 5:20 pm
by lemaire
well i wrote a c server that is supposed to be listening on that port. Can it be that the port(10022) is closed?
Re: client-server
Posted: Thu Mar 12, 2009 5:45 pm
by panic!
Yup it's probably closed. Why not try and connect onto a server and port you know are definitely working?
Re: client-server
Posted: Thu Mar 12, 2009 5:55 pm
by lemaire
Since the c server and php client are written in two different languages, could that be the problem?
Re: client-server
Posted: Thu Mar 12, 2009 6:01 pm
by VladSun
No. You can't even open a TCP connection for now. (I tried to telnet it but the port is closed)
Execute:
Code: Select all
netstat -ntap | grep LISTEN | grep 10022
(that's for Linux OS)
and see if the service is running and listening to this port. Also, be sure that it listens to publicly accessible IPs
Re: client-server
Posted: Thu Mar 12, 2009 6:07 pm
by lemaire
I know this is a php forum but if possible could you guys please take a look at the following c server that was supposed to be listening.
/***** inetserver.c *****/
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h> /* for getenv */
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h> /* Internet domain header */
#include <resolv.h>
#include <arpa/inet.h>
#include <errno.h>
#include <netdb.h>
#include <signal.h>
#define SERVER_PORT 10022
struct sockaddr_in self = {AF_INET, 0};
int main()
{ int soc, ns, k, portno = 0;
char buf[2048];
struct sockaddr_in client_addr = {AF_INET};
self.sin_port = htons(10022);
socklen_t addrlen = sizeof(client_addr);
int val;
/* set up listening socket soc */
soc = socket(AF_INET, SOCK_STREAM, SOL_TCP);
if (soc < 0)
{ perror("server");
exit(1);
}
val = 1;
bzero((char *) &self, sizeof(self));
self.sin_addr.s_addr = htonl(INADDR_ANY);
/*initialize ports and address*/
#if (setsockopt(soc, SOL_SOCKET, SO_REUSEADDR, &val, sizeof val) != 0)
#{ perror("server:setsockopt");
# exit(1);
#}
/*bind the socket(assign a port number */
if (bind(soc, (struct sockaddr *)&self, sizeof(self)) == -1)
{ perror("server:bind");
close(soc);
exit(1);
}
portno = ntohs(self.sin_port);
if(gethostname(hostname,50) == -1) // get hostname of the chat server machine
error("ERROR on gethostname");
host = gethostbyname(hostname); // get host information
strcpy(ipaddress,(char *)inet_ntoa(*(long*)host->h_addr_list[0])); //get IP Address string format
/*make it listening */
listen(soc, 30);
/* accept connection request */
ns = accept(soc, (struct sockaddr *)&client_addr, &addrlen);
if (ns < 0)
{ perror("server:accept");
close(soc);
exit(1);
}
/* data transfer on connected socket ns */
k = read(ns, buf, sizeof(buf) - 1);
buf[k] = '\0'; /* null-terminate string */
printf("SERVER RECEIVED: %s\n",buf);
write(ns, buf, k);
close(ns); close(soc);
return(0);
}
/***** end of inetserver.c *****/
If not could you recommend a c forum where the code can be looked at.
THANK YOU GUYS!!!
Re: client-server
Posted: Thu Mar 12, 2009 6:27 pm
by VladSun
It's full of errors. Too many of them...
Undeclared variables, incompatible types, etc.
I'm sure it's not running because it won't even compile
Take a look at these links:
http://www.google.com/search?client=ope ... 8&oe=utf-8
Re: client-server
Posted: Thu Mar 12, 2009 6:44 pm
by lemaire
still the same problem even with the provided correction.
Here is the link to the php client
http://pdc-amd01.poly.edu/~zcouli01/aform.php
Re: client-server
Posted: Thu Mar 12, 2009 6:48 pm
by VladSun
Did you succeed compiling the C server?
Did you run it in background?
e.g.
Re: client-server
Posted: Thu Mar 12, 2009 7:15 pm
by lemaire
just compiled and run it again, still the same issue.
I have frankly never seen something behaving so strangely.