?? client1.java
字號:
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class client1 extends JFrame
{ private JTextField enter;
private JTextArea display;
PrintStream output;
DataInputStream input;
String message="";
public client1()
{super("Client");
Container c=getContentPane();
enter=new JTextField();
enter.addActionListener(new ActionListener()
{ public void actionPerformed(ActionEvent e)
{sendData(enter.getText());
}
});
c.add(enter,BorderLayout.NORTH);
display=new JTextArea();
c.add(new JScrollPane(display),BorderLayout.CENTER);
setSize(500,500);
show();
}
public void connect()
{Socket socket;
try{
display.setText("準備連接.....");
socket=new Socket(InetAddress.getByName(""),4321);
display.append("連接到:"+socket.getInetAddress().getHostName());
display.append("\n主機IP為:"+socket.getInetAddress().toString());
output=new PrintStream(new BufferedOutputStream(socket.getOutputStream()));
output.flush();
input=new DataInputStream(new BufferedInputStream(socket.getInputStream()));
enter.setEnabled(true);
do{
try{
message=((String)input.readLine());
display.append("\n"+message);
}
catch(IOException e)
{display.append("\n 無法連接獲得信息");
}
}
while(!message.equals("Server:end"));
display.append("\n關閉連接");
output.close();
input.close();
socket.close();
}
catch(EOFException eof)
{System.out.println("服務器中斷");
}
catch (IOException e)
{e.printStackTrace();
}
}
private void sendData(String s)
{try{message=s;
output.println("Client: "+s);
output.flush();
enter.setText("");
}
catch(Exception e)
{display.append("\n數據傳輸錯誤");
}
}
public static void main(String args[])
{client1 app=new client1();
app.addWindowListener(new WindowAdapter()
{public void windowClosing(WindowEvent e)
{System.exit(0);
}
});
app.connect();
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -