Java UDP - Sending a string array from server to client -


hi , in advance,

so i'm trying take array of jlist items , convert them string array (which think i've gotten right), , i'm trying send string array on client attempt display them jlist on side.

i've tried few different things none working.

here latest code attempt send string array over:

string[] fileslist = (string[]) lclient1files.getselectedvalues();  filesbuffer = fileslist.getbytes();  datagrampacket dgpfilesresponse = new datagrampacket(filesbuffer,filesbuffer.length, dgp.getaddress(), dgp.getport()); seedersocket.send(dgpfilesresponse); 

the line: filesbuffer = fileslist.getbytes(); causing issue because getbytes() isn't applicable here.

so questions are: 1) how send array of jlist items(they names) on client (it doesn't particularly have string array), , 2) how receive list on clients side, can use it?

thank you.

one must make binary format string array.

bytearrayoutputstream baos = new bytearrayoutputstream(); try (dataoutputstream dos = new dataoutputstream(baos)) {     dos.writeint(fileslist.length);     (string files : fileslist) {         dos.writeutf(files);     } } byte[] bytes = baos.tobytearray(); 

this internally string writes first length in bytes, , uses string.getbytes("utf-8") string can written.

reading goes reversed input classes.

if think of having many clients out there, maybe different versions, add in message version number.


on other side

bytearrayinputstream bais = new bytearrayinputstream(bytes); try (datainputstream dis = new datainputstream(baos)) {     int stringscount = dis.readint();     string[] fileslist = new string[stringscount];     (int = 0; < stringscount; ++i) {         fileslist[i] = dis.readutf();     }     return fileslist; } 

Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -