java的套接字Socket问题

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java的套接字Socket问题相关的知识,希望对你有一定的参考价值。

用套接字Socket来实现客户端程序和服务器端程序。
客户端在其界面内有三个文本框用来输入一个一元二次方程的三个系数,点击“连接服务器”按钮后,将其发给服务器,点击“求方程的根”按钮后,在两个实根的文本框中显示这个实根的值。
服务器接到数据后将计算出的方程的实根返回给客户端。
(最好有必要的注释)
能不能有多些注释,程序太复杂了?

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();



参考技术A 你的分太少,程序太复杂 参考技术B 1楼说的有理,待我哪天有空给你看看 参考技术C 恩,也有同感,我可以给你写一个,不过5分太少了。

socket(java)

  在java中,客户端套接字和服务端套接字是分开的。客户端套接字使用类Socket实现,常用的构造函数是:

public Socket(String host, int port)

  此构造函数创建一个流套接字,并将其连接到指定主机上的指定端口号。但是这样创建的套接字默认是阻塞的,如果连接不到主机,它将会一直无限期地阻塞下去,直到它建立了到达主机的初始连接为止。可以通过先构建一个无连接的套接字,然后再使用一个超时来进行连接的方法解决这个问题。

以上是关于java的套接字Socket问题的主要内容,如果未能解决你的问题,请参考以下文章

socket(java)

java socket有啥作用

java套接字(socket)实例

java - 如何超时读取Java Socket?

TCP/UDP套接字 java socket编程实例

在客户端 JAVA 中调用 socket.close() 时,套接字未关闭服务器端