会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 132365个问题
JAVA 全系列/(旧的隐藏)第十一阶段:spring全家桶(Spring Boot)/Spring Boot 38056楼
JAVA 全系列/第三阶段:数据库编程/Oracle 数据库的使用 38057楼
JAVA 全系列/第三阶段:数据库编程/MySQL数据库的使用 38059楼
JAVA 全系列/(旧的隐藏)第十一阶段:spring全家桶(Spring Boot)/Spring Boot 38061楼

问题: 双向通信的时候, 包没有按照老师一样命名, 一直报错:java.net.SocketException: Connection reset


代码如下

1-客户端:


package com.bzsxt.client;


import java.io.DataInputStream;

import java.io.IOException;

import java.io.ObjectOutputStream;

import java.net.Socket;

import java.net.UnknownHostException;

import java.util.Scanner;


public class TestClient {


public static void main(String[] args) throws UnknownHostException, IOException {

// (1)创建Socket对象,用于连接服务器

Socket client = new Socket("localhost", 10001);

// (2)获取输出流 (对象流)

ObjectOutputStream oos = new ObjectOutputStream(client.getOutputStream());

// (3)创建User对象

// 调用获取用户对象的方法

//User u = getUser();// new User("bjsxt", "bjsxt");

User u = new User("bjsxt", "bjsxt");

// (4)User对象发送到服务器

oos.writeObject(u);

// (5)获取输入流(数据流)

DataInputStream dis = new DataInputStream(client.getInputStream());

System.out.println(dis.readUTF());

// (6)关闭流

if (dis != null) {

dis.close();

}

if (oos != null) {

oos.close();

}

if (client != null) {

client.close();

}

}

// 获取用户对象的方法

public static User getUser() {

Scanner input = new Scanner(System.in);

System.out.println("请输入用户名:");

String userName = input.next();

System.out.println("请输入密码:");

String password = input.next();

// 封装成User对象

return new User(userName, password);

}

}

2-实例类:


package com.bzsxt.client;

import java.io.Serializable;


public class User implements Serializable{


/**

*/

private static final long serialVersionUID = 8868213509492563406L;

private String userName;

private String pwd;

public String getUserName() {

return userName;

}

public void setUserName(String userName) {

this.userName = userName;

}

public String getPwd() {

return pwd;

}

public void setPwd(String pwd) {

this.pwd = pwd;

}

@Override

public String toString() {

return "User [userName=" + userName + ", pwd=" + pwd + "]";

}

public User(String userName, String pwd) {

super();

this.userName = userName;

this.pwd = pwd;

}

public User() {

super();

}

}

3-服务器端:


package com.bzsxt.server;


import java.io.DataOutputStream;

import java.io.IOException;

import java.io.ObjectInputStream;

import java.net.ServerSocket;

import java.net.Socket;

import com.bzsxt.entity.User;

public class TestServer {


public static void main(String[] args) throws IOException, ClassNotFoundException {

ServerSocket server = new ServerSocket(10001);

System.out.println("服务端开启");

Socket socket = server.accept();

ObjectInputStream oStream = new ObjectInputStream(socket.getInputStream());

User user  = (User) oStream.readObject();

String data = "";

System.out.println(socket.getInetAddress().getHostAddress()+"请求登录:用户名"+user.getUserName()+"\t密码:"+user.getPwd());

System.out.println("----"+user.getUserName());

System.out.println("----"+user.getPwd());

if ("bzsxt".equals(user.getUserName())&&"bzsxt".equals(user.getPwd())){

data = "登录成功";

}else {

data= "登录失败";

}

DataOutputStream dStream = new DataOutputStream(socket.getOutputStream());

dStream.writeUTF(data);

//socket.shutdownOutput();

if (dStream!=null) {

dStream.close();

}if (oStream!=null) {

oStream.close();

}if (socket!=null) {

socket.close();

}

}

}

4-实体类:


package com.bzsxt.entity;

import java.io.Serializable;

public class User implements Serializable{


/**

*/


private static final long serialVersionUID = 8868213509492563406L;


private String userName;


private String pwd;


public String getUserName() {


return userName;


}


public void setUserName(String userName) {


this.userName = userName;


}


public String getPwd() {


return pwd;


}


public void setPwd(String pwd) {


this.pwd = pwd;


}


@Override


public String toString() {


return "User [userName=" + userName + ", pwd=" + pwd + "]";


}


public User(String userName, String pwd) {


super();


this.userName = userName;


this.pwd = pwd;


}


public User() {


super();


}


}


截图结果:



源码:

client.zip

server.zip


JAVA 全系列/第二阶段:JAVA 基础深化和提高/网络编程(旧) 38062楼

食物.jpg

老师,这个ArrayList 关于add方法的源码分析的拷贝元素的个数问题我没太明白,请看图,帮我解答一下好吗?谢谢老师!

JAVA 全系列/第二阶段:JAVA 基础深化和提高/容器(旧) 38065楼
Python 全系列/第一阶段:Python入门/函数和内存分析 38068楼

pygametimerandom
_display = pygame.display
COLOR_BLACK = pygame.Color()
COLOR_RED = pygame.Color()
version = MainGame():
    window = SCREEN_HEIGHT = SCREEN_WIDTH = TANK_P1 = EnemyTank_list = []
    EnemTank_count = Bullet_list = []
    Enemy_bullet_list = []
    ():
        ():
        _display.init()
        MainGame.window = _display.set_mode([MainGame.SCREEN_WIDTHMainGame.SCREEN_HEIGHT])
        MainGame.TANK_P1 = Tank()
        .creatEnemyTank()
        _display.set_caption(+version)
        :
            MainGame.window.fill(COLOR_BLACK)
            .getEvent()
            MainGame.window.blit(.getTextSurface(%(MainGame.EnemyTank_list))())
            MainGame.TANK_P1.displayTank()
            .blitEnemyTank()
            MainGame.TANK_P1 MainGame.TANK_P1.stop:
                MainGame.TANK_P1.move()
            .blitBullet()
            .blitEnemyBullet()
            time.sleep()
            _display.update()
    ():

        top = speed = random.randint()
        i (MainGame.EnemTank_count):
            left = random.randint()
            eTank = EnemyTank(left*topspeed)
            MainGame.EnemyTank_list.append(eTank)
    ():
        eTank MainGame.EnemyTank_list:
            eTank.displayTank()
            eTank.randMove()
            eBullet = eTank.shot()
            MainGame.Enemy_bullet_list.append(eBullet)
    ():
        bullet MainGame.Bullet_list:
            bullet.live:
                bullet.displayBullet()
                bullet.bulletMove()
            :
                MainGame.Bullet_list.remove(bullet)
    ():
        eBullet MainGame.Enemy_bullet_list:
            eBullet.live:
                eBullet.displayBullet()
                eBullet.bulletMove()
            :
                MainGame.Bullet_list.remove(eBullet)

    ():
        eventList = pygame.event.get()
        event eventList:
            event.type == pygame.QUIT:
                .endGame()
            event.type == pygame.KEYDOWN:
                event.key == pygame.K_LEFT:
                    ()
                    MainGame.TANK_P1.direction = MainGame.TANK_P1.stop = event.key == pygame.K_RIGHT:
                    ()
                    MainGame.TANK_P1.direction = MainGame.TANK_P1.stop = event.key == pygame.K_UP:
                    ()
                    MainGame.TANK_P1.direction = MainGame.TANK_P1.stop = event.key == pygame.K_DOWN:
                    ()
                    MainGame.TANK_P1.direction = MainGame.TANK_P1.stop = event.key == pygame.K_SPACE:
                    ()
                    (MainGame.Bullet_list) < :
                        m = Bullet(MainGame.TANK_P1)
                        MainGame.Bullet_list.append(m)
                    :
                        ()
                    (%(MainGame.Bullet_list))
            event.type == pygame.KEYUP:
                event.key == pygame.K_LEFT event.key == pygame.K_RIGHT event.key == pygame.K_UP event.key == pygame.K_DOWN:
                    MainGame.TANK_P1.stop = (text):
        pygame.font.init()
        font = pygame.font.SysFont()
        textSurface = font.render(textCOLOR_RED)
        textSurface
    ():
        ()
        ()
Tank():
    (lefttop):
        .images = {
            :pygame.image.load():pygame.image.load():pygame.image.load():pygame.image.load()
        }
        .direction = .image = .images[.direction]
        .rect = .image.get_rect()
        .rect.left = left
        .rect.top = top
        .speed = .stop = ():
        .direction == :
            .rect.left > :
                .rect.left -= .speed
        .direction == :
            .rect.left + .rect.height < MainGame.SCREEN_WIDTH:
                .rect.left += .speed
        .direction == :
            .rect.top > :
                .rect.top -= .speed
        .direction == :
            .rect.top + .rect.height < MainGame.SCREEN_HEIGHT:
                .rect.top += .speed
    ():
        Bullet()
    ():
        .image = .images[.direction]
        MainGame.window.blit(.image.rect)
MyTank(Tank):
    ():
        EnemyTank(Tank):
    (lefttopspeed):
        .images = {
            : pygame.image.load(): pygame.image.load(): pygame.image.load(): pygame.image.load()
        }
        .direction = .randDirection()
        .image = .images[.direction]
        .rect = .image.get_rect()
        .rect.left = left
        .rect.top = top
        .speed = speed
        .stop = .step = ():
        num = random.randint()
        num == :
            num == :
            num == :
            num == :
            ():
        .step <= :
            .direction = .randDirection()
            .step = :
            .move()
            .step -= Bullet():
    (tank):
        .image = pygame.image.load()
        .direction = tank.direction
        .rect = .image.get_rect()
        .direction == :
            .rect.left = tank.rect.left + tank.rect.width/- .rect.width/.rect.top = tank.rect.top - .rect.height
        .direction == :
            .rect.left = tank.rect.left + tank.rect.width / - .rect.width / .rect.top = tank.rect.top + tank.rect.height
        .direction == :
            .rect.left = tank.rect.left - .rect.width / - .rect.width / .rect.top = tank.rect.top + tank.rect.width / - .rect.width / .direction == :
            .rect.left = tank.rect.left + tank.rect.width
            .rect.top = tank.rect.top + tank.rect.width / - .rect.width / .speed = .live = ():
        .direction == :
            .rect.top > :
                .rect.top -= .speed
            :
                .live = .direction == :
            .rect.top < MainGame.SCREEN_HEIGHT - .rect.height:
                .rect.top += .speed
            :
                .live = .direction == :
            .rect.left > :
                .rect.left -= .speed
            :
                .live = .direction == :
            .rect.left < MainGame.SCREEN_WIDTH - .rect.width:
                .rect.left += .speed
            :
                .live = ():
        MainGame.window.blit(.image.rect)
Explode():
    ():
        ():
        Wall():
    ():
        ():
        Music():
    ():
        ():
        MainGame().startGame()

检查了好几次,每次到了8分38秒此时效果时候就弹出下面的提示,该怎么处理呢?

image.png

Python 全系列/第二阶段:Python 深入与提高/游戏开发-坦克大战 38069楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

©2014-2025百战汇智(北京)科技有限公司 All Rights Reserved 北京亦庄经济开发区科创十四街 赛蒂国际工业园
网站维护:百战汇智(北京)科技有限公司
京公网安备 11011402011233号    京ICP备18060230号-3    营业执照    经营许可证:京B2-20212637