简体   繁体   中英

Sending with tcp/ip

I have client-server application and i need to send words from client to server and on server they must be separated(i will use them in DB), what is the best way to do it, send each word separately or may be divided line with all words on server?

Client:

//arraylist
ArrayList<String> Arsend = new ArrayList<String>();
send.add("dod");
send.add("sani");
send.add("fred");
//sending
ObjectOutputStream out = new ObjectOutputStream(s.getOutputStream());
out.writeObject(Arsend);

Server:

ObjectInputStream in = new ObjectInputStream(s.getInputStream());
Object o = in.readObject();
System.out.println(o); //[dod, sani, fred]

Instead:

Object o = in.readObject();
System.out.println(o); //[dod, sani, fred]

Use:

//Cast in.readObject() to ArrayList<String>
ArrayList<String> receivedList = (ArrayList<String>)in.readObject();
//Loop through receivedList using for-each loop and print every String
for(String s : receivedList){
    System.out.println(s);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM