//ClientFrame.java
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class ClientFrame extends JFrame implements ActionListener{
JButton sendButton;
JTextField inputField;
JTextArea outputArea;
SimpleClient client;
JButton exitButton;
//在构造函数中完成图形界面的初始化
public ClientFrame(){
JLabel label1=new JLabel("输入:");
inputField=new JTextField(20);
JPanel panel1=new JPanel();
panel1.add(label1);
panel1.add(inputField);
JLabel label2=new JLabel("服务器返回:");
outputArea=new JTextArea(6,20);
JScrollPane scrollPane=new JScrollPane(outputArea);
JPanel panel2=new JPanel();
panel2.setLayout(new BorderLayout());
panel2.add(label2,BorderLayout.NORTH);
panel2.add(scrollPane,BorderLayout.CENTER);
sendButton=new JButton("发送");
sendButton.addActionListener(this);
exitButton=new JButton("退出");
exitButton.addActionListener(this);
JPanel panel=new JPanel();
panel.setLayout(new BorderLayout());
panel.add(panel1,BorderLayout.NORTH);
panel.add(sendButton,BorderLayout.CENTER);
panel.add(exitButton,BorderLayout.EAST);
panel.add(panel2,BorderLayout.PAGE_END);
setTitle("Socket客户端");
this.getContentPane().add(panel);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent ae){
//判断事件源空间是否是发送按钮
if(ae.getSource() == sendButton){
try{
client.sendRequest(inputField.getText());
}catch(Exception ex){
ex.printStackTrace();
}
outputArea.append(client.getReponse()+"/n");
}
if(ae.getSource()== exitButton){
outputArea.append("要退出了/n");
// this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//sendButton.hide();
}
}
public static void main(String args[]){
ClientFrame frame=new ClientFrame();
frame.pack();
frame.client=new SimpleClient("127.0.0.1",8888);
frame.setVisible(true);
}
}
//SimpleClient .java
import java.io.*;
import java.net.Socket;
public class SimpleClient {
PrintStream out;
BufferedReader in;
//构造方法
public SimpleClient(String serverName,int port){
try{
Socket clientSocket=new Socket(serverName,port);
out=new PrintStream(clientSocket.getOutputStream());
in=new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
}catch(Exception e){
System.out.println("无法连接服务器");
}
}
//发送请求
public void sendRequest(String request){
out.println(request);
System.out.println("Client发送请求:"+request);
}
public String getReponse(){
String str=new String();
try{
str=in.readLine();
System.out.println("Client收到Server返回:"+str);
}catch(IOException e){
}
return str;
}
}
//SimpleServer.java
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
public class SimpleServer {
ServerSocket serverSkt=null;
Socket clientSkt=null;
BufferedReader in=null;
PrintStream out=null;
//构造方法
public SimpleServer(int port){
System.out.println("服务器代理正在监听,端口:"+port);
try{
serverSkt=new ServerSocket(port);
}catch(IOException e){
System.out.println("监听端口"+port+"失败!");
}
try{
clientSkt=serverSkt.accept();
}catch(IOException e){
System.out.println("连接失败");
}
try{
in=new BufferedReader(new InputStreamReader(clientSkt.getInputStream()));
out=new PrintStream(clientSkt.getOutputStream());
}catch(IOException e){
}
}
//收到客户端请求
public String getRequest(){
String frmClt=null;
try{
frmClt=in.readLine();
System.out.println("Server收到请求:"+frmClt);
}catch(Exception e){
System.out.println("无法读取端口.......");
System.exit(0);
}
return frmClt;
}
//发送响应给客户端
public void sendResponse(String response){
try{
out.println(response);
System.out.println("Server响应请求 :"+response);
}catch(Exception ex){
System.out.println("写端口失败!。。。");
System.exit(0);
}
}
public static void main(String args[]) throws IOException{
SimpleServer sa=new SimpleServer(8888);
while(true){
sa.sendResponse(sa.getRequest());
}
}
}