CariDotMy

 Forgot password?
 Register

ADVERTISEMENT

12Next
Return to list New
View: 3731|Reply: 23

socket programming in window..

[Copy link]
Post time 27-3-2007 03:22 PM | Show all posts |Read mode
camner nak create socket programming dlm window..sbb sblm nih guna terminal dalam linux je.
harap sesiapa dapat membantu..tq tq tq
Reply

Use magic Report


ADVERTISEMENT


Post time 27-3-2007 04:10 PM | Show all posts

Reply #1 jij81's post

ada banyak cara..salah satuya guna VB yang menggunakan winsock..selain tu, boleh guna php socket programming melalui kaedah telnet (biasanya PuTTy) digunakan sebagai medium perantaraan untuk melakukan proses telnet antara client-server..dan byk lagi la..
Reply

Use magic Report

Post time 27-3-2007 11:31 PM | Show all posts

Reply #2 orang_letrik's post

php socket programming?

boleh tunjuk ajar sikit?
Reply

Use magic Report

Post time 28-3-2007 12:20 AM | Show all posts
ni coding untuk php client-server programming

//save sebagai client1.php
----------------------------------------------

<?php
error_reporting(E_ALL);
// $service_port = getservbyname('www', 'tcp');
$service_port = 1144;
$address = '192.168.1.2';
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ($socket < 0)
{
   echo "socket_create() failed: reason: ".socket_strerror($socket)."\n";
}
else
{
   echo "Socket Creation OK.<br>";
}
echo "Attempting to connect to '$address' on port '$service_port'...<br>";
$connect = socket_connect($socket, $address, $service_port);
if ($connect < 0)
{
   echo "socket_connect() failed.\nReason: ($connect) ".socket_strerror($connect)."\n";
}
else
{
   echo "Socket Connection OK.<br>";
}
// ************************************************************************
$i = 1;
while ($i<=30000)
{
        $mesej_untuk_server="Pelanggan1".$i.")<br>";
        socket_write($socket, $mesej_untuk_server, strlen($mesej_untuk_server));
        $baca_dari_server = socket_read($socket, 1024);
        echo "$baca_dari_server\n";
$i++;
}
// ************************************************************************
echo "Closing socket...";
socket_close($socket);
echo "OK.<br><br>";
?>

-----------------------------------------------



//save sebagai client2.php
-----------------------------------

<?php
error_reporting(E_ALL);
// $service_port = getservbyname('www', 'tcp');
$service_port = 1144;
$address = '192.168.1.3';
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ($socket < 0)
{
   echo "socket_create() failed: reason: ".socket_strerror($socket)."\n";
}
else
{
   echo "Socket Creation OK.<br>";
}
echo "Attempting to connect to '$address' on port '$service_port'...<br>";
$connect = socket_connect($socket, $address, $service_port);
if ($connect < 0)
{
   echo "socket_connect() failed.\nReason: ($connect) ".socket_strerror($connect)."\n";
}
else
{
   echo "Socket Connection OK.<br>";
}
// ************************************************************************
$i = 1;
while ($i<=30000)
{
        $mesej_untuk_server="Pelanggan2".$i.")<br>";
        socket_write($socket, $mesej_untuk_server, strlen($mesej_untuk_server));
        $baca_dari_server = socket_read($socket, 1024);
        echo "$baca_dari_server\n";
$i++;
}
// ************************************************************************
echo "Closing socket...";
socket_close($socket);
echo "OK.<br><br>";
?>

-------------------------------------------------


//save sebagai server2clients.php

<?
set_time_limit(0);
ob_implicit_flush();
$host = "192.168.1.1";
$port = 1144;

$socket = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create socket\n");
$result = socket_bind($socket, $host, $port) or die("Could not bind to socket\n");
$result = socket_listen($socket, 300) or die("Could not set up socket listener\n");

echo "Waiting for connections...$host at port $port ....\n";
$spawn = socket_accept($socket) or die("Could not accept incoming connection\n");
echo "Waiting for connections...$host at port $port ....\n";
$spawn1 = socket_accept($socket) or die("Could not accept incoming connection\n");

echo "<br>Received connection request\n";
$input = "";

// ***********************************************************************
$i = 1;
while (1)
{
   $baca_dari_client=socket_read($spawn, 1024) or die("Could not read input\n");
   echo "<br>Dari CLIENT X >>> : $baca_dari_client\n";



   $mesej_untuk_client = "mesej dari server (".$i.")<br>";
   socket_write($spawn, $mesej_untuk_client, strlen($mesej_untuk_client)) or die("Could not write output\n");

   $baca_dari_client1=socket_read($spawn1, 1024) or die("Could not read input\n");
   echo "<br>Dari CLIENT Y >>> : $baca_dari_client1\n";


   $mesej_untuk_client1 = "mesej dari server (".$i.")<br>";
   socket_write($spawn1, $mesej_untuk_client1, strlen($mesej_untuk_client1)) or die("Could not write output\n");
}

// ***********************************************************************
socket_close($spawn);
socket_close($socket);
socket_close($spawn1);

echo "<br>Socket terminated\n";
?>

--------------------------------------


untuk run simple program ni, run xampp dulu..pastu kena guna PuTTY sebagai medium perantaraan antara client ngan server
download dari sini: PUTTY
masukkan alamat IP server pastu guna function telnet (1144 - untuk port)
dah boleh send message...

[ Last edited by  orang_letrik at 28-3-2007 12:22 AM ]
Reply

Use magic Report

Post time 28-3-2007 01:45 AM | Show all posts
pegh.. selama ni aku wat guna java.. itu pun assignment tunang aku... erm kalu php lum penah gi cuba..
Reply

Use magic Report

Post time 28-3-2007 02:14 AM | Show all posts

Reply #5 liverpoolfctv's post

php senang sikit la kalau yang biasa ngan coding php..
Reply

Use magic Report

Follow Us
 Author| Post time 29-3-2007 01:39 PM | Show all posts

Reply #6 orang_letrik's post

tq for the info..
tp nak tnya laa nak run "xampp" tu cmner..yg puTTy tu dah try download dah.dah masukkan ip address n port 1144..skrg yg tak paham nak run "xampp"..
harap boleh bantu..terima kasih..
Reply

Use magic Report

Post time 29-3-2007 08:23 PM | Show all posts
download xampp dulu..
click here:
xampp software (freeware)

tutorial ni utk digunakan pada localhost (127.0.0.1)..kalo nk gna pada computer yang lain boleh je...computer server run server2client.php, client1.php dan client2.php yang diletakkan di dalam folder Xampp...setiap computer perlu melarikan (run) Xampp

masukkan file client1.php, client2.php dan server2client.php ke dalam folder berikut:
C:\Program Files\Xampp\htdocs
paste je file tersebut..then baru run xampp..

server mesti buka pelayar web (web browser) dan masukkan alamat http://localhost/server2client.php (bergantung pada folder yang ada dalam htdocs tu la)
lepas tu baru masukkan IP address destination dan port yang digunakan ke dalam software PuTTY..

bagi pelanggan nanti akan kuar interface macam command prompt..ni bermaksud perhubungan socket berjaya..dh boleh hantar message.

[ Last edited by  orang_letrik at 2-4-2007 01:41 PM ]
Reply

Use magic Report


ADVERTISEMENT


sufiyanna This user has been deleted
Post time 31-3-2007 12:18 PM | Show all posts

Reply #8 orang_letrik's post

mmm..download xampp dah...download puTTy pun dah..laa nih tinggal nak paste kan file dlm folder htdocs..
arinih lab tutup plk..cuti maulidur rasul..esok br leh smbung projek..
anyway tq.harap sudi bantu lagi eh..
Reply

Use magic Report

Post time 31-3-2007 04:25 PM | Show all posts

Reply #9 sufiyanna's post

xde hal nye..

click here utk contoh tutorial socket programming:
1server2client

[ Last edited by  orang_letrik at 31-3-2007 04:38 PM ]
Reply

Use magic Report

Post time 1-4-2007 12:39 AM | Show all posts
good info.


nanti saya bagi kredit... sorry connection slow...
Reply

Use magic Report

sufiyanna This user has been deleted
Post time 1-4-2007 09:24 PM | Show all posts

Reply #11 shahnazz's post

wah..bestnya dapat credit..eh utk saya ker utk orang_letrik tu?? hehhehe..
Reply

Use magic Report

 Author| Post time 2-4-2007 12:09 PM | Show all posts

Reply #10 orang_letrik's post

hi org_letrik..
saya dah try dah yg paste kan kat htdocs tu..mula2 progrm yg awak bg tu,saya paste kan kat notepad.pastu saya save as nama  server2clients.php kat htdocs.dlm htdocs folder tu ada folder lagikan...fobidden,restricted n lg 1 folder xampp..
then saya open web browser taip http://localhost/server2client.php ..tapi malang nyer.....error page yg kuar...page tu kata mcm nih------->>>


Object not found!
The requested URL was not found on this server. The link on the referring page seems to be wrong or outdated. Please inform the author of that page about the error.

If you think this is a server error, please contact the webmaster.

Error 404
localhost
04/02/07 13:02:11
Apache/2.2.4 (Win32) DAV/2 mod_ssl/2.2.4 OpenSSL/0.9.8d mod_autoindex_color PHP/5.2.1


----------->so how??
hrp bleh tolong lagi eh...
anyway tq for advanced...tq tq tq...
Reply

Use magic Report

sufiyanna This user has been deleted
Post time 2-4-2007 12:21 PM | Show all posts

