Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- SERVER
- package server;
- import java.io.*;
- import java.net.*;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- public class Server {
- ServerSocket server;
- Socket socket;
- int porta;
- DataInputStream in;
- DataOutputStream out;
- public Server() throws Exception {
- try {
- server = new ServerSocket(7777);
- } catch (Exception e) {
- System.err.println ("[#] Server non creato ");
- }
- System.out.println ("[#] Server creato");
- socket = server.accept();
- }
- public void comunica() throws Exception {
- in = new DataInputStream(socket.getInputStream());
- out = new DataOutputStream(socket.getOutputStream());
- try {
- System.out.println ("Messaggio: ");
- String messaggioLetto = in.readLine();
- System.out.println ("Messaggio ricevuto: "+ messaggioLetto);
- System.out.println ("Rispondo con: "+messaggioLetto);
- out.writeBytes(messaggioLetto+"\n"); // Inviamo il messaggio
- } catch(IOException e ) {
- e.printStackTrace();
- }
- }
- public void chiudi () {
- try {
- socket.close();
- } catch (IOException ex) {
- Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- public static void main(String[] args) {
- Server server=null;
- try {
- server = new Server();
- } catch (Exception ex) {
- Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
- }
- try {
- server.comunica();
- } catch (Exception ex) {
- Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
- }
- server.chiudi();
- }
- }
- CLIENT
- package client;
- import java.io.*;
- import java.net.*;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- public class Client {
- Socket client;
- DataInputStream in;
- DataOutputStream out;
- BufferedReader input = new BufferedReader (new InputStreamReader(System.in));
- public Client() throws IOException {
- try {
- client = new Socket(InetAddress.getLocalHost(),7777);
- } catch (Exception e) {
- System.err.println("[#]Client non connesso");
- }
- System.out.println ("[#]Client connesso");
- client.close();
- }
- public void comunica() throws IOException {
- System.out.println ("Messaggio da inviare: ");
- String messaggio = input.readLine();
- out.writeBytes(messaggio+ "/n");
- String ricevuto = in.readLine();
- System.out.println ("Messaggio ricevuto: "+ricevuto);
- }
- public static void main(String[] args) throws Exception {
- Client client = new Client();
- client.comunica();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement