多客户端聊天服务器Java

时间:2015-07-10 19:12:50

标签: java multithreading network-programming serversocket

我正在编写一个多客户端聊天服务器。我有一个服务器文件夹,其中包含Server.java和三个客户端文件夹,即client1,client2,client3,包含java文件resp.now当每个客户端加入服务器时我尝试发送文本但是服务器没有选择消息。问题出在void run()try方法中。直到while(true)循环一切正常。

服务器代码:

Chat.java

import java.net.*;
import java.io.*;
import java.util.*;

class Chat implements Runnable {
  Socket skt = null;
  DataInputStream dis = null;
  DataOutputStream dos = null;
  PrintWriter pw = null;
  TreeMap<Socket, String> tm;

  public Chat(Socket skt, TreeMap<Socket, String> tm) {
    this.skt = skt;
    this.tm = tm;
  }

  public void run() {
    try {
      dis = new DataInputStream(skt.getInputStream());
      String msg = "";
      while (true) {
        msg = dis.readUTF();
        Set s = tm.keySet();
        Iterator itr = s.iterator();
        while (itr.hasNext()) {
          String k = (String) itr.next();
          Socket v = (Socket) tm.get(k);
          dos = new DataOutputStream(v.getOutputStream());
          dos.writeUTF();
        }
      }
    } catch (Exception e) {
      System.out.println(e);
    } finally {
      try {
        dis.close();
      } catch (Exception ex) {
        System.out.println(ex);
      }
    }
  }
}

Server.java

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.TreeMap;

class Server
{
    public static void main(String dt[])
    {
        ServerSocket sskt=null;
        Socket skt=null;
        DataInputStream dis=null;
        DataOutputStream dos=null;
        TreeMap <String,Socket>tm=new TreeMap<String,Socket>();

        try
        {
            sskt=new ServerSocket(1234);
            System.out.println("Waiting for Clients");
            while(true)
            {
                skt=sskt.accept();

                dis=new DataInputStream(skt.getInputStream());
                dos=new DataOutputStream(skt.getOutputStream());
                String user=dis.readUTF();

                String pass=dis.readUTF();
                if(user.equals(pass))
                {
                    dos.writeBoolean(true);     
                    tm.put(user,skt);
                    Chat ch=new Chat(skt,tm);
                    Thread t=new Thread(ch);
                    t.start();
                }
                else
                {
                    dos.writeBoolean(false);
                }
            } //end of while.
        }
        catch(Exception e)
        {
            System.out.println(e);
        }
        finally
        {
            try
            {
                dos.close();
                dis.close();
                skt.close();
                sskt.close();
            }
            catch(Exception ex)
            {
                System.out.println(ex);
            }
        }
    }
}

客户代码:

Send.java

import java.io.*;
import java.net.*;

class Send implements Runnable {
  Socket skt = null;

  public Send(Socket skt) {
    this.skt = skt;
    System.out.println(skt);
  }

  public void run() {
    InputStreamReader isrout = null;
    BufferedReader brout = null;
    PrintWriter pw = null;
    DataInputStream dis = null;

    try {
      // Thread.sleep(2000);
      System.out.println("Send a text");
      isrout = new InputStreamReader(System.in);
      brout = new BufferedReader(isrout);
      pw = new PrintWriter(skt.getOutputStream(), true);
      do {
        String msg = brout.readLine();
        pw.println(msg);
      } while (!msg.equals("bye"));
    } catch (Exception e) {
      System.out.println(e);
    } finally {
      try {
        pw.close();
        brout.close();
        isrout.close();
      } catch (Exception ex) {
        System.out.println(ex);
      }
    }
  }
}

Client1.java

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.Socket;

class Client1 {
  public static void main(String dt[]) {
    Socket skt = null;
    InputStreamReader isr = null;
    BufferedReader br = null;
    DataOutputStream dos = null;
    DataInputStream dis = null;

    try {
      skt = new Socket("127.0.0.1", 1234);
      System.out.println("Connected to server");
      System.out.println(skt);

      isr = new InputStreamReader(System.in);
      br = new BufferedReader(isr);
      dos = new DataOutputStream(skt.getOutputStream());
      dis = new DataInputStream(skt.getInputStream());

      System.out.println("Enter a username");
      String user = br.readLine();
      dos.writeUTF(user);
      System.out.println("Enter a password");
      String pass = br.readLine();
      dos.writeUTF(pass);
      if (dis.readBoolean()) {
        System.out.println("User Authenticated");
      } else {
        System.out.println("Incorrect username or password");
      }
      Send sn = new Send(skt);
      Thread t = new Thread(sn);
      t.start();
    } catch (Exception e) {
      System.out.println(e);
    } finally {
      try {
        // skt.close();
      } catch (Exception ex) {
        System.out.println(ex);
      }
    }

  }
}

1 个答案:

答案 0 :(得分:0)

使用输出流写入任何值后,您需要将其刷新以实际发送它。

在使用DataOutputStream的Chat和Server类中,您需要在写入数据后调用它。

dos.flush();

在通过PrintWriter发送数据后的Client类中,您需要调用它。

pw.flush();