这个是上一篇的发酵
servercode:
package TCP;
import java.awt.EventQueue;
public class SocketServer extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
private JPanel contentPane;
private JTextField MsgBox;
private JTextArea MsgDisplay;
private ServerSocket ss ;
private Socket socket ;
private InputStream in ;
private BufferedWriter bw;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
SocketServer frame = new SocketServer();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public void buildSocketServer() throws IOException{
ss = new ServerSocket();
ss.bind(new InetSocketAddress(40000));
socket = ss.accept();
in = socket.getInputStream();
bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
}
/**
* Create the frame.
*/
public SocketServer() {
setTitle("服务端");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 604, 432);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
//发送
Button SendButton = new Button("发送给客户端");
SendButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
if(MsgBox.getText()!=null)
bw.write(MsgBox.getText());
bw.flush();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});
SendButton.setBounds(38, 340, 141, 23);
contentPane.add(SendButton);
//退出
Button ExitButton = new Button("建立连接");
ExitButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
buildSocketServer();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});
ExitButton.setBounds(411, 340, 141, 23);
contentPane.add(ExitButton);
//输入
MsgBox = new JTextField();
MsgBox.setColumns(10);
MsgBox.setBounds(131, 284, 421, 23);
contentPane.add(MsgBox);
//显示
MsgDisplay = new JTextArea();
MsgDisplay.setEditable(false);
MsgDisplay.setBounds(38, 55, 514, 201);
contentPane.add(MsgDisplay);
JLabel label = new JLabel("从客户端接收的内容:");
label.setBounds(38, 32, 131, 15);
contentPane.add(label);
JLabel label_1 = new JLabel("请输入要发送给客户端的内容:");
label_1.setBounds(10, 283, 224, 23);
contentPane.add(label_1);
JButton receiveButton = new JButton("接收");
receiveButton.setBounds(239, 340, 93, 23);
contentPane.add(receiveButton);
receiveButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
byte[] buf = new byte[2048];
int i =-1;
try {
i = in.read(buf);
System.out.println(i);
} catch (IOException e1) {
e1.printStackTrace();
}
String request = new String(buf,0,i);
String NewMsgText = MsgDisplay.getText()+request+"\n";
MsgDisplay.setText(NewMsgText);
}
});
}
}
clientcode:
package TCP;
import java.awt.EventQueue;
public class SocketClient extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
private JPanel contentPane;
private JTextField MsgBox;
private JTextArea MsgDisplay;
private Socket socket;
private InputStream in;
private BufferedWriter bw;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
SocketClient frame = new SocketClient();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* buildSocketClient.
* @return
* @throws IOException
*/
public void buildSocketClient() throws IOException{
socket = new Socket();
socket.connect(new InetSocketAddress(InetAddress.getByName("127.0.0.1"),40000));
in = socket.getInputStream();
bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
}
/**
* Create the frame.
*/
public SocketClient() {
setTitle("客户端");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 601, 445);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
//发送
Button SendButton = new Button("发送给服务端");
SendButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
if(MsgBox.getText()!=null)
bw.write(MsgBox.getText());
bw.flush();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});
SendButton.setBounds(30, 350, 141, 23);
contentPane.add(SendButton);
//退出
Button ExitButton = new Button("建立连接");
ExitButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
buildSocketClient();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});
ExitButton.setBounds(403, 350, 141, 23);
contentPane.add(ExitButton);
//输入
MsgBox = new JTextField();
MsgBox.setBounds(123, 301, 421, 23);
contentPane.add(MsgBox);
MsgBox.setColumns(10);
//显示
MsgDisplay = new JTextArea();
MsgDisplay.setEditable(false);
MsgDisplay.setBounds(30, 57, 514, 201);
contentPane.add(MsgDisplay);
JLabel label = new JLabel("\u8BF7\u8F93\u5165\u8981\u53D1\u9001\u7ED9\u670D\u52A1\u7AEF\u7684\u5185\u5BB9\uFF1A");
label.setBounds(0, 301, 224, 23);
contentPane.add(label);
JLabel label_1 = new JLabel("\u4ECE\u670D\u52A1\u7AEF\u63A5\u6536\u7684\u5185\u5BB9\uFF1A");
label_1.setBounds(30, 32, 214, 15);
contentPane.add(label_1);
JButton receiveButton = new JButton("接收");
receiveButton.setBounds(238, 350, 93, 23);
contentPane.add(receiveButton);
receiveButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
while(true){
byte[] buf = new byte[2048];
int i = -1;
try {
i = in.read(buf);
System.out.println(i);
} catch (IOException e1) {
e1.printStackTrace();
}
String request = new String(buf,0,i);
String NewMsgText = MsgDisplay.getText()+request+"\n";
MsgDisplay.setText(NewMsgText);
}
}
});
}
}