socket programming

Mar 12, 2009 at 12:54am
Greetings,
I am a beginner in socket programming and have been trying to set up a c server that accepts connection from a php page. The problem is that i am unable to connect to the server using the specified port. All i need is someone to look at the code and tell me what is wrong if possible.

/***** 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>


#define SERVER_PORT 10022
/*10022 is the port that must be used */
struct sockaddr_in self = {AF_INET, 0};

int main()
{ int soc, ns, k;
char buf[2048];
struct sockaddr_in client_addr = {AF_INET};
self.sin_port = htons(SERVER_PORT);
socklen_t addrlen = sizeof(client_addr);

int val;
/* set up listening socket soc */
soc = socket(AF_INET, SOCK_STREAM, 0);
if (soc < 0)
{ perror("server");
exit(1);
}
val = 1;

/*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);
}

/*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 *****/

/*******start of clientside.php*****/

<?php
error_reporting(E_ALL);

echo "<h2>TCP/IP Connection</h2>\n";


$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";
}






$message="hello internet";
$len = strlen($message);
#socket_write($socket, $message, $len);
echo "OK.\n";

echo "Reading response:\n\n";
#while ($out = socket_read($socket, 2048)) {
# echo $out;
#}
#$buf = socket_read($socket, 2048);
#echo $buf;

#echo "Closing socket...";
#socket_close($socket);
#echo "OK.\n\n";

?>

/******end of clientside.php******/

Thank you in advance.

Mar 12, 2009 at 11:19am
Regarding the C++ code:
1. socket() hasn't set the protocol
2. bind() hasn't set the port
3. accept() ought to hand off to a loop

You need a PHP forum for the rest.
Mar 12, 2009 at 5:56pm
Thank you for your reply.
How can i correct those?
Mar 12, 2009 at 6:20pm
Douglas Comer's example can be found here: http://rtes.cn.nctu.edu.tw/download/97EOS(F)/EOS_LAB6.pdf

and in his book, Internetworking TCP/IP vol3: http://www.amazon.com/Internetworking-TCP-Vol-III-Client-Server/dp/0130320714
Topic archived. No new replies allowed.