Jump to content


Photo

socket questions


  • Please log in to reply
11 replies to this topic

#1 tomfmason

tomfmason
  • Staff Alumni
  • Advanced Member
  • 1,696 posts
  • Locationstealing your wifi

Posted 20 August 2006 - 10:26 PM

Ok I am attempt to write a script that will recieve mail. This is just a testing script. I am wanting to accept the incomming mail and write it to a text file, so that I will see the format and then decide how to process it from there. The problem is that when I send an email to me@domain.com I get nothing. No error message or anything. Am I not commuicating with the server properly or ?

here is the socket.php
<?php
set_time_limit(0);
$port = 25;

$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ($socket == false) {
    die("Error in createing the socket, the error code is " . socket_last_error() . " 
	    and the error message is " . socket_strerror(socket_last_error()));
}
if (!socket_set_option($socket, SOL_SOCKET, SO_REUSEADDR, 1)) {
    die("Unable to set socket option. The error is " . socket_last_error() . "
	     and the error message is " . socket_strerror(socket_last_error()));
}
$bind = socket_bind($socket, NULL, $port);

if ($bind == false) {
    socket_close($socket);
	die("socket_bind failed. The error code is " . socket_last_error() . "
	     and the error message is " . socket_strerror(socket_last_error()));
}
$listen = socket_listen($socket, SOMAXCONN);
if ($listen == false) {
    socket_close($socket);
    die("socket_listen failed. The error code is " . socket_last_error() ."
	     and the error message is " . socket_strerror(socket_last_error()));
}
while(true){
   $accept = socket_accept($socket);
   $read = socket_read($accept, 1024, PHP_NORMAL_READ);
   if ($read == NULL) {
       die("Unable to read the sent file");
	   socket_close($socket);
   }
   if ($read = 'Exit') {
       socket_close($socket);
   }else{
      $filename = "C:\home\admin\test\test.txt";
	  $fp = fopen($filename, "x+b");
	  fwrite($fp, $read, 1024);
	  fclose($fp);
   }
}
socket_close($socket);
?>

Am I on the right track or am I way off? Any suggestions would be great.

Thanks,
Tom

Traveling East in search of instruction, and West to propagate the knowledge I have had gained.

current projects: pokersource

My Blog | My Pastebin | PHP Validation class | Backtrack linux


#2 ShogunWarrior

ShogunWarrior
  • Members
  • PipPipPip
  • Advanced Member
  • 528 posts
  • LocationIreland

Posted 20 August 2006 - 10:45 PM

Hey weird, exactly what I'm trying to do. And I've never seen a question like this and here we are a day apart.
I think you'll need to read up on the SMTP protocol on Wikipedia, there is a series of messages sent back and forward before you get the message body.
<a href="http://www.daviddora...nmedia.com/">My New Site/Blog</a> | <a href="http://www.daviddora...m/check/">Check your page for broken links/images/scripts</a>

Zend Certified Engineer
Follow me on Twitter: http://twitter.com/davidd

#3 tomfmason

tomfmason
  • Staff Alumni
  • Advanced Member
  • 1,696 posts
  • Locationstealing your wifi

Posted 20 August 2006 - 10:47 PM

thanks. I will check into that

Traveling East in search of instruction, and West to propagate the knowledge I have had gained.

current projects: pokersource

My Blog | My Pastebin | PHP Validation class | Backtrack linux


#4 tomfmason

tomfmason
  • Staff Alumni
  • Advanced Member
  • 1,696 posts
  • Locationstealing your wifi

Posted 21 August 2006 - 01:23 AM

Ok I checked out the SMTP protocol and I am running into another issue. I know now that I have to use
sock_write()
to send, something like this  220 mysite.com ESMTP, to the client. But how can I do this if I do not have there ip and port number.

here is what I have so far(dealing with this issue)

//the code above here is the same as my orginal post
while (true) {
     $accept = socket_accept($socket);
     socket_write($accept, '220 mysite.com ESMTP', 1024);

So what this says to me is that it will write that message back to my server. So how do I get the ip of the sender? Would I use
$_SERVER['REMOTE_ADDR'];
to obtain the ip from the sender?

Then do I need to create a socket to speak back to the sender. Or ?

Any suggestions would be great.

Thanks,
Tom

Traveling East in search of instruction, and West to propagate the knowledge I have had gained.

current projects: pokersource

My Blog | My Pastebin | PHP Validation class | Backtrack linux


#5 tomfmason

tomfmason
  • Staff Alumni
  • Advanced Member
  • 1,696 posts
  • Locationstealing your wifi

Posted 21 August 2006 - 02:55 AM

Is a socket capable of to way communication or no? What I mean is do I need to create a new socket for writing to the client?

Traveling East in search of instruction, and West to propagate the knowledge I have had gained.

current projects: pokersource

My Blog | My Pastebin | PHP Validation class | Backtrack linux


#6 tomfmason

tomfmason
  • Staff Alumni
  • Advanced Member
  • 1,696 posts
  • Locationstealing your wifi

Posted 21 August 2006 - 04:00 AM

Here is what I have so far. This is the most difficult script that I have tried to write to date. This is only because I cannot test it. If I am wrong I get nothing. No errors or anything.

Here we go. This is the same as above execpt for the while loop
$sender = array();
$data = array();
while (true) {
     $sender['ip'] = $_SERVER['REMOTE_ADDR'];
     $sender['port'] = $_SERVER['REMOTE_PORT'];
     $sender['sock'] = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
     $b = socket_bind($sender['sock'], $sender['ip'], $sender['port']);
     socket_write($sender['sock'], '220 mysite.com ESMPT', 1024);
     socket_recv($socket, $data[0], 1024, 0);
     if ($data[0] == 'QUIT') {
        socket_close($sender['sock']);
     }
     list($sender['greet'], $sender['domain']) = explode(" ", $data[0]);
     $ip = gethostbyname($domain);
     if ($ip !== $sender['ip']);
        socket_write($sender['sock'], 'QUIT' 1024);
        socket_close($sender['sock']);
     }else{
        socket_write($sender['sock'], 'Hellow ' . $sender['domain'] . '', 1024);
     }
     //continue with some more 
}

Ok does that look right. I still don't know if a socket can support two way communication or not.

Any suggestions would be great.

Thanks,
Tom
     

Traveling East in search of instruction, and West to propagate the knowledge I have had gained.

current projects: pokersource

My Blog | My Pastebin | PHP Validation class | Backtrack linux


#7 448191

448191
  • Staff Alumni
  • Advanced Member
  • 3,545 posts
  • LocationNetherlands

Posted 21 August 2006 - 05:13 AM

You won't be able to use any port lower than 1024 if you don't have root access on the servermachine.
If you can somehow 'trick' the client to use a higher port, you'd be fine.

#8 tomfmason

tomfmason
  • Staff Alumni
  • Advanced Member
  • 1,696 posts
  • Locationstealing your wifi

Posted 21 August 2006 - 05:15 AM

I have root access. By the way great pic ^

Traveling East in search of instruction, and West to propagate the knowledge I have had gained.

current projects: pokersource

My Blog | My Pastebin | PHP Validation class | Backtrack linux


#9 tomfmason

tomfmason
  • Staff Alumni
  • Advanced Member
  • 1,696 posts
  • Locationstealing your wifi

Posted 21 August 2006 - 05:23 AM

So am I right so far? Do I need to create another socket to write to the client or is the first socket capable of two way communication?

Any help would be greatly appericated.

Thanks,
Tom

Traveling East in search of instruction, and West to propagate the knowledge I have had gained.

current projects: pokersource

My Blog | My Pastebin | PHP Validation class | Backtrack linux


#10 tomfmason

tomfmason
  • Staff Alumni
  • Advanced Member
  • 1,696 posts
  • Locationstealing your wifi

Posted 21 August 2006 - 05:31 AM

I guess I will just have to keep messing with it untill I get it..lol. After I am done with this, every other script I write will seem easy in comparison..

Traveling East in search of instruction, and West to propagate the knowledge I have had gained.

current projects: pokersource

My Blog | My Pastebin | PHP Validation class | Backtrack linux


#11 448191

448191
  • Staff Alumni
  • Advanced Member
  • 3,545 posts
  • LocationNetherlands

Posted 21 August 2006 - 05:59 AM

If you look at this: http://www.zend.com/...als/sockets.php, you'll see that the same socket is used for both reading and writing.
For debugging, write to a log file. I you ARE going to create a full blown smtp server, a log file is must anyway.

#12 ShogunWarrior

ShogunWarrior
  • Members
  • PipPipPip
  • Advanced Member
  • 528 posts
  • LocationIreland

Posted 21 August 2006 - 02:47 PM

I tried sockets on my local PC but when I used telnet to access my 'PHP Server' socket_read would only read one character from telnet, I'm not sure what the problem was, so I haven't continued with it.
<a href="http://www.daviddora...nmedia.com/">My New Site/Blog</a> | <a href="http://www.daviddora...m/check/">Check your page for broken links/images/scripts</a>

Zend Certified Engineer
Follow me on Twitter: http://twitter.com/davidd




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users