0% found this document useful (0 votes)
2 views

Client

The document contains a Java class named 'client' that establishes a connection to a server using a specified port. It includes methods for connecting to the server and communicating by sending an integer input from the user. The class handles exceptions related to unknown hosts and connection errors.

Uploaded by

matteo61106
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Client

The document contains a Java class named 'client' that establishes a connection to a server using a specified port. It includes methods for connecting to the server and communicating by sending an integer input from the user. The class handles exceptions related to unknown hosts and connection errors.

Uploaded by

matteo61106
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

/*

* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to
change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this
template
*/
package clientmulticl;

import java.io.BufferedReader;
import java.io.*;
import java.net.*;
import java.util.Scanner;

/**
*
* @author matte
*/
public class client {

private String nomeServer;


private int portaServer;
private Scanner in;
private Socket clientSocket;
private BufferedReader inDalServer;
private DataOutputStream outVersoServer;
private int numDaInviareAlServer;
private String stringaUtente;
private String ricevuto;

public client(int porta) {


this.nomeServer = "localhost";
this.portaServer = porta;
}

public void connetti() {


System.out.println("2. Client in esecuzione");
try {
in = new Scanner(System.in);
clientSocket = new Socket(nomeServer, portaServer);
outVersoServer = new DataOutputStream(clientSocket.getOutputStream());
inDalServer = new BufferedReader(new
InputStreamReader(clientSocket.getInputStream()));

} catch (UnknownHostException e) {
System.err.println("Host sconosciuto");
} catch (Exception e) {
System.out.println(e.getMessage());
System.out.println("Errore durante la connessione!");
System.exit(1);
}
comunica();
}

public void comunica() {


int num=0;

try{
num = in.nextInt();//numero da inviare
System.out.println(num);//stampa per prova
outVersoServer.write(num);
clientSocket.close();
}

catch(Exception e){
System.out.println(e.getMessage());
}

}
}

You might also like