Guest User

Untitled

a guest
Nov 23rd, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.84 KB | None | 0 0
  1. //FIg17.6 - server.java
  2. //configura um servidor que ira receber pacotes de um cliente e enviar-lhe pacotes
  3.  
  4. import java.io.*;
  5. import java.net.*;
  6. import java.awt.*;
  7. import java.awt.event.*;
  8. import java.util.*;
  9.  
  10. import javax.swing.*;
  11.  
  12. public class Server1 extends JFrame
  13. {
  14. private JTextArea displayArea;
  15. private DatagramPacket sendPacket, receivePacket;
  16. private DatagramSocket socket;
  17.  
  18. public Server1()
  19. {
  20. super("Server");
  21.  
  22. displayArea = new JTextArea();
  23. getContentPane().add(new JScrollPane (displayArea), BorderLayout.CENTER);
  24. setSize (400,300);
  25. setVisible(true);
  26.  
  27. //cria DatagramSocket para enviar e receber pacotes
  28. try
  29. {
  30. socket = new DatagramSocket(5000);
  31. }
  32. catch (SocketException socketException)
  33. {
  34. socketException.printStackTrace();
  35. System.exit(1);
  36. }
  37. }
  38.  
  39. //espera que os pacotes cheguem e, depois, exibe os dados e ecoa o pacote p/ o cliente
  40. public void waitForPackets()
  41. {
  42. while (true)
  43. {
  44. //recebe o pacote, exibe o conteudo e ecoa p/ o cliente
  45. try
  46. {
  47. byte data[] = new byte[100];
  48. receivePacket = new DatagramPacket (data,data.length);
  49.  
  50. //espera o pacote
  51. socket.receive (receivePacket);
  52.  
  53. //processa o pacote
  54. displayPacket();
  55.  
  56. //ecoa as informacoes do pacote de volta p/ o cliente
  57. sendPacketToClient();
  58. }
  59. catch (IOException ioException)
  60. {
  61. displayArea.append(ioException.toString() + "\n");
  62. ioException.printStackTrace();
  63. }
  64. }
  65. }
  66.  
  67. //exibe conteudo do pacote
  68. private void displayPacket()
  69. {
  70.  
  71. //� aqui que os dados sao recebidos em bytes, convertidos p/ string e jogados nos
  72. //seus campos pelo Token
  73. String SNome="",SData="",SHora="";
  74. String dados = new String(receivePacket.getData(),0,receivePacket.getLength());
  75. String auxToken = "";
  76. int iCont=0;
  77. StringTokenizer tokens = new StringTokenizer(dados);
  78.  
  79. while (tokens.hasMoreTokens())
  80. {
  81.  
  82. SNome = tokens.nextToken();
  83. SData = tokens.nextToken();
  84. SHora = tokens.nextToken();
  85.  
  86. }
  87. displayArea.append("Nome: " + SNome +"\nData: " + SData + "\nHora: " + SHora);
  88.  
  89. /*displayArea.append("\n Packet received: " +
  90. "\nFrom host : " + receivePacket.getAddress() +
  91. "\nHost port : " + receivePacket.getPort() +
  92. "\nLength : " + receivePacket.getLength() +
  93. "\nContaining: \n\t" + auxToken);*/
  94.  
  95.  
  96. }
  97.  
  98.  
  99. //ecoa pacote p/ o cliente
  100. private void sendPacketToClient() throws IOException
  101. {
  102. displayArea.append("\n\nEcho data to client...");
  103.  
  104. //cria pacote p/ enviar
  105. /*sendPacket = new DatagramPacket(receivePacket.getData(),
  106. receivePacket.getLength(),
  107. receivePacket.getAddress(),
  108. receivePacket.getPort());
  109. //envia o pacote
  110. socket.send(sendPacket);*/
  111.  
  112. String message = "MENSAGEM ENTREGUE COM SUCESSO!!!";//event.getActionCommand();
  113. byte data[] = message.getBytes();
  114. sendPacket = new DatagramPacket(data,data.length,receivePacket.getAddress(),receivePacket.getPort());
  115. socket.send(sendPacket);
  116.  
  117. displayArea.append("Packet sent\n\n================================");
  118. displayArea.setCaretPosition(displayArea.getText().length());
  119. }
  120.  
  121. //executa o aplicativo
  122. public static void main (String args[])
  123. {
  124. Server1 application = new Server1();
  125. application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  126. application.waitForPackets();
  127. }
  128. }
  129.  
  130.  
  131.  
  132. // Fig. 17.7: Client.java
  133. // Set up a Client that will send packets to a
  134. // server and receive packets from a server.
  135.  
  136. // Java core packages
  137. import java.io.*;
  138. import java.net.*;
  139. import java.awt.*;
  140. import java.awt.event.*;
  141. import java.util.*;
  142.  
  143. // Java extension packages
  144. import javax.swing.*;
  145.  
  146. public class Client1 extends JFrame {
  147. private JTextField enterField;
  148. private JTextArea displayArea;
  149. private DatagramPacket sendPacket, receivePacket;
  150. private DatagramSocket socket;
  151.  
  152. // set up GUI and DatagramSocket
  153. public Client1()
  154. {
  155. super( "Client" );
  156.  
  157. Container container = getContentPane();
  158.  
  159. enterField = new JTextField( " " );
  160.  
  161. enterField.addActionListener(
  162.  
  163. new ActionListener() {
  164.  
  165. // create and send a packet
  166. public void actionPerformed( ActionEvent event )
  167. {
  168. // create and send packet
  169. try
  170. {
  171. displayArea.append("\nSending packet containing: " + event.getActionCommand() + "\n" );
  172.  
  173. // get message from textfield and convert to
  174. // array of bytes
  175. String Nome="Bruno",Hora="15:30";
  176. String Data="14/02/1982";
  177. String message = /*event.getActionCommand()+*/ Nome + " " + Data + " "+ Hora;
  178. byte data[] = message.getBytes();
  179.  
  180. //TOKENS
  181. /*String stringToTokenize = event.getActionCommand();
  182. StringTokenizer tokens = new StringTokenizer(stringToTokenize);*/
  183.  
  184. // create sendPacket
  185. sendPacket = new DatagramPacket(data,data.length, InetAddress.getByName("localhost"), 5000 );
  186.  
  187. // send packet
  188. socket.send( sendPacket );
  189.  
  190. displayArea.append( "Packet sent\n" );
  191. displayArea.setCaretPosition(displayArea.getText().length() );
  192. }
  193.  
  194. // process problems creating or sending packet
  195. catch ( IOException ioException ) {
  196. displayArea.append(
  197. ioException.toString() + "\n" );
  198. ioException.printStackTrace();
  199. }
  200.  
  201. } // end actionPerformed
  202.  
  203. } // end anonymous inner class
  204.  
  205. ); // end call to addActionListener
  206.  
  207. container.add( enterField, BorderLayout.NORTH );
  208.  
  209. displayArea = new JTextArea();
  210. container.add( new JScrollPane( displayArea ), BorderLayout.CENTER );
  211.  
  212. setSize( 400, 300 );
  213. setVisible( true );
  214.  
  215. // create DatagramSocket for sending and receiving packets
  216. try
  217. {
  218. socket = new DatagramSocket();
  219. }
  220.  
  221. // catch problems creating DatagramSocket
  222. catch( SocketException socketException )
  223. {
  224. socketException.printStackTrace();
  225. System.exit( 1 );
  226. }
  227.  
  228. } // end Client constructor
  229.  
  230. // wait for packets to arrive from Server,
  231. // then display packet contents
  232. public void waitForPackets()
  233. {
  234. // loop forever
  235. while ( true )
  236. {
  237. // receive packet and display contents
  238. try
  239. {
  240.  
  241. // set up packet
  242. byte data[] = new byte[ 100 ];
  243. receivePacket = new DatagramPacket( data, data.length );
  244.  
  245. // wait for packet
  246. socket.receive( receivePacket );
  247.  
  248. // display packet contents
  249. displayPacket();
  250. }
  251.  
  252. // process problems receiving or displaying packet
  253. catch( IOException exception )
  254. {
  255. displayArea.append( exception.toString() + "\n" );
  256. exception.printStackTrace();
  257. }
  258.  
  259. } // end while
  260.  
  261. } // end method waitForPackets
  262.  
  263. // display contents of receivePacket
  264. private void displayPacket()
  265. {
  266. displayArea.append( "\nPacket received:" +
  267. "\nFrom host: " + receivePacket.getAddress() +
  268. "\nHost port: " + receivePacket.getPort() +
  269. "\nLength: " + receivePacket.getLength() +
  270. "\nContaining:\n\t" +
  271. new String( receivePacket.getData(), 0, receivePacket.getLength() ) );
  272.  
  273. displayArea.setCaretPosition(displayArea.getText().length() );
  274. }
  275.  
  276. // execute application
  277. public static void main( String args[] )
  278. {
  279. Client1 application = new Client1();
  280.  
  281. application.setDefaultCloseOperation(
  282. JFrame.EXIT_ON_CLOSE );
  283.  
  284. application.waitForPackets();
  285. }
  286.  
  287. } // end class Client
Add Comment
Please, Sign In to add comment