123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106 |
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package rsx.tcp;
- import java.io.BufferedReader;
- import java.io.DataOutputStream;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.net.InetAddress;
- import java.net.Socket;
- import java.net.UnknownHostException;
- /**
- *
- * @author loquicom
- */
- public class TcpClient {
-
- protected InetAddress adr;
- protected int port;
- protected Socket socket;
- protected BufferedReader input;
- protected DataOutputStream output;
-
- /* --- Constructeurs --- */
-
- public TcpClient(String ip, int port) throws UnknownHostException{
- this.adr = InetAddress.getByName(ip);
- this.port = port;
- }
-
- public TcpClient(InetAddress adr, int port){
- this.adr = adr;
- this.port = port;
- }
-
- /* --- Methodes --- */
-
- public boolean connect(){
- try {
- this.socket = new Socket(this.adr, this.port);
- this.input = new BufferedReader(new InputStreamReader(this.socket.getInputStream()));
- //this.output = new PrintWriter(new BufferedWriter(new OutputStreamWriter(this.socket.getOutputStream())));
- this.output = new DataOutputStream(this.socket.getOutputStream());
- } catch (IOException ex) {
- System.err.println("Impossible de se connecter au serveur : " + ex.getMessage());
- return false;
- }
- return true;
- }
-
- public boolean send(String msg){
- try {
- output.writeBytes(msg);
- } catch (IOException ex) {
- System.err.println("Impossible d'envoyer le message : " + ex.getMessage());
- return false;
- }
- return true;
- }
-
- public String receive(){
- try {
- return input.readLine();
- } catch (IOException ex) {
- System.err.println("Impossible de lire : " + ex.getMessage());
- return null;
- }
- }
-
- public boolean close(){
- try {
- input.close();
- output.close();
- socket.close();
- } catch (IOException ex) {
- System.err.println("Impossible de de fermer le client : " + ex.getMessage());
- return false;
- }
- return true;
- }
-
- /* --- Getter/Setter --- */
- public InetAddress getAdr() {
- return adr;
- }
- public void setAdr(InetAddress adr) {
- this.adr = adr;
- }
- public int getPort() {
- return port;
- }
- public void setPort(int port) {
- this.port = port;
- }
-
-
-
- }
|