1
回答
哪位大神能帮忙看下这个socket程序?
利用AWS快速构建适用于生产的无服务器应用程序,免费试用12个月>>>   
package chat;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;

public class Server {
    private List<ObjectOutputStream> outs;
    private ServerSocket serverSocket;

    public Server() throws Exception{
        outs = new ArrayList<>();
        serverSocket = new ServerSocket(5000);
    }
    public void start() throws Exception {
        while (true) {
            Socket socket = serverSocket.accept();
            ObjectOutputStream out = new ObjectOutputStream(socket.getOutputStream());
            outs.add(out);
            new Thread(()->{
                try {
                    ObjectInputStream in = new ObjectInputStream(socket.getInputStream());
                    Msg msg = (Msg)in.readObject();
                    System.out.println(msg);
                    printMsg(msg);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }).start();
        }
    }

    //广播
    public void printMsg(Msg msg) {
        outs.forEach(out->{
            try {
                out.writeObject(msg);
                out.flush();
            } catch (IOException e) {
                e.printStackTrace();
            }
        });
    }

    //启动主线程
    public static void main(String[] args) throws Exception {
        System.out.println("服务器已启动");
        new Server().start();

    }

}
package chat;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.sql.Timestamp;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class Client implements Runnable{

    private JFrame mFrame;
    private JPanel mPane;
    private JTextArea mArea;
    private JScrollPane mScrollPane;
    private JButton mButton;
    private JTextField mField;

    private Socket socket;
    private ObjectOutputStream out;
    private ObjectInputStream in;

    public Client() throws Exception {
        setUI();
        setNetWork();
    }

    private void setNetWork() throws Exception {
        socket = new Socket("127.0.0.1", 5000);
        out = new ObjectOutputStream(socket.getOutputStream());
        in = new ObjectInputStream(socket.getInputStream());
    }
    private void setUI() {
        mButton = new JButton("Send");
        mButton.setFocusable(false);
        mButton.setBackground(Color.WHITE);
        mButton.addActionListener((evt)->{
            System.out.println(2);
            String message = mField.getText().toString();
            System.out.println(message);
            Msg msg = new Msg();
            msg.setSendUser("user1");
            msg.setSendTime(new Timestamp(System.currentTimeMillis()));
            msg.setSendContext(message);
            System.out.println(msg);
            try {
                //为什么第二次发送不出去了?
                out.writeObject(msg);
                out.flush();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
            mField.setText("");
            mField.requestFocus();
        });

        mField = new JTextField(20);

        mArea = new JTextArea(6, 18);
        Font font = new Font("sanserif", Font.BOLD, 24);
        mArea.setFont(font);
        mArea.setLineWrap(true);
        mArea.setWrapStyleWord(true);
        mArea.setEditable(false);

        mScrollPane = new JScrollPane(mArea);
        mScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        mScrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);

        mPane = new JPanel();
        mPane.add(mScrollPane);
        mPane.add(mField);
        mPane.add(mButton);

        mFrame = new JFrame();
        mFrame.getContentPane().add(mPane, BorderLayout.CENTER);
        mFrame.setSize(480, 320);
        mFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        mFrame.setLocationRelativeTo(null);
        mFrame.setVisible(true);

    }
    //运行线程
    @Override
    public void run() {
        try {
            //问题好像是在这里,但是不知道该怎么写,求大神帮忙
            while(true){
                System.out.println(1);
                 Msg msg = (Msg) in.readObject();
                 System.out.println(msg);
            }


        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public static void main(String[] args) throws Exception {
        new Client().run();
    }

}
 package chat;

import java.io.Serializable;
import java.sql.Timestamp;

public class Msg implements Serializable{

    String sendUser;
    String sendContext;
    Timestamp sendTime;
    public String getSendUser() {
        return sendUser;
    }
    public void setSendUser(String sendUser) {
        this.sendUser = sendUser;
    }
    public String getSendContext() {
        return sendContext;
    }
    public void setSendContext(String sendContext) {
        this.sendContext = sendContext;
    }
    public Timestamp getSendTime() {
        return sendTime;
    }
    public void setSendTime(Timestamp sendTime) {
        this.sendTime = sendTime;
    }
    @Override
    public String toString() {
        return "Msg [sendUser=" + sendUser + ", sendContext=" + sendContext + ", sendTime=" + sendTime + "]";
    }



}

程序第一次可以发送pojo
但是第二次就无法发送pojo了
为什么呢?

可以把代码粘到本地运行下,只能发送一次消息,第二次不知道为什么就发不出去了

<无标签>
举报
OscRGY
发帖于3个月前 1回/144阅

client和server的socket只会创建一次,第一次创建的时候,同时创建了一个thread,接收了一个msg,然后这个thread就结束了,再次发送消息的时候,不是发不出去,服务器端没有在从socket中读取数据,就更别说收到返回的消息了。可以考虑把服务器端的接收线程搞成while(true)的形式。

可能你没理解好socket的状态吧。

顶部