会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 132358个问题

问题: 双向通信的时候, 包没有按照老师一样命名, 一直报错: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 基础深化和提高/网络编程(旧) 3826楼

食物.jpg

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

JAVA 全系列/第二阶段:JAVA 基础深化和提高/容器(旧) 3827楼

——————————————————————————————

老师请问一下,我的代码,没进行线程同步,却怎么也试不出品牌与商品错乱的情况?试了好几次了


UC截图20190308152553.png

package com.jingdong.www;

//商品类
public class Goods {
	//品牌
	private String brand;  //通过赋值,实现多种商品简单点
	private String commodity;
	public String getBrand() {
		return brand;
	}
	public void setBrand(String brand) {
		this.brand = brand;
	}
	public String getCommodity() {
		return commodity;
	}
	public void setCommodity(String commodity) {
		this.commodity = commodity;
	}
	public Goods(String brand, String commodity) {
		super();
		this.brand = brand;
		this.commodity = commodity;
	}
	public Goods() {
		super();
	}
	
	
	
}
package com.jingdong.www;


//生产者类  线程才能同时嘛
public class Producer implements Runnable{
	Goods producer;

	public Producer(Goods producer) {
		super();
		this.producer = producer;
	}

	@Override
	public void run() {
		// TODO 自动生成的方法存根
		for(int i=1;i<=10;i++) {  //生产10次
			if(i%2==0) {//双数生产娃哈哈
				try {
					Thread.sleep(300);
				} catch (InterruptedException e) {
					// TODO 自动生成的 catch 块
					e.printStackTrace();
				}
				producer.setBrand("娃哈哈");
				producer.setCommodity("矿泉水");
			}else {//如果是单数生产旺仔小馒头
				try {
					Thread.sleep(300);
				} catch (InterruptedException e) {
					// TODO 自动生成的 catch 块
					e.printStackTrace();
				}
				producer.setBrand("旺仔");
				producer.setCommodity("小馒头");
			}
			//生产什么打印什么
			System.out.println("生产了     "+producer.getBrand()+""+producer.getCommodity());
		}
		
	}
	
	

}
package com.jingdong.www;

//顾客类
public class Customer implements Runnable{
	//商品实例
	private Goods customer;

	public Customer(Goods customer) {
		super();
		this.customer = customer;
	}

	@Override
	public void run() {
		// TODO 自动生成的方法存根
		for(int i=1;i<=10;i++) {
			try {
				Thread.sleep(300);
			} catch (InterruptedException e) {
				// TODO 自动生成的 catch 块
				e.printStackTrace();
			}
			System.out.println("消费者     "+customer.getBrand()+customer.getCommodity());
		}
	}
	
	
}
package com.jingdong.www;

public class Test {
	public static void main(String[] args) {
		Goods goods=new Goods();
		new Thread(new Producer(goods)).start();
		new Thread(new Customer(goods)).start();
	}
}	


JAVA 全系列/第二阶段:JAVA 基础深化和提高/多线程技术(旧) 3831楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/网络编程(旧) 3832楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

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