您的位置:首页 > 编程语言 > Java开发

Java用socket与awt实现简单对话

2017-03-30 16:25 204 查看


客户端

public class RoomClient01 implements ActionListener {
static TextArea textArea;
static TextField textField;
static Button send;
static String toServer;
static DataInputStream dataInputStream;
static DataOutputStream dataOutputStream;
Socket socket;
public RoomClient01(Socket socket) {
// TODO Auto-generated constructor stub
this.socket = socket;
}
/*
* 创建GUI面板
*/
public void createUI () throws IOException {
Frame f = new Frame("客户端");
textArea = new TextArea();
textField = new TextField();
send = new Button("send");
send.addActionListener(this);
Panel p = new Panel();
p.setLayout(new BorderLayout());
p.add(textField, "Center");
p.add(send, "East");
f.add(textArea, "Center");
f.add(p, "South");
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
f.setSize(400, 400);
f.setLocation(600, 0);
f.setVisible(true);
connect();
}
/*
* 连接服务端并且不断从服务端读取数据
*/

public void connect() throws IOException {
dataInputStream = new DataInputStream(socket.getInputStream());
dataOutputStream = new DataOutputStream(socket.getOutputStream());
while(true) {
String string = dataInputStream.readUTF();
textArea.append("服务>>"+string+"\n");
}
}
/*
* 添加点击事件
*/
@Override
public void actionPerformed(ActionEvent e) {
Object source = e.getSource();
if (source==send) {
toServer=textField.getText();
try {
textArea.append("客户>>"+toServer+"\n");
dataOutputStream.writeUTF(toServer);
dataOutputStream.flush();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
public static void main(String[] args) throws IOException {
Socket socket = new Socket("localhost", 5551);
RoomClient01 roomClient01 = new RoomClient01(socket);
roomClient01.createUI();
}

}


服务端

public class RoomServer01 implements ActionListener{
static  TextArea textArea;
static TextField textField;
static String fromclient;
static Button send;
static DataInputStream dataInputStream;
static DataOutputStream dataOutputStream;
ServerSocket serverSocket;
/*
* 创建GUI面板
*/
public void createUI () throws IOException {
Frame f = new Frame("服务端");
textArea = new TextArea();
textField = new TextField();
send = new Button("send");
send.addActionListener(this);
Panel p = new Panel();
p.setLayout(new BorderLayout());
p.add(textField, "Center");
p.add(send, "East");
f.add(textArea, "Center");
f.add(p, "South");
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
f.setSize(400, 400);
f.setLocation(600, 0);
f.setVisible(true);
connect();
}
public RoomServer01(ServerSocket serverSocket) {
// TODO Auto-generated constructor stub
this.serverSocket = serverSocket;
}
/*
*添加点击事件
*/
@Override
public void actionPerformed(ActionEvent e) {
Object source = e.getSource();
if (source==send) {
String toclient = textField.getText();
textArea.append("服务>>"+toclient);
try {
dataOutputStream.writeUTF(toclient);
dataOutputStream.flush();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
/*
* 连接客户端并且不断从客户端读取数据
*/
public  void connect() throws IOException{
Socket socket = serverSocket.accept();
dataInputStream = new DataInputStream(socket.getInputStream());
dataOutputStream = new DataOutputStream(socket.getOutputStream());
while(true) {
String string = dataInputStream.readUTF();
textArea.append("客户>>"+string+"\n");
}
}

public static void main(String[] args) throws IOException {
ServerSocket serverSocket = new ServerSocket(5551);
RoomServer01 roomServer01 = new RoomServer01(serverSocket);
roomServer01.createUI();
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: