所谓可以多客户端,就是需要在服务器端进行多线程编程,每一个客户端对应一个线程。如果要实现时时通讯和话,无论是客户端还是服务器端都要至少两个线程,主线程进行发送信息,子线程进行获取信息,这样可以保证时时通信.
下面贴出我的代码:
客户端:
[java] view plaincopy
- package com.lip;
- import java.io.BufferedReader;
- import java.io.BufferedWriter;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.io.OutputStreamWriter;
- import java.io.PrintWriter;
- import java.net.Socket;
- import java.util.Scanner;
- public class Client
- {
- /**
- * Author: Lip
- * 客户端
- */
- public static void main(String[] args)
- {
- Socket socket = null;
- BufferedReader br = null;
- PrintWriter pw = null;
- Scanner scanner = new Scanner(System.in);// 从键盘读取
- try
- {
- // 创建客户端socket
- socket = new Socket(Server.HOST, Server.PORT);
- // 读取从客户端发来的消息
- br = new BufferedReader(new InputStreamReader(
- socket.getInputStream()));
- // 写入信息到服务器端
- pw = new PrintWriter(
- new BufferedWriter(new OutputStreamWriter(
- socket.getOutputStream())));
- new ReadServerMessage(br);// 从服务器读取消息
- while (true)
- {
- String temp = scanner.nextLine();// 从键盘读取一行
- pw.println(temp);// 写到服务器
- pw.flush();
- if (temp.equals("q"))
- break;
- }
- }
- catch (Exception e)
- {
- e.printStackTrace();
- }
- finally
- {
- try
- {
- System.out.println("close......");
- br.close();
- pw.close();
- socket.close();
- }
- catch (IOException e)
- {
- e.printStackTrace();
- }
- }
- }
- }
- class ReadServerMessage extends Thread//从服务器读取消息
- {
- BufferedReader bReader;
- Socket socket;
- public ReadServerMessage(BufferedReader br)
- {
- this.bReader = br;
- start();
- }
- public void run()
- {
- String str = "";
- while (true)//一直等待着服务器的消息
- {
- try
- {
- str = bReader.readLine();
- if (str.equals("q"))
- {
- bReader.close();
- socket.close();
- break;
- }
- }
- catch (IOException e)
- {
- e.printStackTrace();
- }
- System.out.println("Server Message:" + str);
- }
- }
- }
服务器端:
[java] view plaincopy
- package com.lip;
- import java.io.IOException;
- import java.net.ServerSocket;
- import java.net.Socket;
- public class Server
- {
- /**
- * Author:Lip Desc:SSD8_exercise1
- */
- public static int PORT = 8000;
- public static String HOST = "127.0.0.1";
- public static void main(String[] args)
- {
- ServerSocket serverSocket = null;
- Socket socket = null;
- try
- {
- serverSocket = new ServerSocket(PORT);
- // 等待请求,无请求一直等待
- while (true)
- {
- System.out.println("Waiting Client");
- socket = serverSocket.accept();// 接受请求
- System.out.println("Client Conect!");
- new ThreadedServer(socket);
- }
- }
- catch (Exception e)
- {
- try
- {
- socket.close();
- }
- catch (IOException e1)
- {
- e1.printStackTrace();
- }
- }
- finally
- {
- try
- {
- serverSocket.close();
- }
- catch (IOException e)
- {
- e.printStackTrace();
- }
- }
- }
- }
辅助线程:
[java] view plaincopy
- package com.lip;
- import java.io.BufferedReader;
- import java.io.BufferedWriter;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.io.OutputStreamWriter;
- import java.io.PrintWriter;
- import java.net.Socket;
- import java.util.Scanner;
- public class ThreadedServer extends Thread
- {
- private Socket socket = null;
- private BufferedReader br = null;
- private PrintWriter pw = null;
- private Scanner scanner = null;
- public ThreadedServer(Socket s)
- {
- socket = s;
- try
- {
- br = new BufferedReader(new InputStreamReader(
- socket.getInputStream()));
- pw = new PrintWriter(
- new BufferedWriter(new OutputStreamWriter(
- socket.getOutputStream())), true);
- scanner = new Scanner(System.in);// 从键盘读取数据
- start();
- }
- catch (Exception e)
- {
- e.printStackTrace();
- }
- }
- public void run()
- {
- new ReadClientMessage(br, socket);//读取客户端数据
- while (true)//向客户端发送数据
- {
- try
- {
- pw.println(scanner.nextLine());
- pw.flush();
- }
- catch (Exception e)
- {
- try
- {
- br.close();
- pw.close();
- socket.close();
- }
- catch (IOException e1)
- {
- e1.printStackTrace();
- }
- }
- }
- }
- }
- class ReadClientMessage extends Thread
- {
- BufferedReader bReader;
- Socket socket;
- public ReadClientMessage(BufferedReader br,Socket s)
- {
- this.bReader = br;
- this.socket=s;
- start();
- }
- public void run()
- {
- String str = "";
- while (true)
- {
- try
- {
- str = bReader.readLine();
- if (str.equals("q"))
- {
- bReader.close();
- socket.close();
- break;
- }
- }
- catch (IOException e)
- {
- e.printStackTrace();
- }
- System.out.println("Client Message:" + str);
- }
- }
- }
现在就可以实现多客户端和服务器端通信了 ,不过客户端和客户端的通信暂时还没有搞懂,还要继续去学习
时间: 2024-10-17 02:52:47