dataoutputstream

Can not read number send with DataOutputStream

放肆的年华 提交于 2019-12-02 11:03:32
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) { // TODO Auto-generated catch block e.printStackTrace(); } But i don't recive anything in fromClient

writeUTF(String s) vs writeObject(String s)

情到浓时终转凉″ 提交于 2019-12-02 01:19:14
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 constructing it, I read somewhere you have to do this. I want to send both Strings and another Object

Writing unicode to rtf file

我只是一个虾纸丫 提交于 2019-11-29 16:02:22
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 tried: byte[] b = strJapanese.getBytes("UTF-8"); String output = new String(b); Or more specific: byte

Unable to write into DataOutputStream beyond a specific size - OutOfMemoryError

﹥>﹥吖頭↗ 提交于 2019-11-29 07:41:36
I have the following code which produces an OutOfMemory exception: byte[] buf = new byte[10240]; int len = 0; DataOutputStream dataOS = new DataOutputStream(conn.getOutputStream()); while ((len = _inputStream.read(buf)) > 0) { System.out.println("len : " + len); System.out.println("Going to write buf into dataOS : " + buf.length); dataOS.write(buf, 0, len); System.out.println("dataOS.size() : " + dataOS.size()); } _inputStream.close(); The following is the last few lines from the debug output: len : 10240 Going to write buf into dataOS : 10240 dataOS.size() : 342804702 len : 10240 Going to

Android TCP does not flush until socket is closed [duplicate]

心已入冬 提交于 2019-11-28 05:07:21
问题 This question already has an answer here: Java Socket why server can not reply client 4 answers I have been trying various implementations to make this work, and have searched StackOverflow and Android Developers for a solution, but I am not too experienced in programming and cannot get this block to code to work properly. My Intent: This is in a thread that will loop through checking if there is an outMessage, if there is, it will send the message. Next it will check it if there is anything

Why does DataOutputStream.writeUTF() add additional 2 bytes at the beginning?

守給你的承諾、 提交于 2019-11-27 20:48:27
When I was trying to parse xml using sax over sockets I came across a strange occurence. Upon analysing I noticed that DataOutputStream adds 2 bytes in front of my data. Message send by DataOutputStream: 0020 50 18 00 20 0f df 00 00 00 9d 3c 3f 78 6d 6c 20 P.. .... ..<?xml 0030 76 65 72 73 69 6f 6e 3d 22 31 2e 30 22 3f 3e 3c version= "1.0"?>< 0040 63 6f 6d 70 61 6e 79 3e 3c 73 74 61 66 66 3e 3c company> <staff>< 0050 66 69 72 73 74 6e 61 6d 65 3e 79 6f 6e 67 3c 2f firstnam e>yong</ 0060 66 69 72 73 74 6e 61 6d 65 3e 3c 6c 61 73 74 6e firstnam e><lastn 0070 61 6d 65 3e 6d 6f 6f 6b 20 6b 69 6d

Why does DataOutputStream.writeUTF() add additional 2 bytes at the beginning?

坚强是说给别人听的谎言 提交于 2019-11-26 20:15:15
问题 When I was trying to parse xml using sax over sockets I came across a strange occurence. Upon analysing I noticed that DataOutputStream adds 2 bytes in front of my data. Message send by DataOutputStream: 0020 50 18 00 20 0f df 00 00 00 9d 3c 3f 78 6d 6c 20 P.. .... ..<?xml 0030 76 65 72 73 69 6f 6e 3d 22 31 2e 30 22 3f 3e 3c version= "1.0"?>< 0040 63 6f 6d 70 61 6e 79 3e 3c 73 74 61 66 66 3e 3c company> <staff>< 0050 66 69 72 73 74 6e 61 6d 65 3e 79 6f 6e 67 3c 2f firstnam e>yong</ 0060 66 69