问答文章1 问答文章501 问答文章1001 问答文章1501 问答文章2001 问答文章2501 问答文章3001 问答文章3501 问答文章4001 问答文章4501 问答文章5001 问答文章5501 问答文章6001 问答文章6501 问答文章7001 问答文章7501 问答文章8001 问答文章8501 问答文章9001 问答文章9501

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分太少了。
声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com
抖音弹幕怎么关掉?怎么关闭抖音弹幕? 惠普LaserJet P3005D是否支持B5纸的双面打印? word打印出图片总是缺一部分怎么办-word打印图片不完整怎么解决_百度... 理想one哪里产的车辆? 抚州抚州ONE在哪里? one地址在哪里? 如何在图片上写字(如何在图片上添加文字) 网商贷为什么钱没到账 高级经济师职称怎么评 高级经济师需要评审吗 冒险岛恶魔猎手火炮手英雄哪个多好哪个多费钱 小米刷机包哪个文件是基带 存包柜问题(C语言的) 金龙火炮和英雄火炮对比 MySQL建表错误??? 英雄联盟:商店的装备是哪些英雄手中的武器? 冒险岛火炮与英雄比较 哪个更好?从价钱 输出 生存分析 求分支限界法解01背包问题 小米6基带支持多少网速 火炮英雄游戏打炮技巧详解 让你百发百中 我做了一个公司logo,含有艺术字体,我通过了简单变形。该logo是要注册的。 小米2s电信版的基带芯片型号是什么,是否支持LTE 小米6x可以升级基带吗 哪个公司做艺术字logo做的比较好? 小米基带是怎么升级的,每周五更新版本能升级基带吗 小米手机 采用的是什么牌子跟什么型号的基带 奥迪Q5和奔驰GLK260哪一款比较好 各保险公司理赔电话号码 2014款 奔驰GLK260和奔驰E260哪个好,有什么区别 人寿保险车险电话 小米5X线刷丢基带问题 冒险岛最实用的超级技能排行榜,一一列举,必须解释 java可以像VB一样添加控件么? 英雄联盟英雄出火炮后仔该英雄周围的圈是什么意思 冒险岛恶魔猎手,火炮手,英雄哪个多好,哪个多费钱? 小米5没有基带了怎么办 js脚本怎么调用@viewbag的值?? 爆枪英雄火炮配谁人 hibernate的hbm文件&lt;bag&gt;标签的cascade属性值为lock,是什么含义?_百度... 爆枪英雄火炮用什么技能详解 JAVA中commons-collections-3.2.1.jar包是干什么用的 冒险岛107版本火炮手属于哪个职业群,在哪转职 就是背包问题。请发我邮箱731247494@qq.com谢谢 爆枪英雄火炮怎么获得 爆枪英雄火炮获得方 ASP 谁能够帮我注解一下这段ASP代码, 介绍下V104冒险岛职业特色!最好全一点! 欧陆战争火箭炮带什么英雄 怎样用DirectShow来压缩一个AVI文件 求一帝国2征服者地图 侠盗猎车手全套秘籍