Commit b88d32c4 authored by Luca Arrotta's avatar Luca Arrotta

first commit

parent c3502d55
setup_SDP
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="GradleMigrationSettings" migrationVersion="1" />
<component name="GradleSettings">
<option name="linkedExternalProjectsSettings">
<GradleProjectSettings>
......
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="" vcs="Git" />
</component>
</project>
\ No newline at end of file
TCPServer.java
TCPServer
TCPClient.java
TCPClient
iterative/Client.java
iterative.Client
iterative/IterativeServer.java
iterative.IterativeServer
multithread/MultiServer.java
multithread.MultiServer
multithread/ServerThread.java
multithread.ServerThread
import java.io.*;
import java.net.*;
class TCPClient {
public static void main(String argv[]) throws Exception {
String sentence;
String modifiedSentence;
/* Inizializza l'input stream (da tastiera) */
BufferedReader inFromUser =
new BufferedReader(new InputStreamReader(System.in));
/* Inizializza una socket client, connessa al server */
Socket clientSocket = new Socket("localhost", 6789);
/* Inizializza lo stream di output verso la socket */
DataOutputStream outToServer =
new DataOutputStream(clientSocket.getOutputStream());
/* Inizializza lo stream di input dalla socket */
BufferedReader inFromServer =
new BufferedReader(new
InputStreamReader(clientSocket.getInputStream()));
/* Legge una linea da tastiera */
sentence = inFromUser.readLine();
/* Invia la linea al server */
outToServer.writeBytes(sentence + '\n');
/* Legge la risposta inviata dal server (linea terminata da \n) */
modifiedSentence = inFromServer.readLine();
System.out.println("FROM SERVER: " + modifiedSentence);
clientSocket.close();
}
}
import java.io.*;
import java.net.*;
class TCPServer {
public static void main(String argv[]) throws Exception
{
String clientSentence;
String capitalizedSentence;
/* Crea una "listening socket" sulla porta specificata */
ServerSocket welcomeSocket = new ServerSocket(6789);
while(true) {
/*
* Viene chiamata accept (bloccante).
* All'arrivo di una nuova connessione crea una nuova
* "established socket"
*/
Socket connectionSocket = welcomeSocket.accept();
/* Inizializza lo stream di input dalla socket */
BufferedReader inFromClient =
new BufferedReader(new
InputStreamReader(connectionSocket.getInputStream()));
/* Inizializza lo stream di output verso la socket */
DataOutputStream outToClient =
new DataOutputStream(connectionSocket.getOutputStream());
/* Legge una linea (terminata da \n) dal client */
clientSentence = inFromClient.readLine();
capitalizedSentence = clientSentence.toUpperCase() + '\n';
/* Invia la risposta al client */
outToClient.writeBytes(capitalizedSentence);
connectionSocket.close();
}
}
}
package iterative;
import java.io.*;
import java.net.*;
class Client {
public static void main(String argv[]) throws Exception {
String sentence;
String modifiedSentence;
// input stream initialization (from user keyboard)
BufferedReader inFromUser =
new BufferedReader(new InputStreamReader(System.in));
/* client socket initialization
localhost: server address
6789: server service port number */
Socket clientSocket = new Socket("localhost", 6789);
// output stream towards socket initialization
DataOutputStream outToServer =
new DataOutputStream(clientSocket.getOutputStream());
// input stream from socket initialization
BufferedReader inFromServer =
new BufferedReader(
new InputStreamReader(clientSocket.getInputStream()));
// read a line from the user
sentence = inFromUser.readLine();
// send the line to the server
outToServer.writeBytes(sentence + '\n');
// read the response from the server
modifiedSentence = inFromServer.readLine();
System.out.println("FROM SERVER: " + modifiedSentence);
clientSocket.close();
}
}
package iterative;
import java.io.*;
import java.net.*;
class IterativeServer {
public static void main(String argv[]) throws Exception {
String clientSentence;
String capitalizedSentence;
// create a "listening socket" on the specified port
ServerSocket welcomeSocket = new ServerSocket(6789);
while(true) {
/* accept is a blocking call
once a new connection arrived, it creates
a new "established socket" */
Socket connectionSocket = welcomeSocket.accept();
// input stream from the socket initialization
BufferedReader inFromClient =
new BufferedReader(
new InputStreamReader(connectionSocket.getInputStream()));
// output stream to the socket initialization
DataOutputStream outToClient =
new DataOutputStream(connectionSocket.getOutputStream());
// read a line (that terminates with \n) from the client
clientSentence = inFromClient.readLine();
// wait for 10 seconds
//Thread.sleep(10000);
capitalizedSentence = clientSentence.toUpperCase() + '\n';
// send the response to the client
outToClient.writeBytes(capitalizedSentence);
}
}
}
package multithread;
import java.io.*;
import java.net.*;
class MultiServer {
public static void main(String argv[]) throws Exception {
ServerSocket welcomeSocket = new ServerSocket(6789);
while(true) {
Socket connectionSocket = welcomeSocket.accept();
// thread creation passing the established socket as arg
ServerThread theThread =
new ServerThread(connectionSocket);
// start of the thread
theThread.start();
}
}
}
package multithread;
import java.io.*;
import java.net.*;
public class ServerThread extends Thread {
private Socket connectionSocket = null;
private BufferedReader inFromClient;
private DataOutputStream outToClient;
// the constructor argument is an established socket
public ServerThread(Socket s) {
connectionSocket = s;
try {
inFromClient =
new BufferedReader(
new InputStreamReader(connectionSocket.getInputStream()));
outToClient =
new DataOutputStream(connectionSocket.getOutputStream());
} catch (IOException e) {
e.printStackTrace();
}
}
public void run() {
String clientSentence;
String capitalizedSentence;
try {
clientSentence = inFromClient.readLine();
capitalizedSentence = clientSentence.toUpperCase() + '\n';
outToClient.writeBytes(capitalizedSentence);
connectionSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment