服务器
import java.io.*;
import java.net.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Scanner;
class User implements Serializable{
private String name;
private InetAddress id;
private int port;
public User(String name,InetAddress id,int port){
setName(name);
setId(id);
setPort(port);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public InetAddress getId() {
return id;
}
public void setId(InetAddress id) {
this.id = id;
}
public int getPort() {
return port;
}
public void setPort(int port) {
this.port = port;
}
}
//输出线程
class Writer extends Thread{
private ObjectOutputStream oos=null;
private PipedInputStream pis=null;
public Writer(ObjectOutputStream oos){
this.oos=oos;
}
public void run(){
try{
while(true){
pis=new PipedInputStream();
int i=0;
byte b[]=new byte[1024];
int n=0;
for(;(i=pis.read())!=-1;n++){
b[n]=(byte)i;
}
oos.write(b,0,n);
oos.flush();
}
}catch(Exception e){
e.printStackTrace();
}
}
public PipedInputStream getPis() {
return pis;
}
public void setPis(PipedInputStream pis) {
this.pis = pis;
}
}
//输入线程
class Sends extends Thread{
private ObjectInputStream ois=null;
private PipedOutputStream pos=null;
public Sends(ObjectInputStream ois){
this.ois=ois;
}
public void run(){
try{
while(true){
byte b[]=(byte[])ois.readObject();
pos=new PipedOutputStream();
setPos(pos);
pos.write(b);
}
}catch(Exception e){
e.printStackTrace();
}
}
public PipedOutputStream getPos() {
return pos;
}
public void setPos(PipedOutputStream pos) {
this.pos = pos;
}
}
class Reader extends Thread{
private int port=1;
private static int n=0;
public Reader(){}
public void run(){
try{
while(true){ //但n等于一的是后开始执行
for(int i=0;i<=n;i++){
Object o=ChatRoomCase5.ois.get(i).readObject();
if(o!=null){
for(int j=0;j<=n;j++){
int p=(ChatRoomCase5.user.get(j)).getPort();
if(o==(Object)p){//记录客户输入的内容判断是谁的端口号
setOis(ChatRoomCase5.ois.get(i));//这是自己的输入流
setPort(p);//这是别人的端口号
yield();
}
}
}
}
}
}catch(Exception e){
e.printStackTrace();
}
}
ObjectInputStream o=null;
public ObjectInputStream getOis() {
return o;
}
public void setOis(ObjectInputStream ois) {
this.o= ois;
}
public int getPort(){
return this.port;
}
public void setPort(int port){
this.port=port;
}
}
class ChatRoomCase5{
private ServerSocket server=null;
private Socket socket[]=null;
static ArrayList<ObjectInputStream> ois=new ArrayList();
static ArrayList<ObjectOutputStream> oos=new ArrayList();
static ArrayList<User> user=new ArrayList();
private static int i=0;
public ChatRoomCase5(){
try{
server=new ServerSocket(5000,45);
socket=new Socket[45];
while(true){
if(i<45)
{
socket[i]=server.accept();//连接服务器
System.out.println("-------1-------");
ois.add(new ObjectInputStream(new BufferedInputStream(socket[i].getInputStream())));
oos.add(new ObjectOutputStream(socket[i].getOutputStream()));
System.out.println("-------2-------");
user.add((User)ois.get(i).readObject());//服务器获取用户的信息 反序列化
System.out.println("-------3-------");
System.out.println("-------4-------");
System.out.println(user.get(i).getName()+"已经登录;");
String s=""+user.get(i).getName()+"上线了";
if((i>=1)&&(i<45)){
anline(s,socket[i]);//通知各个用户有人已经上线了
sendM();
}
i++;
}
}
}catch(Exception e){
e.printStackTrace();
}
}
public void sendM(){
try{
Reader r=new Reader();
r.start();//等待每个用户输入的第一条信息
if(r.isInterrupted()){
int i=r.getPort();
Object o="你已经和端口号为"+i+"连接上了,可以聊天了";
System.out.println("-----6----"+i);
oos.get(i-5001).writeObject(o);
sender(r.getOis(),i);
sendM();
}
}catch(Exception e){
e.printStackTrace();
}
}//接受每个人发过来的信息
public void sender(ObjectInputStream o,int p){
try{
Writer w=new Writer(oos.get(p-5001));
Sends s=new Sends(o);
s.getPos().connect(w.getPis());
w.start();
s.start();
}catch(Exception e){
e.printStackTrace();
}
}
public void anline(Object o,Socket soc){//向每个人通知自己上线了
try{
for(int i=0;i<45;i++){
if((socket[i]!=null)&&(socket[i]!=soc)){
ObjectOutputStream oos=new ObjectOutputStream(socket[i].getOutputStream());
oos.writeObject(o);
System.out.println("-------5-------");
}else{
continue;
}
}
}catch(Exception e){
e.printStackTrace();
}
}
}
public class ChatRoom5 {
public static void main(String agrs[]){
new ChatRoomCase5();
}
}
客户端
import java.io.BufferedInputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.util.Scanner;
class Send5 extends Thread{
private ObjectOutputStream oos=null;
public Send5(ObjectOutputStream oos){
this.oos=oos;
}
public synchronized void run(){
try{
while(true){
Object o=new Scanner(System.in).next();
oos.writeObject(o);
oos.flush();
}
}catch(Exception e){
e.printStackTrace();
}
}
}
class Read5 extends Thread{
ObjectInputStream ois=null;
public Read5(ObjectInputStream ois){
this.ois=ois;
}
public synchronized void run(){
try{
while(true){
if(ois!=null)
System.out.println(ois.readObject());
}
}catch(Exception e){
e.printStackTrace();
}
}
}
class ClientCase5{
private Socket socket=null;
private ObjectOutputStream oos=null;
private ObjectInputStream ois=null;
User u=null;
public ClientCase5(){
try{
this.socket=new Socket(InetAddress.getLocalHost(),5000,InetAddress.getLocalHost(),5001);
System.out.println("-------1-------");
oos=new ObjectOutputStream(socket.getOutputStream());
ois=new ObjectInputStream(new BufferedInputStream(socket.getInputStream()));
System.out.println("-------2-------");
u=new User("菜鸟莱特",InetAddress.getLocalHost(),5001);
Object o=u;
oos.writeObject(o);
System.out.println("-------3-------");
new Send5(oos).start();
new Read5(ois).start();
}catch(Exception e){
e.printStackTrace();
}
}
}
public class Client50 {
public static void main(String agrs[]){
new ClientCase5();
}
}
3 个解决方案
#1
太长了 帮顶了
#2
谢谢,不长就是,有一个异常java.io.StreamCorruptedException: invalid type code: AC解决不了
#3
出错原因是在online方法取oos的地方,因为你对同一个socket.getOutputStream()做了两次封装,所以出错。
修改方法是从ArrayList<ObjectOutputStream>里取出oos
public void online(Object o, Socket soc) {
try {
for (int i = 0; i < 45; i++) {
if ((socket[i] != null) && (socket[i] != soc)) {
ObjectOutputStream oos = outputList.get(i); // 这边直接取已经封装好的oos
oos.writeObject(o);
logger.info("---------5-------");
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
修改方法是从ArrayList<ObjectOutputStream>里取出oos
public void online(Object o, Socket soc) {
try {
for (int i = 0; i < 45; i++) {
if ((socket[i] != null) && (socket[i] != soc)) {
ObjectOutputStream oos = outputList.get(i); // 这边直接取已经封装好的oos
oos.writeObject(o);
logger.info("---------5-------");
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
#1
太长了 帮顶了
#2
谢谢,不长就是,有一个异常java.io.StreamCorruptedException: invalid type code: AC解决不了
#3
出错原因是在online方法取oos的地方,因为你对同一个socket.getOutputStream()做了两次封装,所以出错。
修改方法是从ArrayList<ObjectOutputStream>里取出oos
public void online(Object o, Socket soc) {
try {
for (int i = 0; i < 45; i++) {
if ((socket[i] != null) && (socket[i] != soc)) {
ObjectOutputStream oos = outputList.get(i); // 这边直接取已经封装好的oos
oos.writeObject(o);
logger.info("---------5-------");
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
修改方法是从ArrayList<ObjectOutputStream>里取出oos
public void online(Object o, Socket soc) {
try {
for (int i = 0; i < 45; i++) {
if ((socket[i] != null) && (socket[i] != soc)) {
ObjectOutputStream oos = outputList.get(i); // 这边直接取已经封装好的oos
oos.writeObject(o);
logger.info("---------5-------");
}
}
} catch (Exception e) {
e.printStackTrace();
}
}