tanszek:oktatas:tcp_socket_connection

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
tanszek:oktatas:tcp_socket_connection [2023/02/26 15:28] kneheztanszek:oktatas:tcp_socket_connection [2023/02/27 09:16] (current) – [Exercise 1.] knehez
Line 1: Line 1:
-====== Összetett feladat ======+====== Socket client and server exercises ======
  
-Készítsen egy egyszerűsített FTP (file transport) klienst és szervert, amelynél a kliens elküldhet vagy letölthet szöveges file-okat a szerverről. +==== Exercise 1====
-Általános funkció leírás: +
-  -) Kliens becsatlakozik a szerverhez és küld egy listázás üzenetet +
-  -) Szerver visszaküldi a tárolt file-ok listáját (vagy előzőleg feltöltött file-ok listáját) +
-  -) Kliens kilistázza a fileokat, és bekéri a felhasználótól, hogy milyen műveletet szeretne végezni? Feltöltés vagy letöltés? ('u' vagy 'd')  +
-  -) Mindkét esetben be kell írni a file nevét kiterjesztéssel együtt +
-  -) A kliens elküldi a szerverre a kiválasztott file-t, vagy letölti a kiválasztott file-t egy adott könyvtárba.+
  
 +Create a simplified FTP (file transport) client and server where the client can send or download text files from the server:
  
-**Szerver nézőpont:** +== General use-cases == 
-  -) Becsatlakozás után felolvassa a file-okat a /store alkönyvtárból és a listázás üzenet megérkezése után a fájlneveket elküldi a kliensnek.  +  -) Client connects to the server and sends 'file listing' message 
-  -) Várakozunk a kliens 'u' vagy 'd' műveletére +  -) Server sends back the list of the downloadable files 
-  -) Klienstől kapunk egy filenevet és ha 'd' (download) a művelet, akkor felolvassuk a file-t és visszaküldjük a tartalmát +  -) Client lists the files and asks the user what action they want to take? Upload or download? ('u' or 'd' 
-  -) Ha a művelet 'u' (feltöltés), akkor nyitunk egy új file-t megadott néven és várjuk az adatokat, amiket kiírunk a file-ba.+  -) In both cases users must give the full file name with extension 
 +  -) The client sends the selected file to the server (uploador downloads the selected file from the server to specific directory.
  
  
-**Kliens nézőpont** +== Server viewpoint == 
-  -) A kliens becsatlakozik és várja a visszajövő fájlok listájátmajd ha megjön akkor kiírjuk a konzolra +  -) After connectingit reads the files from the /store subdirectory and sends the file names to the client after receiving the listing message.  
-  -) Bekérjük a "u" vagy "d" billentyűt +  -) We are waiting for the client's 'u' or 'd' operation  
-  -) Majd kérjük file-nevet is+  -) We get filename from the client and if the action is 'd' (download), we read the file content and return its contents 
-  -a kliens a /files könvytárából olvassa a file-okatvagy a letöltött file-t is ide hozza létre +  -) If the operation is 'u' (upload), we open new file with the specified name and wait for the data to be written to the file.
-  -) "d" billentyű esetén létrehozza a /files/<filename> állományt és a szerverről jövő adatokat beleírja +
-  -"u" billentyű esetén /files/<filename> állományt elküldi a szervernek+
  
-====== Alappéldák ======+== Client viewpoint ==
  
 +  -) The client connects and waits for the list of files coming back and writes it to the console 
 +  -) We ask for the "u" or "d" key 
 +  -) 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 "d", it creates /files/ and writes data from the server
 +  -) If you press "u", /files/ is sent to the server
  
 +==== TCP style ====
  
  
Line 33: Line 34:
  
  
-==== 1.) Hagyományos blokkolt TCP alapú socket szerver ==== 
  
-=== Socket szerver kód ===+ 
 + 
 +==== 1.) Traditional blocked TCP based socket server class in Java ==== 
 + 
 +=== Socket server source code ===
 <code java> <code java>
     import java.io.IOException;     import java.io.IOException;
Line 55: Line 59:
     void run() {     void run() {
     try {     try {
-    // 1. szerver socket létrehozása+    // 1. create a socket server listening to port 8080
     providerSocket = new ServerSocket(8080, 10);     providerSocket = new ServerSocket(8080, 10);
-    // 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 streamek megadása+    // 3. create Input and Output streams
     out = new ObjectOutputStream(connection.getOutputStream());     out = new ObjectOutputStream(connection.getOutputStream());
     in = new ObjectInputStream(connection.getInputStream());     in = new ObjectInputStream(connection.getInputStream());
-    // 4. socket kommunikáció+    // 4. socket communication
     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:
     }     }
 </code> </code>
-=== Socket kliens kód ===+=== Socket client source ===
 <code java> <code java>
     import java.io.IOException;     import java.io.IOException;
Line 125: Line 129:
     void run() {     void run() {
     try {     try {
-    // 1. socket kapcsolat létrehozása+    // 1. try to connect to the socket: localhost:8080
     requestSocket = new Socket("localhost", 8080);     requestSocket = new Socket("localhost", 8080);
-    // 2. Input and Output streamek+    // 2. Input and Output streams
     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("Hello szerver");+    sendMessage("Hello server");
     sendMessage("bye");     sendMessage("bye");
     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:
     }     }
 </code> </code>
 +==== 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 2. ==  
- +Modify the code so that you can transfer burned-in name and existing text or image file larger than kbytes and verify that it was successfully sent.
- +
- +
- +
-==== 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**: módosítsuk a kódot, hogy át tudjon küldeni egy beégetett nevű, és létező, kbyte-nál nagyobb szöveges vagy kép állományt és ellenőrizzük a sikeres küldést.+
  
 <code java> <code java>
Line 213: Line 211:
  String modifiedSentence = new String(receivePacket.getData());  String modifiedSentence = new String(receivePacket.getData());
   
- System.out.println("átalakítva:" + modifiedSentence);+ System.out.println("converted:" + modifiedSentence);
  clientSocket.close();  clientSocket.close();
  }  }
Line 219: Line 217:
 </code> </code>
  
-2.b) Az UDP szerver a 8080-as porton várja az ágensek üzeneteit és nagybetűre konvertálva visszaküldi a kliens UDP socketre.+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 UDP socket.
  
 <code java> <code java>
Line 238: Line 236:
   
  DatagramPacket receivePacket = new DatagramPacket(bytesReceived, bytesReceived.length);  DatagramPacket receivePacket = new DatagramPacket(bytesReceived, bytesReceived.length);
- // 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 szoveg = new String(receivePacket.getData());+ String textMessage = new String(receivePacket.getData());
   
- System.out.println("kaptam: " + szoveg);+ System.out.println("I got: " + textMessage);
   
  InetAddress IPAddress = receivePacket.getAddress();  InetAddress IPAddress = receivePacket.getAddress();
  int port = receivePacket.getPort();  int port = receivePacket.getPort();
   
- String nagybetűsSzöveg szoveg.toUpperCase(); + String upperCaseText textMessage.toUpperCase(); 
- bytesSent = nagybetűsSzöveg.getBytes();+ bytesSent = upperCaseText.getBytes();
   
- // visszaküldi+ // send back
  DatagramPacket sendPacket = new DatagramPacket(bytesSent, bytesSent.length, IPAddress, port);  DatagramPacket sendPacket = new DatagramPacket(bytesSent, bytesSent.length, IPAddress, port);
  serverSocket.send(sendPacket);  serverSocket.send(sendPacket);
tanszek/oktatas/tcp_socket_connection.1677425305.txt.gz · Last modified: 2023/02/26 15:28 by knehez