java的套接字Socket问题
发布网友
发布时间:2022-05-01 18:38
我来回答
共4个回答
热心网友
时间:2022-06-21 08:53
server:
package socket;
import javax.swing.*;
import java.awt.*;
import java.io.IOException;
import java.net.*;
import java.util.Vector;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.GridBagConstraints;
import java.awt.Point;
import javax.swing.JButton;
import javax.swing.JLabel;
import java.awt.Color;
import javax.swing.ImageIcon;
public class Server extends JFrame implements Runnable {
private static final long serialVersionUID = 1L;
private JPanel jContentPane = null;
private JPanel diaPanel = null;
private JPanel outPanel = null;
private JPanel btnPanel = null;
private JButton closeButton = null;
private JButton sendButton = null;
private JTextArea outTextArea = null;
private List dialist = null;
public Server() {
super();
initialize();
Listen();
}
private void initialize() {
this.setSize(355, 267);
this.setContentPane(getJContentPane());
this.setTitle("服务器");
this.setVisible(true);
this.setLocationRelativeTo(null);
this.setResizable(false);
}
private JPanel getJContentPane() {
if (jContentPane == null) {
img3Label = new JLabel();
img3Label.setBounds(new Rectangle(269, 94, 85, 95));
img3Label.setIcon(new ImageIcon(getClass().getResource("/socket/7.jpg")));
img3Label.setText("");
img2Label = new JLabel();
img2Label.setBounds(new Rectangle(267, 1, 86, 94));
img2Label.setIcon(new ImageIcon(getClass().getResource("/socket/6.jpg")));
img2Label.setText("");
imgLabel = new JLabel();
imgLabel.setBounds(new Rectangle(1, 95, 269, 23));
imgLabel.setBackground(new Color(255, 153, 153));
imgLabel.setIcon(new ImageIcon(getClass().getResource("/socket/24.jpg")));
imgLabel.setText("");
jContentPane = new JPanel();
jContentPane.setLayout(null);
jContentPane.add(getDiaPanel(), null);
jContentPane.add(getOutPanel(), null);
jContentPane.add(getBtnPanel(), null);
jContentPane.add(imgLabel, null);
jContentPane.add(img2Label, null);
jContentPane.add(img3Label, null);
}
return jContentPane;
}
private JPanel getDiaPanel() {
if (diaPanel == null) {
GridBagConstraints gridBagConstraints11 = new GridBagConstraints();
gridBagConstraints11.fill = GridBagConstraints.BOTH;
gridBagConstraints11.gridy = 0;
gridBagConstraints11.weightx = 1.0;
gridBagConstraints11.weighty = 1.0;
gridBagConstraints11.gridheight = 1;
gridBagConstraints11.gridx = 0;
diaPanel = new JPanel();
diaPanel.setLayout(new GridBagLayout());
diaPanel.setLocation(new Point(0, 0));
diaPanel.setSize(new Dimension(271, 94));
diaPanel.add(getDialist(), gridBagConstraints11);
}
return diaPanel;
}
private JPanel getOutPanel() {
if (outPanel == null) {
GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
gridBagConstraints1.fill = GridBagConstraints.BOTH;
gridBagConstraints1.weighty = 1.0;
gridBagConstraints1.weightx = 1.0;
outPanel = new JPanel();
outPanel.setLayout(new GridBagLayout());
outPanel.setLocation(new Point(1, 120));
outPanel.setSize(new Dimension(269, 68));
outPanel.add(getOutTextArea(), gridBagConstraints1);
}
return outPanel;
}
private JPanel getBtnPanel() {
if (btnPanel == null) {
btnPanel = new JPanel();
btnPanel.setLayout(new GridBagLayout());
btnPanel.setBounds(new Rectangle(-2, 188, 356, 50));
btnPanel.add(getSendButton(), new GridBagConstraints());
btnPanel.add(getCloseButton(), new GridBagConstraints());
}
return btnPanel;
}
private JTextArea getOutTextArea() {
if (outTextArea == null) {
outTextArea = new JTextArea();
}
return outTextArea;
}
private List getDialist() {
if (dialist == null) {
dialist = new List();
}
return dialist;
}
private JButton getCloseButton() {
if (closeButton == null) {
closeButton = new JButton();
closeButton.setText("关闭");
closeButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
System.exit(EXIT_ON_CLOSE);
}
});
}
return closeButton;
}
private JButton getSendButton() {
if (sendButton == null) {
sendButton = new JButton();
sendButton.setText("发送");
sendButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
String s = outTextArea.getText();
processmsg(s);
outTextArea.setText(null);
for (int i = 0; i < clients.size(); i++) {
Connection c = (Connection) clients.get(i);
try {
c.sendmsg(s);
} catch (IOException e1) {;}
}
}
});
}
return sendButton;
}
public void processmsg(String str) {
dialist.add(str);
}
public static void main(String[] args) {
Server sv = new Server();
}
public final static int DEFAULT_PORT = 8808;
ServerSocket listen_socket;
Socket client_socket;
Vector clients;
private JLabel imgLabel = null;
private JLabel img2Label = null;
private JLabel img3Label = null;
public void Listen() {
try {
listen_socket = new ServerSocket(DEFAULT_PORT);
} catch (IOException e) {
e.printStackTrace();
}
processmsg("server:listening on port:" + DEFAULT_PORT);
clients = new Vector();
Thread thread = new Thread();
thread.start();
Socket client_socket;
try {
while(true){
client_socket = listen_socket.accept();
Connection c = new Connection(client_socket, this);
clients.add(c);
processmsg("有客户端请求连接,客户端ip地址:"
+ client_socket.getInetAddress().getHostAddress());
processmsg("one client connection!");
}
} catch (IOException e) {
e.printStackTrace();
}
}
public void run() {
//Socket client_socket;
// try {
// while(true){
// client_socket = listen_socket.accept();
// processmsg("one client connection!");
// Connection c = new Connection(client_socket, this);
// clients.add(c);
// System.out.println("有客户端请求连接,客户端ip地址:"
// + client_socket.getInetAddress().getHostAddress()
// + ",远程端口:" + client_socket.getPort() + ",本地端口:"
// + client_socket.getLocalPort());
// processmsg("有客户端请求连接,客户端ip地址:"+client_socket.getInetAddress().getHostAddress());
// processmsg("one client connection!");
// }
// } catch (IOException e) {
// e.printStackTrace();
// }
}
} // @jve:decl-index=0:visual-constraint="10,10"
client
package socket;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.Socket;
import javax.swing.*;
import java.awt.Rectangle;
import javax.swing.JButton;
import javax.swing.ImageIcon;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.Color;
import javax.swing.JLabel;
public class Client extends JFrame implements Runnable {
private static final long serialVersionUID = 1L;
private JPanel jContentPane = null;
private JPanel btnPanel = null;
private JButton sendButton = null;
private JButton closeButton = null;
private JButton connButton = null;
private JTextArea outTextArea = null;
private List dialist = null;
Socket sock;
Thread thread;
BufferedReader in;
PrintWriter out;
public final static int DEFAULT_PORT = 8808;
boolean bConnected;
private JLabel imgLabel = null;
private JLabel img2Label = null;
private JLabel img3Label = null;
public Client() {
super();
initialize();
}
private void initialize() {
this.setSize(355, 267);
this.setContentPane(getJContentPane());
this.setTitle("客户端");
this.setVisible(true);
this.setLocationRelativeTo(null);
this.setResizable(false);
}
private JPanel getBtnPanel() {
if (btnPanel == null) {
GridBagConstraints gridBagConstraints3 = new GridBagConstraints();
gridBagConstraints3.gridx = 3;
gridBagConstraints3.gridy = 1;
GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
gridBagConstraints2.gridx = 1;
gridBagConstraints2.gridy = 1;
GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
gridBagConstraints1.gridx = 0;
gridBagConstraints1.gridy = 1;
btnPanel = new JPanel();
btnPanel.setLayout(new GridBagLayout());
btnPanel.setBounds(new Rectangle(1, 187, 347, 47));
btnPanel.add(getSendButton(), gridBagConstraints1);
btnPanel.add(getCloseButton(), gridBagConstraints2);
btnPanel.add(getConnButton(), gridBagConstraints3);
}
return btnPanel;
}
private JButton getSendButton() {
if (sendButton == null) {
sendButton = new JButton();
sendButton.setText("发送");
sendButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
String msg = outTextArea.getText();
if (msg!=null) { //此句无效???
processmsg(msg);
outTextArea.setText(null);
try {
sendmsg(msg);
} catch (IOException e2) {
processmsg(e2.toString());
}
}
}
});
}
return sendButton;
}
private JButton getCloseButton() {
if (closeButton == null) {
closeButton = new JButton();
closeButton.setText("关闭");
closeButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
System.exit(EXIT_ON_CLOSE);
}
});
closeButton.setText("关闭");
}
return closeButton;
}
private JButton getConnButton() {
if (connButton == null) {
connButton = new JButton();
connButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
startConnect();
}
});
connButton.setText("链接");
}
return connButton;
}
private JTextArea getOutTextArea() {
outTextArea = new JTextArea();
outTextArea.setLocation(new Point(0, 119));
outTextArea.setSize(new Dimension(269, 68));
return outTextArea;
}
public void processmsg(String str) {
this.dialist.add(str);
}
private List getDialist() {
dialist = new List();
dialist.setLocation(new Point(-1, 1));
dialist.setSize(new Dimension(271, 94));
return dialist;
}
public void run() {
while (true) {
try {
String msg = receivemsg();
Thread.sleep(100L);
if (msg != null) {
processmsg(msg);
}
} catch (IOException e) {
e.printStackTrace();
}catch (InterruptedException ei) {}
}
}
public void sendmsg(String msg)throws IOException {
out.println(msg);
out.flush();
}
public String receivemsg() throws IOException{
String msg = new String();
try {
msg = in.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return msg;
}
public void startConnect() {
bConnected = false;
try {
sock = new Socket("127.0.0.1", DEFAULT_PORT);
bConnected = true;
processmsg("connect ok!");
in =new BufferedReader(new InputStreamReader(sock.getInputStream()));
out=new PrintWriter(new BufferedWriter(new OutputStreamWriter(sock.getOutputStream())));
} catch (IOException e) {
e.printStackTrace();
processmsg("connection failed");
}
if(thread==null){
thread=new Thread(this);
thread.start();
}
}
private JPanel getJContentPane() {
if (jContentPane == null) {
img3Label = new JLabel();
img3Label.setText("");
img3Label.setSize(new Dimension(269, 23));
img3Label.setLocation(new Point(1, 94));
img2Label = new JLabel();
img2Label.setBounds(new Rectangle(270, 95, 81, 89));
img2Label.setIcon(new ImageIcon(getClass().getResource("/socket/10.jpg")));
img2Label.setText("");
imgLabel = new JLabel();
imgLabel.setBounds(new Rectangle(270, 0, 78, 94));
imgLabel.setIcon(new ImageIcon(getClass().getResource("/socket/3.jpg")));
imgLabel.setText("");
jContentPane = new JPanel();
jContentPane.setLayout(null);
jContentPane.add(getBtnPanel(), null);
jContentPane.add(getOutTextArea(), null);
jContentPane.add(getDialist(), null);
jContentPane.add(imgLabel, null);
jContentPane.add(img2Label, null);
jContentPane.add(img3Label, null);
}
return jContentPane;
}
public static void main(String[] args) {
Client cl = new Client();
}
}
热心网友
时间:2022-06-21 08:54
你的分太少,程序太复杂
热心网友
时间:2022-06-21 08:54
1楼说的有理,待我哪天有空给你看看
热心网友
时间:2022-06-21 08:55
恩,也有同感,我可以给你写一个,不过5分太少了。