Salut,
je ne crois pas qu'il y ai de moyen portable de lire la console caractère par caractère (peut-être Console (java6) mais pas testé), en revanche tu peux mettre au point un simple GUI avec un text field + un key event listener qui se charge d'envoyer ça sur un socket.
J'ai trouvé cet article qui parle de façons de faire via une console si c'est vraiment obligatoire : http://stackoverflow.com/questions [...] r-types-it
Voici un exemple, je te laisse le soin de gérer les exceptions correctement...
Code :
- import javax.swing.*;
- import java.awt.event.*;
- import java.awt.BorderLayout;
- import java.io.DataOutputStream;
- import java.io.IOException;
- import java.net.Socket;
- public class CharacterTcpClient extends JPanel implements KeyListener, ActionListener {
- private static final long serialVersionUID = 3556354942861359706L;
- private final JTextField typing;
- private Socket clientSocket;
- private DataOutputStream out;
- public CharacterTcpClient() {
- super(new BorderLayout());
-
- // a JTextField to listen for key typed, and to show what was typed
- typing = new JTextField();
- typing.setEditable(false);
- typing.addKeyListener(this);
- typing.setText("Press any key..." );
- typing.setHorizontalAlignment(JTextField.CENTER);
- add(typing, BorderLayout.PAGE_END);
- typing.requestFocusInWindow();
-
- // socket stuff
- try {
- clientSocket = new Socket("localhost", 1234);
- out = new DataOutputStream(clientSocket.getOutputStream());
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- /** Key typed in the JTextField */
- public void keyTyped(KeyEvent e) {
- showTyped(e, "KEY TYPED: " );
- }
- public void keyPressed(KeyEvent e) {}
- public void keyReleased(KeyEvent e) {}
- public void actionPerformed(ActionEvent e) {
- typing.requestFocusInWindow();
- }
- /** Show what was typed, and send it over TCP. */
- protected void showTyped(KeyEvent e, String s) {
- if (e.getID() != KeyEvent.KEY_TYPED) return;
-
- String typed = String.valueOf(e.getKeyChar());
- typing.setText(typed);
- try {
- out.writeBytes(typed + "\n" );
- } catch (IOException e1) {
- e1.printStackTrace();
- }
- }
- private static void runWindow() {
- JFrame.setDefaultLookAndFeelDecorated(true);
-
- // Create frame
- JFrame frame = new JFrame("Character TCP Client" );
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- // Create content
- JComponent pane = new CharacterTcpClient();
- pane.setOpaque(true);
- frame.setContentPane(pane);
- //Display
- frame.pack();
- frame.setVisible(true);
- }
- public static void main(String[] args) {
- // run the window
- javax.swing.SwingUtilities.invokeLater(new Runnable() {
- public void run() {
- runWindow();
- }
- });
- }
- }
|
Pour des tests tu peux aussi monter un simple serveur TCP en quelques lignes (là encore, la gestion des exceptions sera à ta charge) :
Code :
- import java.io.*;
- import java.net.*;
- class TcpServer {
- public static void main(String argv[]) throws Exception {
- ServerSocket serverSocket = new ServerSocket(1234);
- System.out.println("Waiting for someone..." );
- Socket client = serverSocket.accept();
- System.out.println("Accepted client" );
- BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
- String received = null;
- while ((received = in.readLine()) != null) {
- System.out.println("Received: " + received);
- }
- System.out.println("Client disconnected" );
- client.close();
- }
- }
|
Bonne chance pour la suite de ton programme...
---------------
Always wear a camera!