Networking in Java
Java provides built-in classes for networking that allow you to create network applications, such as web servers and clients. You can use these classes to establish connections, send and receive data, and manage network communication.
Example code:
try {
Socket socket = new Socket("localhost", 8080);
OutputStream output = socket.getOutputStream();
output.write("Hello, server!".getBytes());
InputStream input = socket.getInputStream();
byte[] buffer = new byte[1024];
int length = input.read(buffer);
String response = new String(buffer, 0, length);
System.out.println(response);
socket.close();
} catch (IOException e) {
System.out.println("An error occurred: " + e.getMessage());
}
No comments:
Post a Comment