dataoutputstream

How to send JSON response using socket in Java

不打扰是莪最后的温柔 提交于 2019-12-23 01:19:39
问题 I have built a basic HTTP server and I am attempting to send a reply to a GET request. I have installed Gson as the JSON parser, but I'm not sure how to encode the response in JSON and send back to the client. Here is my code, any help is much appreciated. (last method for actual response) public class HttpServer extends Thread{ //Private variables private Socket connectedClient = null; private BufferedReader clientRequest = null; private DataOutputStream responseToClient = null; /** * Public

Can not read number send with DataOutputStream

不羁岁月 提交于 2019-12-20 07:26:11
问题 this is my Client code Random rand = new Random(); int n = rand.nextInt(50) + 1; DataInputStream dis = new DataInputStream(_socket.getInputStream()); DataOutputStream dos = new DataOutputStream(_socket.getOutputStream()); dos.writeInt(n); and this is the Server code try { DataInputStream dis = new DataInputStream(socket.getInputStream()); BufferedReader input = new BufferedReader(new InputStreamReader(dis)); int fromClient = input.read(); System.out.println(fromClient); } catch (IOException e

writeUTF(String s) vs writeObject(String s)

白昼怎懂夜的黑 提交于 2019-12-20 03:38:31
问题 In this Java project I'm working on for university, I have a situation where I am currently sending strings through the network successfully using streamOut = ObjectOutputStream streamIn = ObjectInputStream streamOut.writeUTF(msgs.peek()); where msgs is a linked blocking queue, receiving it with String in = streamIn.readUTF(); however, I would like to use an ObjectInputStream and an ObjectOutputStream. I have initialized them both in the constructor and I flush the ObjectOutputStream after

Reading/writing a BINARY File with Strings?

有些话、适合烂在心里 提交于 2019-12-19 04:58:28
问题 How can I write/read a string from a binary file? I've tried using writeUTF / readUTF (DataOutputStream/DataInputStream) but it was too much of a hassle. Thanks. 回答1: Forget about FileWriter, DataOutputStream for a moment. For binary data one uses OutputStream and InputStream classes. They handle byte[] . For text data one uses Reader and Writer classes. They handle String which can store all kind of text, as it internally uses Unicode. The crossover from text to binary data can be done by

Writing unicode to rtf file

♀尐吖头ヾ 提交于 2019-12-18 09:23:09
问题 I´m trying write strings in diffrent languages to a rtf file. I hav tried a few different things. I use japanese here as an example but it´s the same for other languages i have tried. public void writeToFile(){ String strJapanese = "日本語"; DataOutputStream outStream; File file = new File("C:\\file.rtf"); try{ outStream = new DataOutputStream(new FileOutputStream(file)); outStream.writeBytes(strJapanese); outStream.close(); }catch (Exception e){ System.out.println(e.toString()); } } I alse have

how to use ByteArrayOutputStream and DataOutputStream simultaneously (Java)

烈酒焚心 提交于 2019-12-18 02:43:34
问题 I'm having quite a problem here, and I think it is because I don't understand very much how I should use the API provided by Java. I need to write an int and a byte[] into a byte[] I thought of using a DataOutputStream to solve the data writing with writeInt(int i) and write(byte[] b) , and to be able to put that into a byte array, I should use ByteArrayOutputStream method toByteArray(). I understand that this classes use the Wrapper pattern, so I had two options: DataOutputStream w = new

how to use ByteArrayOutputStream and DataOutputStream simultaneously (Java)

会有一股神秘感。 提交于 2019-12-18 02:43:23
问题 I'm having quite a problem here, and I think it is because I don't understand very much how I should use the API provided by Java. I need to write an int and a byte[] into a byte[] I thought of using a DataOutputStream to solve the data writing with writeInt(int i) and write(byte[] b) , and to be able to put that into a byte array, I should use ByteArrayOutputStream method toByteArray(). I understand that this classes use the Wrapper pattern, so I had two options: DataOutputStream w = new

After sending byte arrays via dataoutputstream, the received byte arrays are not equal to original byte arrays

拈花ヽ惹草 提交于 2019-12-12 04:46:46
问题 For some reasons, I need to send multiple byte arrays separately via server socket, and client socket will receive these byte arrays. After sending byte arrays, I found byte arrays received in client socket are not equal to those in server socket. If I use ObjectOutputStream and ObjectInputStream, then everything works fine, but for my need, I can't use ObjectOutputStream & ObjectInputStream, because my server need to connect more than 2 sockets. Here is Server and Client code: Server(Sender)

Why it returns Internal Server Error on httpUrlConnection?

天大地大妈咪最大 提交于 2019-12-11 20:09:06
问题 I'm developing an application that can send different files to web server.Also I want to send large files, in able to do this I need to chunk the files. But when I'm sending the files to server nothing is uploaded. I don't know if there's error on how I'm sending the files and it gives an error 500 (internal server error) on my response.I don't think the server is the problem because when I'm uploading a file using multiPartEntity it works but when Im using BufferedInputStream and

Writing large strings with DataOutputStream

荒凉一梦 提交于 2019-12-09 12:01:26
问题 I've been doing some socket programming to transmit information across the wire. I've run into a problem with DataOutputStream.writeUTF(). It seems to allow strings of up to 64k but I have a few situations where I can run over this. Are there any good alternatives that support larger strings or do I need to roll my own? 回答1: It actually uses a two bytes to write the length of the string before using an algorithm that compacts it into one, two or three bytes per character. (See the