?? 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 + -