tugas jarkom,, berasa grafcit yang kedua..

hari ini nyari bahan jarkom,, dapet tugas bikin buat aplikasi yang yang memperlihatkan implementasi dari program client server yang sederhana yg menggunakan socket untuk kirim message lewat koneksi TCP . jadi kayak buat server yang bisa kirim pesan ke clientnya.

hahaha… judulnya ajah udah ribet,, =p

terserah pake bahasa apa sama c bapak,, tapi tetep ajah bingung,, maksudnya apa??

hmm,, bener harus belajar lagi nih.. mo beli buku di dunia baru ah,,

akhirnya dapat bahan di http://www.planetsourcecode.com.. memutuskan pake bahasa java,, biar sinkron ma OOP juga.

akhirnya dapet referensi yang isinya  begini:

//  Message accepted by Server in Dos Window

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

public class ServerAccept
{

public static void main(String ar[]) throws Exception
{
ServerSocket se =new ServerSocket(5001);
Socket s =se.accept();

DataInputStream da = new DataInputStream(s.getInputStream());
System.out.println(“Server Receiving Messages”);
while(true)
{
String c=da.readUTF();
System.out.println(“Message:”+c);
if(c.equals(“bye”))
break;
}
s.close();

}

}

tapi g ada TCP nya…

kalo yang ini ada, tapi ribet:

/** **************************************************************
* tcpServer.java
*
* usage : java tcpServer .
* default port is 5050.
* connection to be closed by client.
* this server handles only 1 connection; it is single threaded.
* *
*
* Copyright (c) 2002-2004 Advanced Applications Total Applications Works.
* (AATAW)  All Rights Reserved.
*
* AATAW grants you (“Licensee”) a non-exclusive, royalty free, license to use,
* modify and redistribute this software in source and binary code form,
* provided that i) this copyright notice and license appear on all copies of
* the software; and ii) Licensee does not utilize the software in a manner
* which is disparaging to AATAW.
*
* This software is provided “AS IS,” without a warranty of any kind. ALL
* EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
* IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
* NON-INFRINGEMENT, ARE HEREBY EXCLUDED. AATAW AND ITS LICENSORS SHALL NOT BE
* LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
* OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL AATAW OR ITS
* LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT,
* INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
* CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF
* OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGES.
*
* This software is not designed or intended for use in on-line control of
* aircraft, air traffic, aircraft navigation or aircraft communications; or in
* the design, construction, operation or maintenance of any nuclear
* facility. Licensee represents and warrants that it will not use or
* redistribute the Software for such purposes or for commercial purposes.
*
* Changelog:
****************************************************************
*/

import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;

public class tcpServer extends JFrame implements  ActionListener {
private int port = 5050;
private ServerSocket server_socket;
private BufferedReader input;
private PrintWriter output;
private Container c ;
private JTextArea display ;
private JButton cancel , send, exit;
private JPanel buttonPanel ;

/** This is the tcpServer constructor */
public  tcpServer() {
super ( “Server” ) ;

setup() ;

ServerRun() ;
}

/** The setUp() method does the intialization for the application */
public  void setup() {
c = getContentPane();

exit = new JButton( “Exit” );
exit.setBackground( Color.red ) ;
exit.setForeground( Color.white ) ;
buttonPanel = new JPanel() ;
buttonPanel.add( exit ) ;
c.add( buttonPanel , BorderLayout.SOUTH) ;

exit.addActionListener( this );

display = new JTextArea();
addWindowListener( new WindowHandler( this ) );
c.add( new JScrollPane( display ),
BorderLayout.CENTER );

setSize( 400, 400 );
setLocation( 10, 20 ) ;
show();
}

/**
* The run() method in the server reads and writes data to the client
*/
public void ServerRun() {
try {

server_socket = new ServerSocket( 5050, 100,
InetAddress.getByName(“127.0.0.1”));
display.setText(“Server waiting for client on port ” +
server_socket.getLocalPort() + “\n”);

// server infinite loop
while(true) {
Socket socket = server_socket.accept();
display.append(“New connection accepted ” +
socket.getInetAddress() +
“:” + socket.getPort() + “\n”);
input = new BufferedReader(new InputStreamReader(socket.getInputStream()));
output = new PrintWriter(socket.getOutputStream(),true);

// print received data
try {
while(true) {
String message = input.readLine();
display.append(“\n” + message);
output.println( “\nFrom Server==> ” + message );
if (message.toUpperCase().equals( “QUIT” ))
break;
}
}
catch (IOException e) {
display.append(“\n” + e);
}

// connection closed by client
try {
socket.close();
display.append(“\n Connection closed by client”);
}
catch (IOException e) {
display.append(“\n” + e);
}
}
}
catch (IOException e) {
display.append(“\n” + e);
}
}

/**
* This method responds to the exit button
*  being pressed on the tcpServer frame.
*/
public void actionPerformed( ActionEvent e )    {
if ( e.getSource() == exit )
closeConnection() ;
}

/**
* This method closes the socket connect to the server.
*/
private void closeConnection() {
try {
server_socket.close();
input.close();
System.exit( 0 );
}
catch (IOException e) {
display.append(“\n” + e);
System.exit( 0 ) ;
}
}

/**
* This method is the main entry point called by the JVM.
*/
public static void main(String args[]) {
final tcpServer server = new tcpServer() ;
server.addWindowListener(
new WindowAdapter() {
public void windowClosing( WindowEvent e )
{
server.closeConnection() ;
System.exit( 0 );
}
}
);
}

/**
* This method closes the socket connect to the server when the
* application window is closed.
*/
class WindowHandler extends WindowAdapter {
tcpServer tcpS;

public WindowHandler( tcpServer t ) { tcpS = t; }

public void windowClosing( WindowEvent e ) { tcpS.closeConnection(); }
}

}
contoh yang sulit,, tapi baiklah.. akan dicoba mengerjakan.. Smangatt!!! =D

Iklan

Tinggalkan Balasan

Isikan data di bawah atau klik salah satu ikon untuk log in:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout / Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout / Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout / Ubah )

Foto Google+

You are commenting using your Google+ account. Logout / Ubah )

Connecting to %s

%d blogger menyukai ini: