Welcome To Portal Komuniti Muslimah -- Hanan.com.my.my

  Create An Account Home  ·  Topik  ·  Statistik  ·  Your Account  ·  Hantar Artikel  ·  Top 10 19-09-2019  

  Suka!
New Page 1

  Mutiara Kata
Sahabat yang beriman ibarat mentari yang menyinar. Sahabat yang setia bagai pewangi yang mengharumkan. Sahabat sejati menjadi pendorong impian. Sahabat berhati mulia membawa kita ke jalan Allah.
-- suatumasa

  Menu Utama

  Keahlian Portal Komuniti
Terkini: lysazae
Hari Ini: 0
Semalam: 0
Jumlah Ahli: 13715

  Sedang Online
Assalamualaikum
Tetamu

Nickname

Password



[ Mendaftar ]

Sedang Online:
Tetamu: 36
Ahli: 0
Jumlah: 36




  Dari Galeri
sebahagian muslimah yg hadir...

  Yang Masuk Ke Sini
nishazainal: 2 hari yang lalu
uzmawahida: 10 hari yang lalu
fayyzah: 22 hari yang lalu
bintanghatee: 28 hari yang lalu


Komputer dan IT
Topik: Java Source Code


Carian Forum

Moderator: Pengurusan
Portal Komuniti Muslimah -- Hanan.com.my.my Indeks Forum
  »» Komputer dan IT
    »» Java Source Code

Please Mendaftar To Post


Oleh Java Source Code

yusha
WARGA RASMI
Menyertai: 22.08.2006
Ahli No: 6811
Posting: 10

blank   avatar


posticon Posting pada: 20-08-07 13:26


TCP IP PROGRAMMING...

siapa ada ambil sebab ini?

saja tanya.....
InsyaAllah, kalau ada masa lapang..
script yang dipelajar akan diletakkan kat sini...
hanya untuk muslimah yang rajin dalam programming...
tension juga bila cari source code kat google dan yahoo...
susah nak cari yang "kita" punya....
--------------------------------------------------------------

Code yang melibatkan SERVER dan Client, yang menggunakan UDP
Tujuan code ini untuk menukarkan celcius pada farenheit

SERVER SIDE

import java.net.*;
import java.io.*;

public class Server
{
public static void main (String args)
{
    try
    {
    //Declare buffers, port number
    byte rcv_buffer = new byte;
    byte send_buffer = new byte;
    int port = 3000;
    int des_port;
    
    //process 1
    DatagramSocket socket = new DatagramSocket(port);

//process 2
DatagramPacket rcv_packet = new DatagramPacket(rcv_buffer,rcv_buffer.length);
    
//Process 4
    socket.receive(rcv_packet);
    
    //Process 6
//use ByteArrayInputStream to read data from packet into input stream and convert into human readable form using InputStreamReader and BufferedReader for String type, DataInputStream for numeric type
ByteArrayInputStream byteIn = new ByteArrayInputStream(rcv_packet.getData(),rcv_packet.getOffset(), rcv_packet.getLength());
    DataInputStream dataIn = new DataInputStream (byteIn);
    double farenheit = dataIn.readDouble();
    
//Process 7
    double celcius = Math.round(0.5556 * (farenheit - 32));

//Write the temperature in double into an output stream and convert the output stream into bytes of array
    ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
    DataOutputStream dataOut = new DataOutputStream(byteOut);
    dataOut.writeDouble(celcius);
    dataOut.flush();
    send_buffer = byteOut.toByteArray();
    byteOut.flush();



//Process 8
to send the area to the client, server needs to construct a new DatagramPacket
The server also needs to find out the client’s address and port number

    InetAddress destination = rcv_packet.getAddress();
    des_port = rcv_packet.getPort();

DatagramPacket send_packet = new DatagramPacket (send_buffer, send_buffer.length, destination, des_port);

// Process 9
    socket.send(send_packet);
    }

    catch(SocketException socketerr)
    {
        System.out.println("Socket error : " + socketerr);
    }
    catch(IOException ioerr)
    {
        System.out.println("I/O error : " + ioerr);
    }
}
}


CLIENT SIDE

import java.net.*;
import java.io.*;

public class Client
{
public static void main (String args)
{
    try
    {
// Declare buffers for sending and receiving packets in bytes, server address, port number

    byte rcv_buffer = new byte;
    byte send_buffer = new byte;
    InetAddress server_add = InetAddress.getByName("127.0.0.1");
    int portnum = 3000;

    //PROCESS 1
    
    DatagramSocket socket = new DatagramSocket();
    
    // To receive input from user and convert into type double
    //InputStream input = System.in;
    //InputStreamReader reader = new InputStreamReader(input);
    InputStreamReader reader = new InputStreamReader(System.in);
    BufferedReader bufReader = new BufferedReader(reader);

    System.out.print("Enter the temperature in Farenheit : ");
    String temperature = bufReader.readLine();
    double farenheit = Double.valueOf(temperature).doubleValue();

//Write the integer into an output stream and convert the output stream into bytes of array
    ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
    DataOutputStream dataOut = new DataOutputStream(byteOut);

    dataOut.writeDouble(farenheit);
    dataOut.flush();
    send_buffer=byteOut.toByteArray();
    byteOut.flush();

    //Process 3
DatagramPacket send_packet = new DatagramPacket(send_buffer, 0, send_buffer.length, server_add, portnum);




//Process 5
    socket.send(send_packet);
    
//Process 10
DatagramPacket rcv_packet = new DatagramPacket(rcv_buffer, rcv_buffer.length);
    socket.setSoTimeout(3000);

    //Process 11
    socket.receive(rcv_packet);

    //Process 11
ByteArrayInputStream byteIn = new ByteArrayInputStream(rcv_packet.getData(), rcv_packet.getOffset(), rcv_packet.getLength());

    DataInputStream dataIn = new DataInputStream(byteIn);

    double celcius = dataIn.readDouble();

    System.out.println("Temperature in Celcius : ");
    System.out.println(celcius);
    //Process 13
    socket.close();
}
catch(SocketException socketerr)
    {
        System.out.println("Socket error : " + socketerr);
    }
    catch(IOException ioerr)
    {
        System.out.println("I/O error : " + ioerr);
    }
}
}



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


Bookmark and Share




  Member Information For yushaProfil   Hantar PM kepada yusha   Quote dan BalasQuote

Member Messages

Forum Search & Navigation

 

Log in to check your private messages

Silakan Login atau Mendaftar





  


 

[ Carian Advance ]

Jum ke Forum 


-------------------------------------------------------------------------------
Portal Komuniti Muslimah
© Hakcipta 2003 oleh Hanan Alam Faizli / Hanan Network
Made in: Bandar Sunway, Selangor
Tarikh Mula: 17hb April 2003

Dibina oleh: Team Walasri

Ditadbir oleh:





Loading: 0.054228 saat. Lajunya....