import java.io.*;
import java.net.*;
import java.util.Scanner;
public class sockclient {
public static void main(String[] args) throws Exception{
Socket socket = new Socket("192.168.159.128",8888);
InputStream input = socket.getInputStream();
OutputStream output = socket.getOutputStream();
Thread t = new Thread(new Runnable() {
public void run() {
try {
while(true) {
System.out.println("[Enter your message]");
byte[] by = new byte[4096];
Scanner scan = new Scanner(System.in);
String mess = null;
if(scan.hasNext()) {
mess = scan.next();
scan.close();
}
by = mess.getBytes();
output.write(by);
output.flush();
byte[] b = new byte[4096];
int leng;
StringBuilder sb = new StringBuilder();
while((leng = input.read(b)) != -1) {
sb.append(new String(b,0,leng));
if(new String(b,0,leng).indexOf("\r") >= 0) {
break;
}
System.out.println(sb.toString().substring(0,sb.toString().length()-4));
Thread.sleep(1000);
}
}
} catch (IOException e) {
e.printStackTrace();
} catch(Exception e) {
e.printStackTrace();
}
}
});
t.start();
}
}
各位博主,请问如何修改以上代码实现消息的多次发送?以上代码发送一次后就不可以发送第二次,望解决。