how send multiple answer from client to server without close BufferedReader [duplicate] - java

This question already has answers here:
Java multiple file transfer over socket
(3 answers)
Closed 7 years ago.
First i'm sorry for my english. :-) It's my first post here.
I have application, something like torrent. I run it in one computer but 2 or more instance. i must ask user what file he want, and send this file to client. if i want its be host-to-host or multi-host.
and my problem is: when i send to client list file in directory , he choose one of them and send to server nameFile. then server send this file to client. But when i send listFile i must close bufferedreader because readline() is blocking. but if i close that i don't have connection. Any idea?
please for any proposition. this is my code:
Server:
public class Server extends Thread {
private Socket s;
int numerKlienta;
String line;
List<String> list = new ArrayList();
private ServerSocket serverSocket;
String nazwaPliku = "";
PrintWriter outToClient;
String text = "";
String tmp = "";
public Server(int port) throws Exception {
serverSocket = new ServerSocket(port);
while (true) {
Socket clientSocket = serverSocket.accept();
BufferedReader inFromClient = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
outToClient = new PrintWriter(clientSocket.getOutputStream(), true);
String path = "C:\\Users\\Ania\\Desktop";
File directory = new File(path);
File[] files = directory.listFiles();
for (int j = 0; j < files.length; j++) {
if (files[j].isFile()) {
text = files[j].getName();
outToClient.println(text);
}
}
//outToClient.flush();
outToClient.close(); //i must close beacuse in client when i writeBytes its blocking next steps
nazwaPliku = inFromClient.readLine();
System.out.println(nazwaPliku);
outToClient.close();
}
}
}
Client:
public class Client {
public Client(String host, int port) throws Exception{
s = new Socket(host, port);
DataOutputStream outToServer= new DataOutputStream(s.getOutputStream());
BufferedReader inFromServer =
new BufferedReader(new
InputStreamReader(s.getInputStream()));
System.out.println("lista plików w katalogu : ");
while ((( odpowiedz = inFromServer.readLine()) != null)){
System.out.println( odpowiedz);
}
//here is blocking and stop
System.out.println(" Jaki plik chcesz przesłać? podaj pełną nazwę");
Scanner sc= new Scanner(System.in);
String nazwaPliku=sc.next();
outToServer.writeBytes(nazwaPliku);
//saveFile(nazwaPliku);
}
And my Main:
public class Main {
public static void main(String[] args) throws Exception {
Server serwer=null;
System.out.println("Czy czy chcesz rozpocząc pracę jako serwer? (t/n)");
Scanner sc = new Scanner(System.in);
String odpowiedz = sc.next();
if (odpowiedz.equals("t")) {
System.out.println(" Na jakim porcie rozpocząc nasłuch?");
sc = new Scanner(System.in);
int portSerwera = sc.nextInt();
serwer = new Server(portSerwera);
//serwer.start();
}
else{
System.out.println("Czy chcesz rozpocząc połączenie z jakimś serwerem? (t/n)");
sc = new Scanner(System.in);
odpowiedz = sc.next();
if (odpowiedz.equals("t")) {
System.out.println("podaj numer portu do połączenia z serwerem");
sc = new Scanner(System.in);
int portKlienta = sc.nextInt();
Client fc = new Client("localhost", portKlienta);

You need to design a protocol with structured messages, instead of just sending lines. The client must know when a message ends.
For example you could decide to end your list of files by sending an empty line (1).
When the client reads the files, when it received the empty line, it knows that the list is terminated, and that it's up to the client to now send its choice.
Using separators is one way of doing. Another way can be to send "packets", where each packet starts with a number of bytes or characters to expect in the packet. The client thus knows that it must read N bytes to read the entire packet, and that once the packet is read, it should send its choice, and then read another packet (2).
(1) and (2): note that these two strategies are used by the protocol you probably use the most: HTTP. The header of an HTTP response ends with an empty line. And it usually contains the content length of the body of the response.

Related

Accessing byte array values from socket inputStream

I am attempting to retrieve the byte values from an InputStream which is being sent to the socket. I have used many ways but it always prints me the address of the byte array instead of its contents.
Below is my code for Client and Server. When a packet is sent from the client to the server, the server instantiates a new Thread to handle the connection. So slaveSocket is the socket I want to use for this.
public class TCPClient {
public static void main(String[] args) throws IOException{
Socket socket;
String address;
int port;
String userInput;
String serverResponse;
PrintWriter out;
BufferedReader in;
//read characters from user
BufferedReader stdIn;
if (args.length != 2) {
System.err.println("Usage: java EchoClient <address> <port>");
System.exit(1);
}
byte[] mode = "octet".getBytes(Charset.forName("UTF-8"));
address = args[0];
port = Integer.parseInt(args[1]);
try{
//connect socket to server
socket = new Socket(address, port);
//Construct PrintWriter to write objects to the socket
out = new PrintWriter(socket.getOutputStream(), true);
//Construct BufferedReader to read input from the socket
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
//Another reader to read characters typed by the user
stdIn = new BufferedReader(new InputStreamReader(System.in));
//scanner for menu option
Scanner scanner = new Scanner(System.in);
int menuOption;
System.out.println("Press 1 to read from file or 2 to write to file");
menuOption = scanner.nextInt();
if (menuOption == 1){
String filename = "";
String text = "";
System.out.println("Enter file name");
filename = scanner.next();
byte[] packet = new byte[512];
//Constructing the RRQ Packet
//Ading the OPCODE
packet[0] = 1;
//adding the filename
filename.getBytes(Charset.forName("UTF-8"));
byte[] filenameB = filename.getBytes(Charset.forName("UTF-8"));
System.arraycopy(filenameB,0,packet,1, filenameB.length);
//adding a 0
packet[filenameB.length +1] = 0;
//adding the mode
System.arraycopy(mode,0,packet,1+filenameB.length+1,mode.length);
//adding the last 0
packet[1+filenameB.length+1+mode.length+1] = 0;
out.println(packet);
}else if(menuOption == 2){
}
socket.close();
}catch(UnknownHostException e){
System.err.println("Dont know about host" + address);
System.exit(1);
}catch(IOException e){
System.err.println("Couldnt get I/O for the connection to " + address);
System.exit(1);
}
}
}
public class TCPServer {
public static void main(String[] args) throws IOException{
//port of the server
int port = 10000;
//Socket objects
ServerSocket masterSocket;
Socket slaveSocket;
//instantiate the server socket
masterSocket = new ServerSocket(port);
System.out.println("Server Started");
boolean flag1 = true;
while(true){
slaveSocket = masterSocket.accept();
System.out.println("Accepted TCP connection from: " +
slaveSocket.getInetAddress() + ", " + slaveSocket.getPort() + "...");
System.out.println("Initialising new Thread...");
new TCPServerThread(slaveSocket).start();
}
}
}
public class TCPServerThread extends Thread{
private Socket slaveSocket = null;
public TCPServerThread(Socket socket){
super("TCPServerThread");
this.slaveSocket = socket;
}
public void run(){
byte[] ClientPacket = new byte[512];
PrintWriter socketOutput;
InputStream socketInput;
try{
//send packet to client
socketOutput = new PrintWriter((slaveSocket.getOutputStream()), true);
//read packet from client
socketInput = new DataInputStream(slaveSocket.getInputStream());
ClientPacket = socketInput.readAllBytes();
System.out.println(new String(ClientPacket, StandardCharsets.UTF_8));
}catch (IOException e){
System.err.println(e);
}
}
}
You've hopelessly overengineered this.
Writer and Reader do character input and output. InputStream and OutputStream do byte input and output.
You turn byte-based stuff (and in the end, network ports are byte based, not character based) into character based stuff in dangerous ways and then are attempting to read and write bytes into and out of the char-based things.
The solution is simple. Just stop doing that. You have byte-based stuff, there is absolutely no need to involve Reader and Writer.
A bunch of lines that cause problems:
out.println(packet);
PrintStreams are debug aids. You can't use them for any of this. For example, this line will print newlines (definitely not something you'd want in a byte based stream system!), and will print 'objects' - it does that by invoking the .toString() method, and the toString method of arrays are mostly useless. That explains why you see what you see. This is not how you send bytes. You cannot send bytes to a PrintStream (which is a confused mess, as it tries to let you send characters to a byte based system. As I said, you use it for debugging and nothing else. You should not be using it here at all).
new InputStreamReader(socket.getInputStream())
This is dangerous. You're turning a byte based system (InputStream) into a char-based one (Reader) and this always means somebody is making an explicit, 'out of band' (not based on the data in that stream) decision about charset encoding. In this case, as per the docs of InputStreamReader, you get the 'platform default'. Starting with JDK18, it's guaranteed to be UTF-8 fortunately, but before that, who knows what it is. You never want to call this constructor to avoid the confusion. new InputStreamReader(socket.getInputStream, StandardCharsets.UTF_8).
Mostly, though, don't make a reader in the first place. You have no interest whatsoever in reading streams of characters, you just want bytes.
If you have smallish strings and the information about where they 'end' is done 'out of band' (example: The size in bytes (not characters) is sent first, then X bytes that are the string, UTF_8 encoded), you can just read that in as bytes, and then make a string off of that, bypassing any need for Readers and Writers. Reader and Writer is useful only if the entire stream is all character based, or if you have huge strings (hundreds of megabytes) where their end can only be surmised by interpreting the data as characters first. (Mostly, those are horrible protocols that shouldn't be used).
//Construct PrintWriter to write objects to the socket
No, you can't write objects to sockets. Objects aren't bytes. You can write bytes to a socket; some objects will let themselves be turned into bytestreams but this is decidedly not a trivial job, and PrintWriter can't do it at all.
catch (IOException e) { System.err.println(e);
Most code has no reasonable route to 'deal' with them, but the solution to that is to throw them onwards. Not to catch the exception, print a note of despair, and just keep going on like nothing happened. Doing it right is also less code, so, win-win.
stdIn = new BufferedReader(new InputStreamReader(System.in));
//scanner for menu option
Scanner scanner = new Scanner(System.in);
You're making 2 different ways to read standard input. That makes no sense. Pick one.
I tried to fix it for you:
public class TCPClient {
public static void main(String[] args) throws Exception { // always throw Exception from `main`.
if (args.length != 2) {
System.err.println("Usage: java EchoClient <address> <port>");
System.exit(1);
return; // Always return after System.exit.
}
byte[] mode = "octet".getBytes(Charset.forName("UTF-8"));
String address = args[0];
int port = Integer.parseInt(args[1]);
Scanner scanner = new Scanner(System.in);
scanner.useDelimiter("\\R"); // split on newlines, not spaces. So much more logical.
// resources need to be safe-closed - use try-with!
try (var socket = new Socket(address, port);
var out = new BufferedOutputStream(socket.getOutputStream());
var in = socket.getInputStream()) {
System.out.println("Press 1 to read from file or 2 to write to file");
int menuOption = scanner.nextInt();
if (menuOption == 1) {
System.out.println("Enter file name");
String filename = scanner.next();
//Constructing the RRQ Packet
//Adding the OPCODE
out.write(1);
out.write(filename.getBytes(StandardCharsets.UTF_8));
out.write(0);
// The above is dangerous; NUL (0) is actually a valid char.
// A proper way to send strings is to send length in bytes
// first. I'll leave it to you to fix your protocol.
// If it can't be fixed, scan for `\0` chars and get rid of em.
//adding the mode
out.write(mode);
out.write(0);
}else if (menuOption == 2) {
}
}
}
Sending bytes one at a time can be slow (as it ends up sending an entire packet) but can also be useful - the data is just sent, instead of waiting perhaps for a long time for more data. In your case, you send it all in one go, so sending it all off very quickly is not a good idea. Hence, why the outputstream is wrapped in a BufferedOutputStream, which fixes that. You can always use flush() to force sending now, in case you want to keep the connection open (close(), naturally, also flushes).
It's fine if you want to use a byte[] packet instead, but it seems convoluted and unneccessary here. out.write(someByteArray), where out is an OutputStream of some sort, works fine. out.println(byteArray), where out is a Writer of some sort, or a PrintStream - doesn't work at all. (It would take the array, call toString() on it which isn't useful, then convert those bytes using some unknown charset and send that, and none of that is what you want).
You'll need to similarly eliminate PrintStream and the like from your server code.

BufferedReader give more null character in string while using with TCP Socket

My TCP Server is like this.
import java.net.*;
import java.io.*;
public class NetTCPServer {
public static void main(String[] args) throws Exception{
ServerSocket sock;
sock = new ServerSocket(1122);
if(sock == null)
System.out.println("Server binding failed.");
System.out.println("Server is Ready ..");
do{
System.out.println("Waiting for Next client.");
Socket clientSocket = sock.accept();
if(clientSocket!=null)
System.out.println("Clinet accepted. "+sock.getInetAddress().getHostAddress());
DataOutputStream out = new DataOutputStream(clientSocket.getOutputStream());
//DataInputStream in = new DataInputStream(clientSocket.getInputStream());
BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
String name;
String pass;
String line;
name = in.readLine();
pass = in.readLine();
for(int i=0;i<name.length();i++)
System.out.print(name.charAt(i)+","); //see more null char are receiving here
System.out.println("");
System.out.println(name +" "+ name.length()+" \n" + pass+" "+pass.length());
}while(true);
}
}
And respective TCP Client is as follows.
import java.net.*;
import java.io.*;
public class NetTCPClient {
public static void main(String[] args) throws Exception {
InetAddress addr = InetAddress.getByName("localhost");
Socket sock;
sock = new Socket(addr,1122);
if(sock == null)
System.out.println("Server Connection failed.");
System.out.println("Waiting for some data...");
DataInputStream input = new DataInputStream(sock.getInputStream());
DataOutputStream output = new DataOutputStream(sock.getOutputStream());
String uname="ram";
String pass="pass";
output.writeChars(uname+"\n");// \n is appended just make to readline of server get line
output.writeChars(pass+"\n");
}
}
When i compiled both and the server is started and there after client is run, i get following output.
Server is Ready ..
Waiting for Next client.
Clinet accepted. 0.0.0.0
,r,,a,,m,,
ram7 pass9
The null character after each character receive is somewhat strange to me. To make me unable to compare the string with something stored in server.
What is those null characters and where does they come from.
You write characters but read lines of bytes. That won't work. If you're going to write characters, you need to read characters with precisely the same encoding. If you're going to write bytes, you need to read bytes. Specify your protocol precisely at the byte level and follow the specification in both the client and the server.
See this question for more information.

Java Sockets - Server hangs after client sends its response

Just trying to get a handle on sockets. The server and client are running in two different programs.
They seem to be connecting fine to each other but the client will not properly send its output to the server. The server just hangs. Here's the code:
Server:
private ServerSocket serverSocket;
private Socket client;
public void run() throws Exception {
serverSocket = new ServerSocket(20005);
while(currentState == Game.State.NORMAL) {
client = serverSocket.accept();
PrintWriter out = new PrintWriter(client.getOutputStream(), true);
BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
String clientInput = in.readLine();
// Takes the client input string and does some simple game logic that returns a Gson object
Gson serverResponse = processInput(clientInput);
out.write(serverResponse.toString());
out.flush();
}
}
Client:
Socket clientSocket;
void run() throws Exception {
clientSocket = new Socket("192.168.0.24", 20005);
PrintWriter out;
BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
// Print the state of the game - returns false if state is win or lose.
while(printState()) {
out = new PrintWriter(clientSocket.getOutputStream(), true);
// This method just takes some input from the console
String clientInput = getInput();
out.write(clientInput);
out.flush();
String serverResponse = in.readLine();
updateState(serverResponse);
}
}
}
There is some underlying game logic that is happening but it's pretty minor and should be irrelevant. I imagine I am just misunderstanding something fundamental here.
Thanks all.
Make sure you send a newline character to match the in.readLine() statement in the Server.
out.write(clientInput + "\n");
The same applys when sending data from Server->Client.

How do I get the server to respond with multiple lines? (Java)

I would like to take text from a file and then send it to the server for it to capitalise before sending back to the client where it is printed out. How do I achieve this?
I can read one line and send that back to the client and I've managed to write multiple lines to the output stream (for the server to read) but I don't know what to do now..
I have a client that reads text from a file:
import java.io.*;
import java.net.*;
import java.util.Date;
public class Client
{
public static void main(String[] args)
{
try
{
// First create the input from keyboard
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Client Program");
// Next we need to find out the IP address and port number of the server
System.out.print("Enter IP Address of server: ");
String ip = input.readLine();
System.out.print("Enter port number of server: ");
String port_string = input.readLine();
// The port number needs to be an int, so convert the string to an int
int port = Integer.parseInt(port_string);
// Connect to the server
Socket sock = new Socket(ip, port);
// Create the incoming stream to read messages from
DataInputStream network = new DataInputStream(sock.getInputStream());
//Create the output stream to the client
DataOutputStream message = new DataOutputStream(sock.getOutputStream());
//Send message
//message.writeUTF("some text");
FileReader file = new FileReader("text.dat");
BufferedReader input_file = new BufferedReader(file);
// Loop until EOF
while (input_file.ready()){
// Read next line from the file
String line = input_file.readLine();
// Write line to server
message.writeUTF(line + "\n");
//System.out.println(line);
}
// Display our address
System.out.println("Address: " + sock.getInetAddress());
String line;
// Loop until the connection closes, reading from the network
while ((line = network.readUTF()) != null)
{
// Display the received message
System.out.println(line);
}
}
catch (IOException ioe)
{
// This is expected when the server closes the network connection
System.err.println("Error in I/O");
System.err.println(ioe.getMessage());
System.exit(-1);
}
}
}
And then a server that is supposed to take those strings and capitalise them:
import java.io.*;
import java.net.*;
import java.util.*;
public class Server
{
public static void main(String[] args)
{
try
{
// First create the input from the keyboard
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Server Program");
// Get the port to listen on
System.out.print("Enter port number to listen on: ");
String port_string = input.readLine();
// The port number needs to be an int, so convert the String to an int
int port = Integer.parseInt(port_string);
// Create a ServerSocket to listen on this address
ServerSocket server = new ServerSocket(port);
// Accept an incoming client connection on the server socket
Socket sock = server.accept();
// Create the output stream to the client
DataOutputStream network = new DataOutputStream(sock.getOutputStream());
//Create the incoming stream to read messages from
DataInputStream message = new DataInputStream(sock.getInputStream());
String newLine = inFromClient.readLine();
//Line to read
String line;
line = message.readUTF();
line = line.toUpperCase();
// Send message
network.writeUTF(newLine);
// Close sockets. This will cause the client to exit
sock.close();
server.close();
}
catch (IOException ioe)
{
System.err.println("Error in I/O");
System.err.println(ioe.getMessage());
System.exit(-1);
}
}
}
The problem is your server is reading the stream only once and closing the socket connection.
How will your server know you have finished sending the client data to the server socket ?
You should modify the server to listen to the port till you have finished sending the whole text. For that do something like -
String newLine;
while ( true )
newLine = inFromClient.readLine();
if (newLine.equalsIgnoreCase("END"))
{
break;
}
newLine = newLine.toUpperCase();
// Send message
network.writeUTF(newLine);
}
// Close sockets. This will cause the client to exit
sock.close();
server.close();
And from the client send "END" after all lines have been sent.
The easiest way is to use IOUtils from Apache Commons. IOUtils.readLines will return a list of Strings
Exact same question : Read/convert an InputStream to a String

Java socket server blocking

I have some problems with my server socket. I create a DatagramSocket to chat between a server and a client.
public static void main (String[] args) throws IOException {
byte[] send = new byte[1024];
byte[] receive = new byte[1024];
BufferedReader entree;
DatagramSocket serverSocket = null;
InetAddress ip;
InetAddress ipDest;
int port;
try {
serverSocket = new DatagramSocket(8888);
} catch (SocketException e) {
e.printStackTrace();
}
while (true) {
DatagramPacket recu = new DatagramPacket(receive, receive.length);
serverSocket.receive(recu);
String sentence = new String(recu.getData());
ipDest = recu.getAddress();
port = recu.getPort();
System.out.println("Reçu:"+sentence);
entree = new BufferedReader(new InputStreamReader(System.in));
String chaine = entree.readLine();
send = chaine.getBytes();
DatagramPacket dp = new DatagramPacket(send, send.length, ipDest, port);
serverSocket.send(dp);
send = new byte[1024];
receive = new byte[1024];
}
But I use new BufferedReader(new InputStreamReader(System.in)) get the next stuff to send, and it is blocking. So, I cannot receive what's comming from the client and print it.
How can I arrange this ?
Merci, eo
Trying to do non-blocking reads on System.in in Java is an exercise in futility. There's no portable way to do it, so Java doesn't support it.
Even if you create a separate thread and do a blocking read there, you'll have the problem of that thread being non-interruptible. See: Java: how to abort a thread reading from System.in
Basically, you either need to use a platform specific library (JNI) (JCurses for linux, for example), or use a GUI.
Edit to add: What you can do is move your socket reading to a different thread, as that is interruptible.

Categories

Resources