Junior — Middle
What is your practical experience working with various network protocols and their applications?
sobes.tech AI
Answer from AI
In my work with Java, I have used several network protocols such as TCP and UDP for data exchange between client and server. For example, TCP was used to create a reliable connection in a chat application, where message order and integrity are important. UDP was used for real-time data transmission, where speed is crucial and losing some packets is not critical (e.g., streaming audio or video).
I also worked with the HTTP protocol when creating REST APIs using libraries like HttpURLConnection or Apache HttpClient. This allowed me to send requests to web services and handle responses.
Here's an example of a simple TCP server in Java:
ServerSocket serverSocket = new ServerSocket(12345);
Socket clientSocket = serverSocket.accept();
BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
String inputLine;
while ((inputLine = in.readLine()) != null) {
out.println("Echo: " + inputLine);
}
clientSocket.close();
serverSocket.close();