Page 1 of 1

Send/receive a message via TCP IP

Posted: Wed Oct 15, 2008 12:19 pm
by hairulazami
Heloo, my php programming is not so good ...
I need help with my problem:

I want to send a raw message from an IP that installed AppServ (Apache, PHP, MySQL packet installation) to a different IP that installed AppServ too

I tried to use this code to send a message first from IP: 192.168.0.1

Code: Select all

<?
//SEND MSG
$sock = @socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
$iptarget = "192.168.0.7";
$port=80;
$msg = "192.168.0.1/images/image01.jpg|2008-10-15 23:51:12";
$len = strlen($msg);
 
if(@socket_sendto($sock, $msg, $len, 0, "$iptarget", 80))
{
    echo "$msg = > sent to $iptarget:$port";
}
else
{
    echo "$msg = > not sent to $iptarget:$port";
}
@socket_close($sock);
?>
the code above was running successfully, but sometime i see the message is not send. The problem is while retrieving the sent $msg on IP 192.168.0.7 that sent from 192.168.0.1, the $msg was empty. I want to do a mysql_query("INSERT INTO new_msg(msg) VALUES('$msg')"); on IP 192.168.0.7 to save each of message record that will used next time.

This is my code that running on IP 192.168.0.7

Code: Select all

 
//RECEIVE MSG THEN INSERT IT INTO A NEW TABLE RECORD
<?php
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_bind($socket, '192.168.0.7', 80);
 
$from = "192.168.0.1";
$port = 80;
 
for($i=1;$i<=10;$i++)
{
    if(@socket_recvfrom($socket, $buf, 12, 0, $from, $port))
    {
        echo "Received $buf from IP $from and port $port" . PHP_EOL;
                mysql_query("INSERT INTO new_msg(msg) VALUES('$msg[$i]')");
    }
    else
    {
        echo "msg[$i] not received !";
    }
}
?>
 
 
the result is the empty message or maybe the IP 192.168.0.7 cannot retrieve the each of message, I am not understand.
I tried to search the solution at the other forum, but the result is still not resolved.

for the heroes of forum, please help me .... how to solve this problem.

Re: Send/receive a message via TCP IP

Posted: Wed Oct 15, 2008 9:45 pm
by hairulazami
no one like to help me ?? oh god :banghead:

Re: Send/receive a message via TCP IP

Posted: Thu Oct 16, 2008 1:20 am
by requinix
hairulazami wrote:no one like to help me ?? oh god :banghead:
That's right. The only possible reason for somebody not solving your problem for you is that nobody likes you.

You're hiding error messages by using @. Did it cross your mind to remove it and see if there's some sort of problem?

Re: Send/receive a message via TCP IP

Posted: Thu Oct 16, 2008 1:24 am
by jmut
tasairis wrote:
hairulazami wrote:no one like to help me ?? oh god :banghead:
That's right. The only possible reason for somebody not solving your problem for you is that nobody likes you.

You're hiding error messages by using @. Did it cross your mind to remove it and see if there's some sort of problem?
Really thats why noeone likes him 8O Dude said he doesn't know php that well...good chance he just copied code and adjusted.

Re: Send/receive a message via TCP IP

Posted: Thu Oct 16, 2008 3:11 am
by hairulazami
TO tasairis
I use @ to disabled warning message.

TO jmut
Thank for your replay. I am just a webmaster. I know my programming is not so good, so I tried to search on google and manual on php.net. But I am not understand to implement it. So I ask you :)

Then, if I try to use a sample code, is that wrong ??? The expert programmer should not said like that :)
I publish my personal website http://www.dremi.info and http://www.dremi.info/forum to share my little bit ability, but really I have a little bit experience about network function on php, so I visit this forum to learn.

Nice day dude !

Re: Send/receive a message via TCP IP

Posted: Thu Oct 16, 2008 7:15 am
by jmut
anyhow...while debugging you should remove those '@' and see if problem is not in there. Good chance it is.