Send/receive a message via TCP IP

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
hairulazami
Forum Newbie
Posts: 4
Joined: Wed May 28, 2008 10:05 pm

Send/receive a message via TCP IP

Post 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.
hairulazami
Forum Newbie
Posts: 4
Joined: Wed May 28, 2008 10:05 pm

Re: Send/receive a message via TCP IP

Post by hairulazami »

no one like to help me ?? oh god :banghead:
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Send/receive a message via TCP IP

Post 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?
jmut
Forum Regular
Posts: 945
Joined: Tue Jul 05, 2005 3:54 am
Location: Sofia, Bulgaria
Contact:

Re: Send/receive a message via TCP IP

Post 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.
hairulazami
Forum Newbie
Posts: 4
Joined: Wed May 28, 2008 10:05 pm

Re: Send/receive a message via TCP IP

Post 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 !
jmut
Forum Regular
Posts: 945
Joined: Tue Jul 05, 2005 3:54 am
Location: Sofia, Bulgaria
Contact:

Re: Send/receive a message via TCP IP

Post by jmut »

anyhow...while debugging you should remove those '@' and see if problem is not in there. Good chance it is.
Post Reply