📄 chatclient.java
字号:
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
public class chatClient extends Frame implements ActionListener
{ Label label=new Label("聊天");
Panel panel=new Panel( );
TextField tf=new TextField(10);
TextArea ta=new TextArea( );
Socket client;
InputStream in;
OutputStream out;
public chatClient( )
{ super("客户机");
setSize(250,250);
panel.add(label);
panel.add(tf);
tf.addActionListener(this);
add("North",panel);
add("Center",ta);
addWindowListener(new WindowAdapter( )
{ public void windowClosing(WindowEvent e)
{ System.exit(0);}} );
show( );
try
{ client=new Socket(InetAddress.getLocalHost( ),5000);
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 e) { }
}
}
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 chatClient( );
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -