tanszek:oktatas:tcp_socket_connection
Differences
This shows you the differences between two versions of the page.
| Next revision | Previous revision | ||
| tanszek:oktatas:tcp_socket_connection [2015/03/15 21:26] – külső szerkesztés 127.0.0.1 | tanszek:oktatas:tcp_socket_connection [2023/02/27 09:16] (current) – [Exercise 1.] knehez | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| - | ==== 1.) Traditional socket based TCP server/client | + | ====== Socket |
| + | ==== Exercise 1. ==== | ||
| - | 1/a) Socket | + | Create |
| - | | + | == General use-cases == |
| - | import java.net.*; | + | -) Client connects to the server and sends a 'file listing' |
| + | -) 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? (' | ||
| + | -) 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. | ||
| + | |||
| + | |||
| + | == Server viewpoint == | ||
| + | -) After connecting, it reads the files from the /store subdirectory and sends the file names to the client after receiving the listing message. | ||
| + | -) We are waiting for the client' | ||
| + | -) We get a filename from the client and if the action is ' | ||
| + | -) If the operation is ' | ||
| + | |||
| + | == Client viewpoint == | ||
| + | |||
| + | -) 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 ==== | ||
| + | |||
| + | |||
| + | |||
| + | |||
| + | |||
| + | |||
| + | |||
| + | |||
| + | ==== 1.) Traditional blocked TCP based socket server class in Java ==== | ||
| + | |||
| + | === Socket server source code === | ||
| + | <code java> | ||
| + | | ||
| + | | ||
| + | import java.io.ObjectOutputStream; | ||
| + | import java.net.ServerSocket; | ||
| + | | ||
| | | ||
| - | | + | |
| - | ServerSocket providerSocket; | + | ServerSocket providerSocket; |
| - | Socket connection = null; | + | Socket connection = null; |
| - | ObjectOutputStream out; | + | ObjectOutputStream out; |
| - | ObjectInputStream in; | + | ObjectInputStream in; |
| - | String message; | + | String message; |
| - | | + | |
| - | void run() { | + | Server() { |
| - | try{ | + | } |
| - | providerSocket = new ServerSocket(8080, | + | |
| - | connection = providerSocket.accept(); | + | void run() { |
| - | out = new ObjectOutputStream(connection.getOutputStream()); | + | try { |
| - | in = new ObjectInputStream(connection.getInputStream()); | + | // 1. create a socket server listening to port 8080 |
| - | + | providerSocket = new ServerSocket(8080, | |
| - | do { | + | // 2. waiting for the connection (here we are waiting until next connection) |
| - | try { | + | connection = providerSocket.accept(); |
| - | message = (String)in.readObject(); | + | // 3. create Input and Output streams |
| - | System.out.println(" | + | out = new ObjectOutputStream(connection.getOutputStream()); |
| - | if (message.equals(" | + | in = new ObjectInputStream(connection.getInputStream()); |
| - | } | + | // 4. socket communication |
| - | catch(ClassNotFoundException classnot){ | + | do { |
| - | System.err.println(" | + | try { |
| - | } | + | message = (String) in.readObject(); |
| - | } while(!message.equals(" | + | System.out.println(" |
| - | } | + | if (message.equals(" |
| - | catch(IOException ioException){ | + | |
| - | ioException.printStackTrace(); | + | } |
| - | } | + | |
| - | finally{ | + | System.err.println(" |
| - | try { | + | } |
| - | in.close(); | + | } while (!message.equals(" |
| - | out.close(); | + | } catch (IOException ioException) { |
| - | providerSocket.close(); | + | ioException.printStackTrace(); |
| - | } | + | } finally { |
| - | catch(IOException ioException){ | + | // 4: close connection |
| - | ioException.printStackTrace(); | + | try { |
| - | } | + | in.close(); |
| - | } | + | out.close(); |
| - | } | + | providerSocket.close(); |
| - | + | } catch (IOException ioException) { | |
| - | void sendMessage(String msg) { | + | ioException.printStackTrace(); |
| - | try { | + | } |
| - | out.writeObject(msg); | + | } |
| - | out.flush(); | + | } |
| - | System.out.println(" | + | |
| - | } | + | void sendMessage(String msg) { |
| - | catch(IOException ioException){ | + | try { |
| - | ioException.printStackTrace(); | + | out.writeObject(msg); |
| - | } | + | out.flush(); |
| - | } | + | System.out.println(" |
| - | + | } catch (IOException ioException) { | |
| - | public static void main(String args[]) { | + | ioException.printStackTrace(); |
| - | Provider | + | } |
| - | while(true){ | + | } |
| - | server.run(); | + | |
| - | } | + | public static void main(String args[]) { |
| - | } | + | |
| - | } | + | while (true) { |
| + | server.run(); | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | === Socket client source === | ||
| + | <code java> | ||
| + | import java.io.IOException; | ||
| + | import java.io.ObjectInputStream; | ||
| + | import java.io.ObjectOutputStream; | ||
| + | import java.net.Socket; | ||
| + | import java.net.UnknownHostException; | ||
| + | |||
| + | public class Client { | ||
| + | Socket requestSocket; | ||
| + | ObjectOutputStream out; | ||
| + | ObjectInputStream in; | ||
| + | String message; | ||
| + | |||
| + | Client() { | ||
| + | } | ||
| + | |||
| + | void run() { | ||
| + | try { | ||
| + | // 1. try to connect to the socket: localhost: | ||
| + | requestSocket = new Socket(" | ||
| + | // 2. Input and Output streams | ||
| + | out = new ObjectOutputStream(requestSocket.getOutputStream()); | ||
| + | in = new ObjectInputStream(requestSocket.getInputStream()); | ||
| + | // 3: communications | ||
| + | do { | ||
| + | try { | ||
| + | sendMessage(" | ||
| + | sendMessage(" | ||
| + | message = (String) in.readObject(); | ||
| + | } catch (Exception e) { | ||
| + | System.err.println(" | ||
| + | } | ||
| + | } while (!message.equals(" | ||
| + | } catch (UnknownHostException unknownHost) { | ||
| + | System.err.println(" | ||
| + | } catch (IOException ioException) { | ||
| + | ioException.printStackTrace(); | ||
| + | } finally { | ||
| + | // 4: close connection | ||
| + | try { | ||
| + | in.close(); | ||
| + | out.close(); | ||
| + | requestSocket.close(); | ||
| + | } catch (IOException ioException) { | ||
| + | ioException.printStackTrace(); | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | |||
| + | void sendMessage(String msg) { | ||
| + | try { | ||
| + | out.writeObject(msg); | ||
| + | out.flush(); | ||
| + | System.out.println(" | ||
| + | } catch (IOException ioException) { | ||
| + | ioException.printStackTrace(); | ||
| + | } | ||
| + | } | ||
| + | |||
| + | public static void main(String args[]) { | ||
| + | Client client = new Client(); | ||
| + | client.run(); | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | ==== Traditional UDP style ==== | ||
| - | 1/b.) Client source | + | 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 a burned-in name and existing text or image file larger than 2 kbytes and verify that it was successfully sent. | ||
| - | | + | <code java> |
| - | import java.net.*; | + | package org.ait; |
| - | + | ||
| - | | + | |
| - | Socket requestSocket; | + | import java.io.InputStreamReader; |
| - | | + | |
| - | | + | |
| - | | + | |
| - | | + | |
| - | void run() { | + | |
| - | try{ | + | public static |
| - | requestSocket | + | BufferedReader inFromUser |
| - | out = new ObjectOutputStream(requestSocket.getOutputStream()); | + | DatagramSocket clientSocket |
| - | in = new ObjectInputStream(requestSocket.getInputStream()); | + | InetAddress IPAddress = InetAddress.getByName("localhost"); |
| - | do { | + | |
| - | try { | + | byte[] sendData = new byte[1024]; |
| - | sendMessage("Hello szerver"); | + | byte[] receiveData |
| - | sendMessage(" | + | |
| - | | + | String sentence = inFromUser.readLine(); |
| - | } | + | sendData = sentence.getBytes(); |
| - | catch(Exception e){ | + | |
| - | System.err.println("data received in unknown format" | + | DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, |
| - | } | + | clientSocket.send(sendPacket); |
| - | } while(!message.equals(" | + | |
| - | } | + | DatagramPacket receivePacket = new DatagramPacket(receiveData, |
| - | catch(UnknownHostException unknownHost){ | + | clientSocket.receive(receivePacket); |
| - | System.err.println("You are trying to connect to an unknown host!"); | + | String modifiedSentence = new String(receivePacket.getData()); |
| - | } | + | |
| - | catch(IOException ioException){ | + | System.out.println(" |
| - | ioException.printStackTrace(); | + | clientSocket.close(); |
| - | } | + | |
| - | finally{ | + | |
| - | try{ | + | |
| - | in.close(); | + | |
| - | out.close(); | + | |
| - | requestSocket.close(); | + | |
| - | } | + | |
| - | catch(IOException ioException){ | + | |
| - | ioException.printStackTrace(); | + | |
| - | } | + | |
| - | } | + | |
| } | } | ||
| - | | ||
| - | void sendMessage(String msg) { | ||
| - | try { | ||
| - | out.writeObject(msg); | ||
| - | out.flush(); | ||
| - | System.out.println(" | ||
| - | } | ||
| - | catch(IOException ioException){ | ||
| - | ioException.printStackTrace(); | ||
| - | } | ||
| } | } | ||
| - | + | </ | |
| - | public static void main(String args[]) { | + | |
| - | Requester client | + | 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. |
| - | client.run(); | + | |
| + | <code java> | ||
| + | package org.ait; | ||
| + | |||
| + | import java.net.DatagramPacket; | ||
| + | import java.net.DatagramSocket; | ||
| + | import java.net.InetAddress; | ||
| + | |||
| + | public class UDPServer { | ||
| + | public static void main(String args[]) | ||
| + | |||
| + | DatagramSocket serverSocket | ||
| + | |||
| + | byte[] bytesReceived = new byte[1024]; | ||
| + | byte[] bytesSent = new byte[1024]; | ||
| + | |||
| + | |||
| + | DatagramPacket receivePacket = new DatagramPacket(bytesReceived, | ||
| + | // here we are waiting for the packets | ||
| + | serverSocket.receive(receivePacket); | ||
| + | |||
| + | String textMessage = new String(receivePacket.getData()); | ||
| + | |||
| + | System.out.println(" | ||
| + | |||
| + | InetAddress IPAddress = receivePacket.getAddress(); | ||
| + | int port = receivePacket.getPort(); | ||
| + | |||
| + | String upperCaseText = textMessage.toUpperCase(); | ||
| + | bytesSent = upperCaseText.getBytes(); | ||
| + | |||
| + | // send back | ||
| + | DatagramPacket sendPacket = new DatagramPacket(bytesSent, | ||
| + | serverSocket.send(sendPacket); | ||
| + | serverSocket.close(); | ||
| + | |||
| } | } | ||
| } | } | ||
| + | </ | ||
tanszek/oktatas/tcp_socket_connection.1426454806.txt.gz · Last modified: 2015/03/15 21:26 by 127.0.0.1
