java小程序,帮忙看看怎么改
我想做一个client 每按一次button 给server 传一个random 在server读入一个字符串到client的textarea stream socket都没关 不足很多 帮忙 尽可能修改 谢谢!!!
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Frame;
import java.awt.Panel;
import java.awt.TextArea;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.BufferedReader;
import
java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.
UnknownHostException;
import java.util.Random;
public class AdviceClient {
Random ra = new Random();
InputStreamReader r = null;
BufferedReader reader = null;
Socket socket = null;
Frame frame;
TextArea area;
Button button;
public static void main(String[] args) {
AdviceClient ac = new AdviceClient();
ac.buildGUI();
}
public void buildGUI() {
frame = new Frame("Advice!");
area = new TextArea();
button = new Button("Get Advice!");
button.addActionListener(new pressListener());
frame.add(BorderLayout.SOUTH,button);
frame.add(BorderLayout.CENTER,area);
frame.setSize(300, 300);
frame.setVisible(true);
frame.setLocation(100, 100);
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
setUpNetWorks();
}
public void setUpNetWorks() {
try {
socket = new Socket("127.0.0.1", 5678);
r = new InputStreamReader(socket.getInputStream());
reader = new BufferedReader(r);
while(true){
area.append(reader.readLine() + '\n');
}
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (
IOException e) {
e.printStackTrace();
}
}
class pressListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
PrintWriter writer;
try {
writer = new PrintWriter(socket.getOutputStream());
writer.write(ra.nextInt(8));
writer.flush();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class AdviceServer {
String[] s = { "1aaa", "2bbb", "3ccc", "4ddd", "5eee", "6fff", "7ggg" };
ServerSocket serverSocket = null;
Socket socket = null;
InputStreamReader r;
BufferedReader reader;
PrintWriter writer;
public static void main(String[] args) {
AdviceServer server = new AdviceServer();
server.connect();