Java Socket实现客户端服务画面同步,报错

java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:345)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:589)
at java.net.Socket.connect(Socket.java:538)
at java.net.Socket.(Socket.java:434)
at java.net.Socket.(Socket.java:211)
at Chat.Client.start2(Client.java:59)
at Chat.Client.main(Client.java:83)
客户端代码
package Chat;

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.Socket;

import javax.swing.*;

public class Client {
JFrame frame;
MyPanel mp;
JButton send;
int width;
int height;

public Client() throws AWTException{
    frame =new JFrame("Client");
    frame.setSize(400, 400);
    JPanel pane = new JPanel();
    frame.add(pane,BorderLayout.SOUTH);

    mp=new MyPanel();

    send = new JButton("开始");
    pane.add(send);
    send.addActionListener(new ActionListener(){

        public void actionPerformed(ActionEvent e) {
            // TODO Auto-generated method stub
            width = 40;
            height = 40;
            mp.repaint();
        }

    });


    frame.add(mp,BorderLayout.CENTER);
    Thread t=new Thread(mp);
    t.start();
    frame.setVisible(true);
    frame.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e){
            System.exit(0);
            }
            }); 
}



public void start2(){

    Socket s = null;
    while(true){
        try{
            s = new Socket("127.0.0.1",5000);

            send.setEnabled(true);

            PrintWriter out = new PrintWriter(new OutputStreamWriter(s.getOutputStream()));
            out.println(mp.x);
            out.println(mp.y);
            out.println(mp.width);
            out.println(mp.height);

            mp.repaint();
            out.close();
            s.close();

        //  Thread.sleep(10);
        }catch(Exception e){
            e.printStackTrace();
        }
    }

}
public static void main (String args[]) {
    try {
        Client c = new Client();
        c.start2();
    } catch (AWTException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

}
class MyPanel extends Panel implements Runnable{
int x=0;
int y=0;

int width = 40;
int height = 40;
boolean yAdd = true;
boolean xAdd = true;
public void paint(Graphics g){
    g.setColor(Color.pink);
    g.fillOval(x, y,width ,height);
}
public void run() {
    while(true) {

        if(yAdd)
            y+=2;
        else
            y-=2;
        if(y >= this.getHeight()- height || y <= 0)
            yAdd=!yAdd;
        if(xAdd)
            x+=3;
        else
            x-=3;
        if(x >= this.getWidth()- width || x <= 0)
            xAdd=!xAdd;
        repaint();

        try {
            Thread.sleep(10);
        } catch (InterruptedException e) {
        // TODO Auto-generated catch block
            e.printStackTrace();
        }
        repaint();
    }

}

}
服务器端代码
package Chat;

import java.awt.*;
import java.net.*;
import java.io.*;
import java.awt.event.*;

import javax.swing.*;

public class Server extends JFrame{

JFrame frame;
JPanel pane;

int x=0;
int y=0;
int height = 40;
int width = 40;

public Server(){
//  super("Server");
    frame = new JFrame("Server");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//  pane = new JPanel();
//  frame.setContentPane(pane);
    frame.setVisible(true);
    frame.setSize(400,400);
//  start();
}
private void start(){
    ServerSocket ss = null;
    try{
        ss = new ServerSocket(5000);
        Socket s = ss.accept();

        BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream()));
        String s_x = in.readLine();
        String s_y = in.readLine();
        String s_width = in.readLine();
        String s_height = in.readLine();

        x = Integer.parseInt(s_x);
        y = Integer.parseInt(s_y);
        width = Integer.parseInt(s_width);
        height = Integer.parseInt(s_height);

        Thread.sleep(10);
        repaint();
        in.close();
        s.close();
        ss.close();

    }catch(Exception e){
        e.printStackTrace();
    }
}
public void paint(Graphics g){
    g.setColor(Color.pink);
    g.fillOval(x, y, height,width);
}
public static void main(String[] args){
    Server s = new Server();
    s.start();
}

}

4个回答

服务器端代码 Socket s = ss.accept();

要写个循环不断获取这貌似只能处理一次连接

Client 端已经在循环内将socket关闭,建议finally内关闭流。看你代码的意图感觉是想把客户端的图片在服务端展示,是么?

xcji01
xcji01 可以,不过感觉你思路有点乱,start的button是要控制开始结束?然后server端也需要一个loop一直接受client发送出来的流内容。修改后代码如何给你?
5 年多之前 回复
xcji01
xcji01 可以,不过感觉你思路有点乱,start的button是要控制开始结束?然后server端也需要一个loop一直接受client发送出来的流内容。修改后代码如何给你?
5 年多之前 回复
qq_23217305
qq_23217305 是的,可以帮我改下错吗?新手啦
5 年多之前 回复

可以使用多线程,进行多个客户端连接

要先启动服务器端的程序,如果先启动客户端的就会报错

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