我设置服务器以及与之关联的所有事件以及包含侦听器和主类的表单。
类启动表单已激活,但服务器不会将文本消息弹出到文本区域,这表明showMessage()
的方法可能不正确。
这是代码:
import java.awt.Button;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Label;
import java.awt.TextArea;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.EOFException;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import javax.swing.SwingUtilities;
public class MiniChat {
private TextField tf2;
private TextArea ta;
private ObjectOutputStream output;
private ObjectInputStream input;
private ServerSocket server;
private Socket connection;
//
public MiniChat(){
Frame f = new Frame("MiniChat");
TextField tf = new TextField();
TextField tf1 = new TextField("127.0.0.1");
tf1.setEditable(false);
final TextField tf2 = new TextField();
final TextArea ta = new TextArea();
Button b = new Button("Poslaji!");
b.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
ta.setText(tf2.getText());
tf2.setText("");
}
});
Label l = new Label("IP adresa korisnika: ");
Label l1 = new Label("Lokalna IP adresa: ");
Label l2 = new Label("Polje za prikaz poruka: ");
Label l3 = new Label("Ukucajte poruku: ");
f.add(l);
f.add(tf);
f.add(l1);
f.add(tf1);
f.add(l2);
f.add(ta);
f.add(l3);
f.add(tf2);
f.add(b);
f.setVisible(true);
f.setSize(400, 400);
f.setLayout(new FlowLayout());
f.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent we){
System.exit(0);
}
});
}
public void startRunning(){
try{
server = new ServerSocket(6789, 100);
while(true){
try{
waitForConnection();
setupStreams();
whileChatting();
}catch(EOFException eofe){
showMessage("\nServer Ended Connection");
} finally {
closeCrap();
}
}
}catch(IOException ie){
ie.printStackTrace();
}
}
private void waitForConnection() throws IOException{
showMessage("Waiting for Someone to Connect\n");
connection = server.accept();
showMessage("Now Connected "+ connection.getInetAddress().getHostName());
}
private void setupStreams() throws IOException{
output = new ObjectOutputStream(connection.getOutputStream());
output.flush();
input = new ObjectInputStream(connection.getInputStream());
showMessage("\n Streams are now setup");
}
private void whileChatting() throws IOException{
String message = "You Are Connected";
sendMessage(message);
ableToType(true);
do{
try{
message = (String) input.readObject();
showMessage("\n" + message);
}catch(ClassNotFoundException cnfe){
showMessage("\nI dont know what user send");
}
}while(!message.equals("CLIENT - END"));
}
private void closeCrap(){
showMessage("\n Closing Connection...\n");
ableToType(false);
try{
output.close();
input.close();
connection.close();
}catch(IOException ie){
ie.printStackTrace();
}
}
private void sendMessage(String message){
try{
output.writeObject("SERVER - " +message);
output.flush();
showMessage("SERVER - " +message);
}catch(IOException ie){
ta.append("I cant send message");
}
}
private void showMessage(final String text){
SwingUtilities.invokeLater(
new Runnable(){
public void run(){
ta.append(text);
}
}
);
}
private void ableToType(final boolean tof){
SwingUtilities.invokeLater(
new Runnable(){
public void run(){
ta.setEditable(tof);
}
}
);
}
}
这是主要的课程:
import java.awt.Frame;
import javax.swing.*;
public class Cs {
public static void main(String[] args){
MiniChat mc = new MiniChat();
mc.startRunning();
}
}