Answer the question
In order to leave comments, you need to log in
How can I use Java.net.* to connect two computers on a network?
Good afternoon, ladies and gentlemen. I have two computers with Internet access. On the first, following the manuals, I write the following code:
import java.net.*;
import java.io.*;
public class Lied {
public static void main (String args[]) throws IOException{
try(ServerSocket serverSocket = new ServerSocket(11111)){
while(true){
try (Socket socket = serverSocket.accept()){
serveClient(socket);
}
}
}
}
private static void serveClient(Socket socket) throws IOException {
InputStream inputStream = socket.getInputStream();
OutputStream outputStream = socket.getOutputStream();
while(true){
int request = inputStream.read();
if (request == -1) {
break;
}
outputStream.write(request*request);
System.out.println("Получили число: " + request);
outputStream.flush();
}
}
}
import java.net.*;
import java.io.*;
public class inet{
public static void main(String args[]) throws Exception{
try(Socket socket = new Socket("93-81-119-229.broadband.corbina.ru",11111)){
OutputStream outputStream = socket.getOutputStream();
outputStream.write(5);
outputStream.flush();
InputStream inputStream = socket.getInputStream();
int response = inputStream.read();
System.out.println(response);
}
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question