Klijen - Server aplikacija napisana u JAVA programskom jeziku. Vrlo jednostavno, moze se koristiti kao instant messaging ili kao command prompt. Evo koda:
----------
SERVER:
----------
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.JOptionPane;
import javax.swing.*;
public class Server extends JFrame {
private JTextArea displayarea;
private JTextField textfield = null;
private Socket connection;
private ServerSocket server;
private int counter = 1;
private ObjectInputStream input;
private ObjectOutputStream output;
private String msg;
private String xxx;
public Server()
{
super ("Serverska aplikacija");
Container container = getContentPane();
textfield = new JTextField();
textfield.addActionListener(
new ActionListener() {
public void actionPerformed( ActionEvent event) {
String mout = textfield.getText();
textfield.setText("");
try {
sendData (mout);
}
catch ( IOException ioException ) {
displayarea.append ("Greška u kucanju");
}
}
});
// displej aplikacije
container.add(textfield , BorderLayout.NORTH);
displayarea = new JTextArea();
container.add(new JScrollPane (displayarea), BorderLayout.CENTER);
setSize( 400, 400);
setVisible (true);
}
//-------------------------------------
public void runServer() // metoda za startovanje servera
{
try {
server = new ServerSocket(1600,10); // kreiranje novog soketa na serveru
while ( true ) {
waitforconnection();
getstreams();
pconnection();
close();
++ counter;
}
}
catch ( IOException ioException ) {
ioException.printStackTrace();
}
}
//-------------------------------------
private void waitforconnection() throws IOException
{
displayarea.append ("\nOCEKUJEM KONEKCIJU OD KLIJENTA...\n");
connection = server.accept();
displayarea.append("DOBIJAM ZAHTEV ZA KONEKCIJU...\n");
displayarea.append("KONEKCIJA BR. " + counter + " USPOSTAVLJENA SA: " + connection.getInetAddress().getHostName() + "\n");
}
//-------------------------------------
public void getstreams() throws IOException {
output = new ObjectOutputStream (connection.getOutputStream() );
output.flush();
input = new ObjectInputStream (connection.getInputStream() );
displayarea.append("POSTAVLJENI I/O STREAMOVI \n\n");
}
//-------------------------------------
public void pconnection() throws IOException{
msg = "\nKONEKCIJA USPOSTAVLJENA, CHAT JE OTVOREN!\nKOMANDA ZA ZATVARANJE KONEKCIJE JE CLOSE"; // ispisivanje pozdravne poruke na klijentu
output.writeObject ( msg );
output.flush();
do {
try{
msg = (String) input.readObject(); // čitanje input stream-a
displayarea.append("\n >> KLIJENT KAŽE: << " + msg); // ispisivanje output stream-a excommand(msg); } catch (ClassNotFoundException classNotFoundException ) { displayarea.append("NEPOZNATO"); } } while (!msg.equals("CLOSE")); // KOMANDA ZA ZATVARANJE KONEKCIJE! } //------------------------------------- // baratanje stringovima za ispisivanje poruka public void excommand(String z) { try { int exitv; int pstatus; int l; String ll; String cmd; String check = new String( ".exe"); String c = z; l = z.length(); if (l<5 cmd =" c;" cmd = "Cmd.exe /c " ll =" c.substring(" cmd =" c;" cmd =" c;" cmd = "Cmd.exe /c " cmd =" c" p =" Runtime.getRuntime().exec(cmd);" pstatus =" p.waitFor();" pstatus =" -1;" subp =" new" gar =" new" str =" subp.readLine()" str =" gar.readLine()" error="2," exitv =" p.exitValue();" pstatus ="="" xxx =" new"> " + c +" < "+ " Proces je uspešno izvršen!\n"); sendData (xxx); displayarea.append( xxx); } else { displayarea.append("KOMANDA> " + c + " <"+ " \nPROCES NIJE GOTOV \n"); sendData("KOMANDA> " + c + " <"+ " \nPROCES NIJE GOTOV \n"); } displayarea.append("Vrednost procesa nakon izlaska je: " + exitv +"\n" ); sendData("KOMANDA> " + c + " <"+ " Vrednost procesa nakon izlaska je: " + exitv +"\n"); } catch (IOException e) { System.err.println(e); } } //------------------------------------- // ovde ispisuje svoje poruke, da bi znao sta je rekao klijentu public void sendData (String x) throws IOException { output.writeObject ( x ); displayarea.append ("\n>> JA << " + x ); output.flush(); } //------------------------------------- // metod za zatvaranje konekcije public void close() throws IOException{ displayarea.append("\nDISKONEKTOVANJE... \n"); sendData ("\nDISKONEKTOVAN SERVER"); output.close(); input.close(); connection.close(); } //------------------------------------- // main metoda public static void main ( String args[] ) { Server application = new Server(); application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); application.runServer(); } } ---------- CLIENT: ---------- import java.io.*; import java.net.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; public class client extends JFrame { private JTextField entercommand; private JTextArea displayarea,displayarea1; private Socket clconnection; private String servern; private ObjectOutputStream output ; private ObjectInputStream input ; private String msg; public client ( String host ) { super("Klijentska aplikacija"); servern = host; Container container = getContentPane(); entercommand = new JTextField( ""); container.add(entercommand, BorderLayout.SOUTH); displayarea = new JTextArea(); container.add(new JScrollPane (displayarea), BorderLayout.CENTER); setSize( 500, 500); setVisible (true); entercommand.addActionListener( new ActionListener() { public void actionPerformed( ActionEvent event) { String ord = entercommand.getText(); try { sendData (ord); } catch ( IOException ioException ) { displayarea.append ("Greska!\n"); } } }); } //------------------------------------- public void runClient() { try { connectToServer(); getstreams(); pconnection(); close(); } catch ( IOException ioException ) { ioException.printStackTrace(); } } //------------------------------------- private void connectToServer() throws IOException { try { displayarea.setText ("\nUSPOSTAVLJAM KONEKCIJU SA SERVEROM....\n"); clconnection = new Socket( InetAddress.getByName ( servern ), 1600); displayarea.append( "USPOSTAVLJENA KONEKCIJA SA: " + clconnection.getInetAddress().getHostName() ); } catch ( IOException d ) { System.out.println(d); System.out.println("Exception za konekciju"); } } //------------------------------------- public void getstreams() throws IOException { try { output = new ObjectOutputStream (clconnection.getOutputStream() ); output.flush(); input = new ObjectInputStream (clconnection.getInputStream() ); displayarea.append("\nPOSTAVLJEN I/O STREAM \n"); } catch (IOException s) { System.out.println(s); System.out.println("Exception from GETSTREAM()"); } } //------------------------------------- public void pconnection() throws IOException { do { try{ msg = (String) input.readObject(); displayarea.append("\n >> SERVER KAŽE << " + msg); } catch (ClassNotFoundException classNotFoundException ) { displayarea.append("NEPOZNAT TEKST"); } } while (!msg.equals("CLOSE") ); } //------------------------------------- public void sendData (String x) throws IOException { output.writeObject ( x ); displayarea.append ("\n>> JA << " + x );
entercommand.setText("");
output.flush();
}
//-------------------------------------
public void close() throws IOException {
displayarea.append("\n Zatvaranje konekcije. .. .. ");
output.close();
input.close();
clconnection.close();
}
//-------------------------------------
public static void main( String[] args ) {
client app;
if (args.length == 0 )
app = new client("127.0.0.1");
else
app = new client( args [0] ) ;
app.runClient();
app.setDefaultCloseOperation(
JFrame.EXIT_ON_CLOSE );
}
}
------------------
Pokrenete prvo server, zatim konektujete klijenta i to je to!
Pazite samo na ip adrese ukoliko konektujete na razlicitim racunarima!
Пријавите се на:
Објављивање коментара (Atom)
Нема коментара:
Постави коментар