Reply #13 jij81's post

jij81...wei..sibuk jer dia nih..hehehheee...
Reply

Use magic Report

sufiyanna This user has been deleted
Post time 2-4-2007 12:22 PM | Show all posts

Reply #8 orang_letrik's post

org_letrik..
harap dpt bantu sayaaaaaaaaaaaaa.....
tq,,..tq..
Reply

Use magic Report

Post time 2-4-2007 04:21 PM | Show all posts
ok..cuba coding yang senang dulu... amik coding ni:

//save sebagai server.php

<?
/* Cara untuk menggunakan server.php
1. buka http://localhost/server.php dari pelayar web
2. buka 2 client melalui telnet dengan PuTTy menggunakan maklumat yang sama iaitu
        host : 127.0.0.1
        port : 7777
3. pelanggan berhenti dengan memasukkan perkataan "quit"
*/
set_time_limit(0);
ob_implicit_flush();
$host = "127.0.0.1";
$port = 7777;

$socket = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create socket\n");
$result = socket_bind($socket, $host, $port) or die("Could not bind to socket\n");
$result = socket_listen($socket, 300) or die("Could not set up socket listener\n");

// sambungan ke client #1
echo "Menunggu pelanggan PERTAMA...$host at port $port ....<br>";
$socket_client1 = socket_accept($socket) or die("Could not accept incoming connection\n");
$tanya_nama_client1 = "Sambungan anda ke pelayan telah BERJAYA\n\rSila masukkan nama anda (max 8 huruf)> ";
socket_write($socket_client1, $tanya_nama_client1, strlen($tanya_nama_client1)) or die("Could not write output\n");
$enter=socket_read($socket_client1, 1024) or die("Could not read input\n");
$nama_client1=socket_read($socket_client1, 1024) or die("Could not read input\n");
$nama_client1=strtoupper($nama_client1);
echo "Sambungan dari pengguna  $nama_client1......<br>";
$selamat_datang = "\n\rSelamat datang ".$nama_client1."....\n\r";
socket_write($socket_client1, $selamat_datang, strlen($selamat_datang)) or die("Could not write output\n");

// sambungan ke client #2
echo "Menunggu pelanggan KEDUA...$host at port $port ....<br>";
$socket_client2 = socket_accept($socket) or die("Could not accept incoming connection\n");
$tanya_nama_client2 = "Sambungan anda ke pelayan telah BERJAYA\n\rSila masukkan nama anda (max 8 huruf)> ";
socket_write($socket_client2, $tanya_nama_client2, strlen($tanya_nama_client2)) or die("Could not write output\n");
$enter=socket_read($socket_client2, 1024) or die("Could not read input\n");
$nama_client2=socket_read($socket_client2, 1024) or die("Could not read input\n");
$nama_client2=strtoupper($nama_client2);
echo "Sambungan dari pengguna  $nama_client2.....<br>";
$selamat_datang = "\n\rSelamat datang ".$nama_client2."....\n\r";
socket_write($socket_client2, $selamat_datang, strlen($selamat_datang)) or die("Could not write output\n");

// beritahu sambungan chatters lain
$client1_login = "\n\r<".$nama_client1." telah berada di atas talian....>\n\r";
socket_write($socket_client2,$client1_login, strlen($client1_login)) or die("Could not write output\n");

$client2_login = "\n\r<".$nama_client2." telah berada di atas talian....>\n\r";
socket_write($socket_client1,$client2_login, strlen($client2_login)) or die("Could not write output\n");

// *********************************************************************************************************************************

while (1)
{
   $enter=socket_read($socket_client1, 1024) or die("Could not read input\n");
   $baca_dari_client1=socket_read($socket_client1, 1024) or die("Could not read input\n");
   if ($baca_dari_client1 == "quit")
   {
        $bye = "<".$nama_client1." telah KELUAR>\n\r";
        socket_write($socket_client2, $bye, strlen($bye)) or die("Could not write output\n");
        $i = 1;
        while ($i < 30000)        $i++;        // delay supaya client2 diberi mesej bahawa client1 dah KELUAR
        break;
   }
   echo "<br>$nama_client1>>> : $baca_dari_client1<br>";

   $mesej_untuk_client2 = "<From  ".$nama_client1."> ".$baca_dari_client1."\n\r>>> ";
   socket_write($socket_client2, $mesej_untuk_client2, strlen($mesej_untuk_client2)) or die("Could not write output\n");

   $enter=socket_read($socket_client2, 1024) or die("Could not read input\n");
   $baca_dari_client2=socket_read($socket_client2, 1024) or die("Could not read input\n");
   if ($baca_dari_client2 == "quit")
   {
        $bye = "<".$nama_client2." telah KELUAR>\n\r";
        socket_write($socket_client1, $bye, strlen($bye)) or die("Could not write output\n");
        $i = 1;
        while ($i < 30000)        $i++;        // delay supaya client1 diberi mesej bahawa client2 dah KELUAR
        break;
   }
   echo "<br>$nama_client2 >>> : $baca_dari_client2<br>";

   $mesej_untuk_client1 = "<From  ".$nama_client2."> ". $baca_dari_client2."\n\r>>> ";
   socket_write($socket_client1, $mesej_untuk_client1, strlen($mesej_untuk_client1)) or die("Could not write output\n");


}

// *********************************************************************************************************************************
socket_close($socket_client1);
socket_close($socket);
socket_close($socket_client2);

echo "<br>Socket terminated\n";
?>

-------------------------------------

//save sebagai client.php

<?
set_time_limit(0);
ob_implicit_flush();
ob_start();
$host = "127.0.0.1";
$port = 7777;

$socket = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create socket\n");
$result = socket_bind($socket, $host, $port) or die("Could not bind to socket\n");
$result = socket_listen($socket, 300) or die("Could not set up socket listener\n");

// sambungan ke client #1
echo "Menunggu pelanggan PERTAMA...$host at port $port ....<br>";
$socket_client1 = socket_accept($socket) or die("Could not accept incoming connection\n");
// *********************************************************************************************************************************
        ob_start();
        $masa1 = date('h:i:s');
        $str1 = "Masa sekarang ialah >".$masa1."\n\rSambungan akan diputuskan 10 saat dari sekarang...";
        socket_write($socket_client1, $str1, strlen($str1)) or die("Could not write output\n");
    sleep(10);
        $masa2 = date('h:i:s');
        $str2 = "\n\r>".$masa2."...sambungan telah diPUTUSKAN";
        socket_write($socket_client1, $str2, strlen($str2)) or die("Could not write output\n");
        $jumlah_masa = $masa2 - $masa1;
        echo $jumlah_masa;       
        sleep(10);
        ob_flush();
// *********************************************************************************************************************************
socket_close($socket_client1);
socket_close($socket);
echo "<br>Socket terminated\n";
?>

------------------------------------------------


masukkan coding tersebut ke dalam folder C:\Program Files\xampp\htdocs

seperti screenshot di bawah nie:



kemudiaan buka web browser, masukkan URL http://localhost/server.php

seperti screenshot di bawah ni:



kemudian run software PuTTY, masukkan alamat IP dan port yang telah digunakan..

pastikan nombor port sama dengan coding di dalam script php yang tertera pada file server.php dan client.php..

seperti screenshot di bawah ni:



akan tertera interface seperti di bawah:



kemudian run kembali PuTTY untuk client yang kedua..sama seperti alamat IP dan nombor port yang digunakan..

screenshot seperti di bawah:



akan tertera interface seperti di bawah:



seterusnya, process chatting boleh dilakukan antara kedua-dua client ni...

paparan pada web browser yang dilarikan (run) pada server akan tertera seperti berikut:



ni la namanya php socket programming...hehe

Rate

1

View Rating Log

Reply

Use magic Report


ADVERTISEMENT


Post time 2-4-2007 05:09 PM | Show all posts
socket programming guna vb.net pon boleh jugak.... ada contoh coding kat planet source code...
Reply

Use magic Report

Post time 2-4-2007 05:33 PM | Show all posts
Originally posted by jij81 at 2-4-2007 12:09 PM
hi org_letrik..
saya dah try dah yg paste kan kat htdocs tu..mula2 progrm yg awak bg tu,saya paste kan kat notepad.pastu saya save as nama  server2clients.php kat htdocs.dlm htdocs folder tu ada ...


mayb masalah pada software xampp

click sini untuk download yang version 1.5.2

xampp version 1.5.2
Reply

Use magic Report

sufiyanna This user has been deleted
Post time 2-4-2007 09:23 PM | Show all posts

Reply #16 orang_letrik's post

oooo tima kasih bebnyak...aper2 hal sy cuba dulu..
thanks a lot.."saya budak baru belajar"

tq tq tq.....
Reply

Use magic Report

sufiyanna This user has been deleted
Post time 2-4-2007 09:42 PM | Show all posts

Reply #16 orang_letrik's post

nak tnya lagi leh tak....mmm kalau server guna window client guna linux boleh tak??camner plk tu...??tq..tq..
Reply

Use magic Report

12Next
Return to list New
You have to log in before you can reply Login | Register

Points Rules

 

ADVERTISEMENT



 

ADVERTISEMENT


 


ADVERTISEMENT
Follow Us

ADVERTISEMENT


Mobile|Archiver|Mobile*default|About Us|CariDotMy

31-12-2024 06:33 AM GMT+8 , Processed in 0.067486 second(s), 37 queries , Gzip On, Redis On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

Quick Reply To Top Return to the list