📄 myclient.java
字号:
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
public class MyClient extends Frame implements ActionListener
{
Socket ClientSocket;
PrintStream os;
BufferedReader is;
String s;
Label Mylabel=new Label("欢迎使用本机提供的信息");
TextArea textArea;
Button MyButton = new Button("发送");
public MyClient()
{
setTitle("Client Window"); //建立并显示与Server通讯的信息窗口
setLayout(new BorderLayout());
this.addWindowListener(new WinAdptClient(this));
MyButton.addActionListener(this);
textArea=new TextArea(20,50);
add("North",Mylabel);
add("South",MyButton);
add("Center",textArea);
setResizable(false);
pack();
show();
connect(); //与Server端连接通讯
}
public void connect()
{
try{
ClientSocket=new Socket("sd-7404",8000); //连向Server主机的8000端口
os=new PrintStream(
new BufferedOutputStream(ClientSocket.getOutputStream()));
is=new BufferedReader(new InputStreamReader(ClientSocket.getInputStream()));
s=is.readLine(); //从Server端读入数据
textArea.append(s+"\n");
}
catch(Exception e){}
}
public void actionPerformed(ActionEvent e)
{ //当点击按钮时向Server端发送信息
if(e.getSource()==MyButton)
{
try{
os.print(textArea.getText());
os.flush();
}
catch(Exception ex){}
}
}
public static void main(String args[])
{
new MyClient();
}
}
class WinAdptClient extends WindowAdapter
{
MyClient m_Parent;
WinAdptClient(MyClient p)
{
m_Parent = p;
}
public void windowClosing(WindowEvent e)
{
try{ //关闭窗口前先向Server端发送结束信息,并关闭各输入输出流与连接
m_Parent.os.println("Bye");
m_Parent.os.flush();
m_Parent.is.close();
m_Parent.os.close();
m_Parent.ClientSocket.close();
m_Parent.dispose();
System.exit(0);
}catch(Exception ex){}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -