TCPClient e TCPServer Multithreading

TCPServer.java

import java.io.*;
import java.net.*;
 
class TCPServer
{
    public static void main(String argv[]) throws Exception
    {
        ServerSocket welcomeSocket = new ServerSocket(6789);
 
        while(true)
        {
            Socket connectionSocket = welcomeSocket.accept();
            if (connectionSocket != null)
            {
                Client client = new Client(connectionSocket);
                client.start();
            }
        }
    }
}
 
class Client extends Thread
{
    private Socket connectionSocket;
    private String clientSentence;
    private String capitalizedSentence;
    private BufferedReader inFromClient;
    private DataOutputStream outToClient;
 
    public Client(Socket c) throws IOException
    {
        connectionSocket = c;
    }
 
    public void run() 
    {
        try
        {    
            inFromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
            outToClient = new DataOutputStream(connectionSocket.getOutputStream());
            clientSentence = inFromClient.readLine();
            capitalizedSentence = clientSentence.toUpperCase() + '\n';
            outToClient.writeBytes(capitalizedSentence);
        }
        catch(IOException e)
        {
            System.out.println("Errore: " + e);
        }
    }
}

TCPClient.java

import java.io.*;    
import java.net.*;
class TCPClient
{
    public static void main(String argv[]) throws Exception
    {
        String sentence;            
        String modifiedSentence;    
 
        Socket clientSocket = new Socket("localhost", 6789);
 
        BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in));
        DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());        
        BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
 
        sentence = inFromUser.readLine();
        outToServer.writeBytes(sentence + '\n');
        modifiedSentence = inFromServer.readLine();
        System.out.println("FROM SERVER: " + modifiedSentence);
 
        clientSocket.close();
 
    }
}
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License