Tuesday, March 9, 2010

Suspension Polaris Xtra 10 Xlt

java



Server


import java.io.*;
import java.net.*;
import java.util.*;
public class ServerSemplice {
    PrintWriter[] clienti=new PrintWriter[20]; Public class
ElaboraCliente implements Runnable {BufferedReader reader
= null;
Socket sock = null;
/ / create the input stream
& # 160; public ElaboraCliente (Socket c) {

            try{

            sock=c;

            lettore=new BufferedReader(new InputStreamReader(sock.getInputStream()));

            }catch(Exception ex){                 ex.printStackTrace(); Cattura1             }         }         @ Override public void run () {& Cattura # 160; String message; ;

try {while ( (message = lettore.readLine ())!= null) { String ind indirizzo.toString = ();

& # 160; String m = ind + "" + message;

& # 160; talkative (m) &

# 160;} &

# 160; }catch(Exception ex){

                ex.printStackTrace();
            }
        }
    }
    public void go () {try {

/ / door listening
ServerSocket door = new ServerSocket (4242);
int i = 0;
while (true) {
/ / create the connection with the customer who knocked on the door
Socket portone.accept door = ();
/ / create the stream to write messages to the client
& # 160; PrintWriter writer = new PrintWriter (portina. getOutputStream ());
/ / Save this stream into an array of output
; customers [i] = writer;
& # 160; i + +;
& # 160; //........
& # 160 / / create an input stream for that client and I start to receive your messages
/ / send the received message to all clients
& # 160; Thread t=new Thread(new ElaboraCliente(portina));
                t.start();
                }
        }catch(Exception ex) {ex
. printStackTrace ();}


} & # 160; public void talker (String message) {
for (int i = 0; i \u0026lt;20; i + +) {
; if (customers [i]! = null) {
& # 160; try {
& # 160; customers [i]. println (message);
customers [i]. flush ();
System.out.println("Ho spedito il messaggio");
            }catch(Exception ex){
                ex.printStackTrace();
            }
        }
    }
}
    public static void main(String args[]){
        ServerSemplice s=new ServerSemplice();
        s.go();
    }
}

 

Client

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

public class Client {BufferedReader reader;
Socket sock;
PrintWriter writer;
JTextField t1;
JTextArea a1;
& # 160; JScrollPane ScrollPane;
public void window () {
JFrame f1 = new JFrame (" simple chat ");
f1.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
& # 160; JPanel p1 = new JPanel ();
a1 = new JTextArea (20,50);
t1 = new JTextField (200);
JButton b1 = new JButton ("invia il messaggio"); ;
p1.setLayout (new GridLayout (3,1));
& # 160; scrollPane = new JScrollPane (a1);
p1.add scrollPane ("BorderLayout . CENTER);

p1.add(t1);

      p1.add(b1);

      b1.addActionListener(new InviaMessaggio());

      f1.add(p1);

      f1.setSize(500,500);

      f1.setVisible(true);
      Thread tt=new Thread(new InarrivoDalServer ());
creaCollegamento ();
; tt.start ();}

public void creaCollegamento () {try {

& # 160; sock = new Socket ("192.168.3.18", 4242);
lettore = new BufferedReader (new InputStreamReader (sock.getInputStream ()));
writer = new PrintWriter (sock.getOutputStream ());
System.out.println ("Connection established") ;
} catch (Exception ex) {ex.printStackTrace
();}


} & # 160; implements ActionListener {public class InviaMessaggio
public void actionPerformed (ActionEvent e) { Try {

& # 160; scrittore.write (t1.getText ());
scrittore.println (t1.getText ());
& # 160; scrittore.flush () / / reset the content so as not to send the same message
} catch (Exception ex) {&
# 160; ex.printStackTrace ();

t1.setText} ( "");


}} public class InarrivoDalServer implements Runnable {
; public void run () {# &
160; String message;
try {
; while ((message = lettore.readLine ())!= null) {
a1.append (message + "\\ n");
& # 160; System.out.println ("read the message:" + message);
;}
;} catch (Exception ex) {&
# 160; ex.printStackTrace();
            }
        }
}
      public static void main(String args[]){
          Client cc=new Client();
          cc.finestra ();}

}



0 comments:

Post a Comment