📄 chatserver.java
字号:
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
public class chatServer extends Frame implements ActionListener
{ Label label=new Label("聊天");
Panel panel=new Panel( );
TextField tf=new TextField(10);
TextArea ta=new TextArea( );
ServerSocket server;
Socket client;
InputStream in;
OutputStream out;
public chatServer( )
{ super("服务器");
setSize(250,250);
panel.add(label);
panel.add(tf);
tf.addActionListener(this);
add("North",panel);
add("Center",ta);
addWindowListener(new WindowAdapter( )
{ public void windowCloseing(WindowEvent e)
{ System.exit(0);}} );
show( );
try
{ server=new ServerSocket(5000);
client=server.accept( );
ta.append("已连接的客户机:"+client.getInetAddress( ).getHostName( )+"\n\n");
in=client.getInputStream( );
out=client.getOutputStream( );
}
catch(IOException ioe) { }
while(true)
{ try
{ byte[ ] buf=new byte[256];
in.read(buf);
String str=new String(buf);
ta.append("客户机说:"+str);
ta.append("\n");
}
catch(IOException ioe) { }
}
}
public void actionPerformed(ActionEvent e)
{ try
{ String str=tf.getText( );
byte[ ] buf=str.getBytes( );
tf.setText(null);
out.write(buf);
ta.append("我说:"+str);
ta.append("\n");
}
catch(IOException ioe) { }
}
public static void main(String args[ ])
{ new chatServer( );
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -