Answer the question
In order to leave comments, you need to log in
How to pass int to server?
Hello.
There is an implemented client-server application.
Reader and Writer are implemented through BufferedReader and BufferedWriter respectively.
The problem is that Buffered reads byte by byte. I can not implement in out so that it would be possible to read, for example, int, float.
Please help.
Sends only the first digit
import java.io.*;
public class Client {
public static void main(String[] args) {
try (Manager manager = new Manager("127.0.0.1", 8000)) {
System.out.println("Connected to server");
int a = manager.readInt();
int b = manager.readInt();
int n = manager.readInt();
System.out.println(a + "," + b + "," + n);
while (manager.readLine() != "close") {
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
import java.io.*;
import java.net.ServerSocket;
import java.util.Scanner;
public class Server {
public static void main(String[] args) throws IOException {
try (ServerSocket server = new ServerSocket(8000);) {
Scanner in = new Scanner(System.in);
System.out.println("Server start");
while (true) {
Manager manager = new Manager(server);
new Thread(new Runnable() {
@Override
public void run() {
System.out.print("Input a A B Step: ");
int a = in.nextInt();
int b = in.nextInt();
int n = in.nextInt();
manager.writeInt(a);
manager.writeInt(b);
manager.writeInt(n);
System.out.println(a + "," + b + "," + n);
try {
manager.close();
} catch (IOException e) {
}
}
}).start();
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.Closeable;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
public class Manager implements Closeable {
private final Socket socket;
private final BufferedReader reader;
private final BufferedWriter writer;
public Manager(String ip, int port) {
try {
this.socket = new Socket(ip, port);
this.reader = createReader();
this.writer = createWriter();
}
catch (IOException e){throw new RuntimeException(e);}
}
public Manager(ServerSocket server) {
try {
this.socket = server.accept();
this.reader = createReader();
this.writer = createWriter();
}
catch (IOException e){throw new RuntimeException(e);}
}
public void writeLine(String message) {
try {
writer.write(message);
writer.newLine();
writer.flush();
}
catch (IOException e) {throw new RuntimeException(e);}
}
public String readLine() {
try {return reader.readLine();}
catch (IOException e) {throw new RuntimeException(e);}
}
public void writeInt(int num) {
try {
writer.write(num);
writer.newLine();
writer.flush();
}
catch (IOException e) {throw new RuntimeException(e);}
}
public int readInt() {
try {return reader.read();}
catch (IOException e) {throw new RuntimeException(e);}
}
private BufferedReader createReader() throws IOException {
return new BufferedReader(new InputStreamReader(socket.getInputStream()));
}
private BufferedWriter createWriter() throws IOException {
return new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
}
@Override
public void close() throws IOException {
socket.getInputStream().close();
reader.close();
writer.close();
}
}
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