tanszek:oktatas:tcp_socket_connection
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
tanszek:oktatas:tcp_socket_connection [2023/02/26 15:38] – [Összetett feladat] knehez | tanszek:oktatas:tcp_socket_connection [2023/02/27 09:16] (current) – [Exercise 1.] knehez | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== | + | ====== |
+ | |||
+ | ==== Exercise 1. ==== | ||
Create a simplified FTP (file transport) client and server where the client can send or download text files from the server: | Create a simplified FTP (file transport) client and server where the client can send or download text files from the server: | ||
- | General use-cases: | + | == General use-cases |
-) Client connects to the server and sends a 'file listing' | -) Client connects to the server and sends a 'file listing' | ||
-) Server sends back the list of the downloadable files | -) Server sends back the list of the downloadable files | ||
-) Client lists the files and asks the user what action they want to take? Upload or download? (' | -) Client lists the files and asks the user what action they want to take? Upload or download? (' | ||
- | -) In both cases the uses must give the full file name with extension | + | -) In both cases users must give the full file name with extension |
-) The client sends the selected file to the server (upload) or downloads the selected file from the server to a specific directory. | -) The client sends the selected file to the server (upload) or downloads the selected file from the server to a specific directory. | ||
Line 17: | Line 19: | ||
-) If the operation is ' | -) If the operation is ' | ||
- | **Kliens nézőpont** | + | == Client viewpoint == |
- | -) A kliens becsatlakozik és várja a visszajövő fájlok listáját, majd ha megjön akkor kiírjuk a konzolra | + | |
- | -) Bekérjük a " | + | |
- | -) Majd kérjük a file-nevet is. | + | |
- | -) a kliens a /files könvytárából olvassa a file-okat, vagy a letöltött file-t is ide hozza létre | + | |
- | -) " | + | |
- | -) " | + | |
- | ====== Alappéldák ====== | + | -) The client connects and waits for the list of files coming back and writes it to the console |
+ | -) We ask for the " | ||
+ | -) Then we'll ask for the file-name as well. | ||
+ | -) The client reads the files from the /files folder, or creates the downloaded file here | ||
+ | -) If you press " | ||
+ | -) If you press " | ||
+ | ==== TCP style ==== | ||
Line 33: | Line 35: | ||
- | ==== 1.) Hagyományos blokkolt TCP alapú socket szerver ==== | ||
- | === Socket | + | |
+ | ==== 1.) Traditional blocked TCP based socket server class in Java ==== | ||
+ | |||
+ | === Socket | ||
<code java> | <code java> | ||
import java.io.IOException; | import java.io.IOException; | ||
Line 55: | Line 59: | ||
void run() { | void run() { | ||
try { | try { | ||
- | // 1. szerver | + | // 1. create a socket |
providerSocket = new ServerSocket(8080, | providerSocket = new ServerSocket(8080, | ||
- | // 2. kapcsolódásra várakozás | + | // 2. waiting for the connection (here we are waiting until next connection) |
connection = providerSocket.accept(); | connection = providerSocket.accept(); | ||
- | // 3. Input és Output | + | // 3. create |
out = new ObjectOutputStream(connection.getOutputStream()); | out = new ObjectOutputStream(connection.getOutputStream()); | ||
in = new ObjectInputStream(connection.getInputStream()); | in = new ObjectInputStream(connection.getInputStream()); | ||
- | // 4. socket | + | // 4. socket |
do { | do { | ||
try { | try { | ||
Line 77: | Line 81: | ||
ioException.printStackTrace(); | ioException.printStackTrace(); | ||
} finally { | } finally { | ||
- | // 4: kapcsolat lezárása | + | // 4: close connection |
try { | try { | ||
in.close(); | in.close(); | ||
Line 106: | Line 110: | ||
} | } | ||
</ | </ | ||
- | === Socket | + | === Socket |
<code java> | <code java> | ||
import java.io.IOException; | import java.io.IOException; | ||
Line 125: | Line 129: | ||
void run() { | void run() { | ||
try { | try { | ||
- | // 1. socket | + | // 1. try to connect to the socket: localhost: |
requestSocket = new Socket(" | requestSocket = new Socket(" | ||
- | // 2. Input and Output | + | // 2. Input and Output |
out = new ObjectOutputStream(requestSocket.getOutputStream()); | out = new ObjectOutputStream(requestSocket.getOutputStream()); | ||
in = new ObjectInputStream(requestSocket.getInputStream()); | in = new ObjectInputStream(requestSocket.getInputStream()); | ||
- | // 3: Kommunikáció | + | // 3: communications |
do { | do { | ||
try { | try { | ||
- | sendMessage(" | + | sendMessage(" |
sendMessage(" | sendMessage(" | ||
message = (String) in.readObject(); | message = (String) in.readObject(); | ||
Line 145: | Line 149: | ||
ioException.printStackTrace(); | ioException.printStackTrace(); | ||
} finally { | } finally { | ||
- | // 4: Kapcsolat zárása | + | // 4: close connection |
try { | try { | ||
in.close(); | in.close(); | ||
Line 172: | Line 176: | ||
} | } | ||
</ | </ | ||
+ | ==== Traditional UDP style ==== | ||
+ | The following Agent sends a message and waits for a response on port 8080, also with UDP. In the Eclipse IDE, the text you type on the console can be sent by pressing ctrl+z | ||
- | + | == Exercise | |
- | + | Modify the code so that you can transfer | |
- | + | ||
- | + | ||
- | + | ||
- | ==== 2.) Hagyományos UDP alapú kommunikáció ==== | + | |
- | + | ||
- | 2.a) Az alábbi Ágens küld egy üzenetet és a 8080-as porton várja a választ rá, ugyancsak UDP-vel. Az eclipse fejlesztőkörnyezetben a consolon beírt szöveget ctrl+z leütésével lehet elküldeni. | + | |
- | + | ||
- | **Feladat**: | + | |
<code java> | <code java> | ||
Line 213: | Line 211: | ||
String modifiedSentence = new String(receivePacket.getData()); | String modifiedSentence = new String(receivePacket.getData()); | ||
- | System.out.println(" | + | System.out.println(" |
clientSocket.close(); | clientSocket.close(); | ||
} | } | ||
Line 219: | Line 217: | ||
</ | </ | ||
- | 2.b) Az UDP szerver a 8080-as porton várja az ágensek üzeneteit és nagybetűre konvertálva visszaküldi a kliens | + | 2.b) The UDP server waits for the agents messages on port 8080 and converts them to uppercase letters and sends them back to the client |
<code java> | <code java> | ||
Line 238: | Line 236: | ||
DatagramPacket receivePacket = new DatagramPacket(bytesReceived, | DatagramPacket receivePacket = new DatagramPacket(bytesReceived, | ||
- | // itt várakozik ameddig adat jön a 8080-as porton | + | // here we are waiting for the packets |
serverSocket.receive(receivePacket); | serverSocket.receive(receivePacket); | ||
- | String | + | String |
- | System.out.println(" | + | System.out.println(" |
InetAddress IPAddress = receivePacket.getAddress(); | InetAddress IPAddress = receivePacket.getAddress(); | ||
int port = receivePacket.getPort(); | int port = receivePacket.getPort(); | ||
- | String | + | String |
- | bytesSent = nagybetűsSzöveg.getBytes(); | + | bytesSent = upperCaseText.getBytes(); |
- | // visszaküldi | + | // send back |
DatagramPacket sendPacket = new DatagramPacket(bytesSent, | DatagramPacket sendPacket = new DatagramPacket(bytesSent, | ||
serverSocket.send(sendPacket); | serverSocket.send(sendPacket); |
tanszek/oktatas/tcp_socket_connection.1677425908.txt.gz · Last modified: 2023/02/26 15:38 by knehez