ServerSocket ss = new ServerSocket(9999);
Socket socket = ss.accept();
InputStreamReader isReader = new InputStreamReader(socket.getInputStream());
BufferedReader reader = new BufferedReader(isReader);
StringBuffer sb = new StringBuffer();
String line;
while((line=reader.readLine())!=null){
sb.append(line);
}
FileWriter fos = new FileWriter("d:/aa.txt",true);
fos.write(sb.toString());
fos.write("\r\n");
fos.close();
socket.close();
ss.close();
out.print(sb.toString()+"<br/>");
客户端代码
Socket socket = new Socket("127.0.0.1",9999);
OutputStreamWriter osWriter = new OutputStreamWriter(socket.getOutputStream());
BufferedWriter writer = new BufferedWriter(osWriter);
double b = Math.random()*10000;
writer.write("发动内容" + b);
writer.close();
socket.close();