为什么serversocket每次收到的消息都是一样的

作者: 贝博体育app网络  发布:2019-11-15

合并流SequenceInputStream

 

 

package test;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.SequenceInputStream;

public class Test {  

    public static void main(String[] args) throws IOException {
     InputStream in1=new FileInputStream("d:"+File.separator+"a.txt");
     InputStream in2=new FileInputStream("d:"+File.separator+"b.txt");
     OutputStream out=new FileOutputStream("d:"+File.separator+"ab.txt");
     SequenceInputStream in=new SequenceInputStream(in1,in2);
     int temp=0;
     while((temp=in.read())!=-1){
      out.write(temp);
     }
     in1.close();
     in2.close();
     in.close();
     out.close();
    }


    /**
     * 输出

     */
}

 

package test;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;i...

为什么serversocket每次收到的消息都是一样的
请大家帮忙看看,为什么每次serversocket收到的信息都是一致的,如何解决。谢谢。
用户名1101密码1111
详细代码
package client;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.util.List;
import java.util.Scanner;

import cash.Cashier;
import warehouse.StoreKeeper;
import datas.Datas;
import datas.Goods;
import datas.User;

public class ClientDemo {

public static void main(String[] args) throws IOException { Scanner input = new Scanner(System.in); Socket socket = null; InputStream is = null; OutputStream os = null; ObjectOutputStream oos = null; ObjectInputStream ois = null; User user=null; // 登陆界面 System.out.println("***********欢迎使用超市管理系统***********"); System.out.println; System.out.println; System.out.println("*****************请选择数字1/2*****************"); int n = input.nextInt(); if  { do { socket = new Socket("localhost", 8800); is = socket.getInputStream(); os = socket.getOutputStream(); oos = new ObjectOutputStream; ois = new ObjectInputStream; try { /* * 给用户赋值,用户名和密码 */ user = new User(); System.out.print("请输入用户名:"); user.setName(input.next; System.out.print; user.setPassword(input.next; // 写出对象 oos.writeObject; /* * 读取服务器返回的对象数据 */ Datas datas =  ois.readObject(); String msg = datas.getMsg(); if (msg != null) { System.out.println; } user = datas.getUser(); /* * 判断是否是仓库管理员 */ if (user != null) { if (user.isAdmin { StoreKeeper storeKeeper = new StoreKeeper(); System.out.println("*****欢迎登陆超市库存管理系统*****"); do { storeKeeper.showMenu(); //Goods good=new Goods(); String choise=input.next(); datas.setChoose; oos.writeObject; } while; } else { Cashier cashier = new Cashier(); System.out.println("*****欢迎登陆超市收银系统*****"); cashier.showMenu(); } } } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { ois.close(); oos.close(); is.close(); os.close(); socket.close(); } } while (user == null); } else { System.exit; }}package server;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

public class ServerDemo {

public static void main(String[] args) throws IOException { //服务端 ServerSocket ss=new ServerSocket; while{ Socket socket=ss.accept(); ServerThread st=new ServerThread; st.start(); }}

}
package server;

import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.Socket;

import datas.Datas;
import datas.Goods;
import datas.User;

public class ServerThread extends Thread{
private Socket socket;
//利用构造方法进行初始化
public ServerThread(Socket socket) {
this.socket = socket;
}

public void run() { InputStream is = null; OutputStream os = null; ObjectInputStream ois = null; ObjectOutputStream oos = null; try { ServerBiz biz = new ServerBiz(); is = socket.getInputStream(); os = socket.getOutputStream(); ois = new ObjectInputStream; oos = new ObjectOutputStream; User user =  ois.readObject(); Datas datas = new Datas(); //Goods good=null; user = biz.login; if (user != null) { datas.setUser; // datas.setMsg; } else { datas.setMsg("用户名或密码输入错误!登陆失败!"); } oos.writeObject; while(user!=null){ datas=ois.readObject(); System.out.println(datas.getChoose; //System.out.println(datas.getGood().getGoodId; //oos.writeObject; oos.flush(); } } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { try { oos.close(); ois.close(); os.close(); is.close(); socket.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }}

}

本文由贝博体育app发布于贝博体育app网络,转载请注明出处:为什么serversocket每次收到的消息都是一样的

关键词:

上一篇:c语言中双维数组与指针的那点事儿
下一篇:没有了