SHARE
TWEET

Untitled

a guest Jun 19th, 2017 50 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5.  
  6. package socket;
  7.  
  8. import java.io.BufferedReader;
  9. import java.io.DataOutputStream;
  10. import java.io.InputStreamReader;
  11. import java.io.PrintWriter;
  12. import java.net.ServerSocket;
  13. import java.net.Socket;
  14.  
  15. /**
  16.  *
  17.  * @author Valter
  18.  */
  19. public class Main {
  20.  
  21.     /**
  22.      * @param args the command line arguments
  23.      */
  24.     public static void main(String[] args)throws Exception {
  25.         // TODO code application logic here
  26.        
  27.         String sentence;
  28.         String modifiedSentence;
  29.         BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in));
  30.         Socket clientSocket = new Socket("192.168.2.8", 5001);
  31.         DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
  32.         BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
  33.         sentence = inFromUser.readLine();
  34.         outToServer.writeBytes(sentence + '\n');
  35.         modifiedSentence = inFromServer.readLine();
  36.         System.out.println("FROM SERVER: " + modifiedSentence);
  37.         clientSocket.close();
  38.        
  39.        
  40.         /* // CLIENTE
  41.         String clientSentence;
  42.         String capitalizedSentence;
  43.         ServerSocket welcomeSocket = new ServerSocket(5001);
  44.         while (true) {
  45.             Socket connectionSocket = welcomeSocket.accept();
  46.             BufferedReader inFromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
  47.             DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream());
  48.             clientSentence = inFromClient.readLine();
  49.             capitalizedSentence = clientSentence.toUpperCase() + '\n';
  50.             outToClient.writeBytes(capitalizedSentence);
  51.             System.out.println("CLIENTE >> "+ clientSentence);
  52.         }
  53.         /*
  54.          * // SERVIDOR
  55.         String clientSentence;
  56.         String capitalizedSentence;
  57.         ServerSocket welcomeSocket = new ServerSocket(6789);
  58.         while (true) {
  59.             Socket connectionSocket = welcomeSocket.accept();
  60.             BufferedReader inFromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
  61.             DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream());
  62.             clientSentence = inFromClient.readLine();
  63.             capitalizedSentence = clientSentence.toUpperCase() + '\n';
  64.             outToClient.writeBytes(capitalizedSentence);
  65.         }
  66.         */
  67.    
  68.         /*
  69.          * // RICK
  70.               Sock.ClientSock Cli = new Sock.ClientSock();
  71.         Cli.Start("127.0.0.1", 5000, null);
  72.  
  73. // Em uma thread ou Timer vc envocaria os métodos desta forma
  74.       Cli.Send("c"); // comando "current"
  75.       string s = Cli.Receive();
  76.       if (!string.IsNullOrEmpty(s))
  77.       { label1.Text = s; }
  78.     }
  79.     */
  80. }
  81. }
RAW Paste Data
Want to get better at Java?
Learn to code Java in 2017
Pastebin PRO Summer Special!
Get 40% OFF on Pastebin PRO accounts!
Top