Jump to content

Archived

This topic is now archived and is closed to further replies.

tomfmason

socket questions

Recommended Posts

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
[code]
<?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);
?>[/code]

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

Thanks,
Tom

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Ok I checked out the SMTP protocol and I am running into another issue. I know now that I have to use [code=php:0]sock_write()[/code] 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)

[code=php:0]
//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);
[/code]

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 [code=php:0]$_SERVER['REMOTE_ADDR'];[/code] 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

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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
[code=php:0]
$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
}[/code]

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
     

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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..

Share this post


Link to post
Share on other sites
If you look at this: http://www.zend.com/pecl/tutorials/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.

Share this post


Link to post
Share on other sites
I tried sockets on my local PC but when I used [b]telnet[/b] to access my 'PHP Server' [b]socket_read[/b] would only read one character from [b]telnet[/b], I'm not sure what the problem was, so I haven't continued with it.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.