java聊天室

来源:互联网 发布:数据挖掘实例分析 编辑:程序博客网 时间:2024/05/20 17:25

服务器端:

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

public class Server extends Thread
{
 int port = 8888;
 ServerSocket serversocket = null;
 protected  DataOutputStream outStream;
 static Scanner sc;
 //构造函数
 public Server(int port){
  try{
   if(port<1024){
    System.out.println("端口号被占用!");
    return ;
   }
   this.port=port;
   serversocket = new ServerSocket(port);
  }catch (Exception e){System.out.println(e.toString());}
 }
 
 //server线程运行函数
 public void run(){
  Socket socket = null;
  try{
   
   //循环接受客户端的信息
   while(true){
    socket = serversocket.accept(); //接受客户端的数据请求
    //System.out.println("test two connect...");
    ServerThread userThread = new ServerThread(socket);
    userThread.start();
    
    //发送数据线程
    Send.insert(socket);
  
   }
  }catch (Exception e){System.out.println(e.toString());}
  finally{
   try{
    socket.close();
    System.exit(1);
   }catch(Exception e){System.out.println(e.toString());}
  }

 }

 public static void main(String[] args){
 //请输入服务器端口号
  sc = new Scanner(System.in);
  System.out.print("请输入端口号: ");
  int port = sc.nextInt();
  Server server = new Server(port);
  System.out.println("Start Server Thread...");
  server.start();
  while(true)
   Send.run(sc.nextLine());
 }


}

//用户发送数据
class Send
{
 protected  static DataOutputStream outStream;
 
 protected static int Count=0;
 protected static Socket[] socket = new Socket[50];
 public Send(){
 }
 public static void insert(Socket sock)
 {
  try{
   socket[Count++]=sock;
  }catch(Exception e){System.out.println(e.toString());}
 }
 //输入发送给客户端的数据

 public static void run(String str){
  int i=0;
  try{
   while(i<Count)
   {
    
    outStream = new DataOutputStream(socket[i].getOutputStream());
    //outStream = new DataOutputStream(socket.getOutputStream());
    outStream.writeUTF(str);
    i++;
    
   }
  }catch (Exception e){System.out.println(e.toString());}
  finally{
   try{
    //socket[i].close();
    //System.exit(1);
   }catch(Exception e){System.out.println(e.toString());}
   }
  }

 public static void Run(String str,String s){
  int i=0;
  try{
   while(i<Count)
   {
    String ss=socket[i].toString();
    //System.out.println(ss+"   "+s);
    if(ss.equals(s)==false)
    {
    outStream = new DataOutputStream(socket[i].getOutputStream());
    //outStream = new DataOutputStream(socket.getOutputStream());
    outStream.writeUTF(str);
    
    }
    i++;
   }
  }catch (Exception e){System.out.println(e.toString());}
  finally{
   try{
    //socket[i].close();
    //System.exit(1);
   }catch(Exception e){System.out.println(e.toString());}
   }
  }

 
}

//接受客户端的信息的线程体
class ServerThread extends Thread
{
 protected DataInputStream inStream;
 protected Socket socket;

 //接受数据的构造函数
 public ServerThread(Socket socket){
  try{
   this.socket = socket;
   inStream = new DataInputStream(socket.getInputStream());
  }catch(Exception e){System.out.println(e.toString());}
 }

 //线程的执行函数
 public void run(){
  
  try{
   while(true)
   {
    String str = null;
    str = inStream.readUTF();
    dataProcess(str);
   }
  }catch(Exception e){System.out.println(e.toString());}
  
 }
 
 //控制数据的输出
 public void dataProcess(String message){
  try{
   System.out.println("客户端发送的信息是: " + message);
   //System.out.println(socket.toString());
   String str = socket.toString();
   Send.Run(message,str);
  }catch(Exception e){System.out.println(e.toString());}
 }
}

 

 

 

客户端:

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

public class Client
{
 int port = 8888;
 String ip = "127.0.0.1";
 Socket socket = null;
 protected DataOutputStream outStream;
 static Scanner sc;

 //构造函数
 public Client(String ip,int port){
  try{
   this.port = port;
   this.ip = ip;
   this.socket = new Socket(ip,port);
   //System.out.println("Client starting ...");
   ClientThread clientthread = new ClientThread(socket);
   clientthread.start();
   
   //不断向服务器发送数据
   while(true){
    try{
    
    String str = sc.nextLine();
    //String str = "怎么搞的";
    outStream = new DataOutputStream(socket.getOutputStream());
    outStream.writeUTF(str);
    }catch(Exception e){System.out.println(e.toString());}
   }

  }catch(Exception e){System.out.println(e.toString());}

 }

 public static void main(String[] args){
  
 //try{
  sc = new Scanner(System.in);
  System.out.print("请输入服务器的IP: ");
  String ip1 = sc.nextLine();
  //String ip1 = "127.0.0.1";
  System.out.print("请输入端口号: ");
  int port = sc.nextInt();
  //int port = 1234;
  Client client = new Client(ip1,port);
  System.out.println("Client starting ...");
  //}catch(Exception e){System.out.println(e.toString());}
 }

}

//循环接受服务器端的信息
class ClientThread extends Thread
{
 protected DataInputStream inStream;
 protected Socket socket;
 //构造函数
 public ClientThread(Socket socket){
  try{
   this.socket = socket;
   inStream = new DataInputStream(socket.getInputStream());
  }catch(Exception e){System.out.println(e.toString());}
 }

 //线程的运行函数
 public void run(){
  try{
   while(true){
    String str = null;
    str = inStream.readUTF();
    dataprocess(str);
   } 
  }catch(Exception e){System.out.println(e.toString());}
  /*finally{
   try{
    socket.close();
    System.exit(1);
   }catch (Exception e){System.out.println(e.toString());}
  }*/
 }

 //处理数据
 public void dataprocess(String message){
  try{
    System.out.println("服务器端发过来的数据是:" + message);
  }catch(Exception e){System.out.println(e.toString());}
 }

}

 

原创粉丝点击