67°

java socket通信:聊天器(1)

目的:实现多个客户之间的通信

首先,这个聊天器的框架是这样的:

 

 对于服务器端:建立socket,连接到服务器,并且开始监听。

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.net.*;
public class MultiTalkServer{
    static int clientnum = 0;
    //创建一个arraylist数组来保存socket,
    public static List <Socket> socketList = new ArrayList <Socket>();
    public static void main(String args[])throws IOException{
        ServerSocket serverSocket = null;
        boolean listening = true;
        try {
            serverSocket= new ServerSocket(4700);
            System.out.println("欢迎来到聊天室。");
        }catch(IOException e) {
            System.out.println("Could not listen on port:4700.");
            System.exit(-1);
        }
        while (listening) {
            clientnum++;
            Socket st = serverSocket.accept();//先创建一个socket
            //此处会阻塞,等待接收
            socketList.add(st);//将这个线程添加到list里
            System.out.println("上线通知: 用户" + clientnum+"上线啦!"); 
            new ServerThread(st,clientnum).start();//再创建一个服务端线程
        }
        serverSocket.close();
    }
}

服务器线程:

import java.io.*;
import java.net.*;
public class ServerThread extends Thread{
    Socket socket = null;//服务器的套接字
    int clientnum;
    String line;
    //将line定义在外面
    public ServerThread(Socket socket,int num){
        this.socket=socket;
        clientnum=num+1;
    }
    public void run() {
        try {
            BufferedReader is = new BufferedReader(new
            InputStreamReader(socket.getInputStream()));//is:从缓存区读入
            
            PrintWriter os = new PrintWriter(socket.getOutputStream());//os:从缓存区输出
            
            BufferedReader sin = new BufferedReader(
                    new InputStreamReader(System.in));//系统标准输入
            
            System.out.println("Client:"+clientnum+is.readLine());//显示从客户端读入的对象,在这里等待客户端输入
            
            line=sin.readLine();
            //前面是初始化
            while(!line.equals("bye")) {
                os.println(line);//向客户端输出该字符串
                os.flush();//刷新,让客户端接收到
                System.out.println("Server:"+line);//显示服务端读入的字符
                System.out.println("Client:"+clientnum+is.readLine());//再次从客户端读入字符串
                line=sin.readLine();//从服务端读入字符
            }
            os.close();
            is.close();
            socket.close();
    }</span><span style="color: #0000ff;">catch</span><span style="color: #000000;">(Exception e) {
        System.out.println(</span>"Error:"+<span style="color: #000000;">e);
    }
}

}

客户端:

import java.net.*;
import java.io.*;
public class TalkClient{
</span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> main(String args[]){
    </span><span style="color: #0000ff;">try</span><span style="color: #000000;"> {
        Socket socket </span>= <span style="color: #0000ff;">new</span> Socket("127.0.0.1",4700<span style="color: #000000;">);
        System.out.print(</span>"已连接成功,"<span style="color: #000000;">);
        </span><span style="color: #0000ff;">new</span> Thread(<span style="color: #0000ff;">new</span><span style="color: #000000;"> ClientThread(socket)).start();
        </span><span style="color: #0000ff;">new</span> Thread(<span style="color: #0000ff;">new</span><span style="color: #000000;"> ClientThread2(socket)).start();
    }</span><span style="color: #0000ff;">catch</span><span style="color: #000000;">(Exception e) {
        System.out.println(</span>"Error"+<span style="color: #000000;">e);
    }

} }

客户端线程1:

import java.io.*;
import java.net.*;
public class ClientThread extends Thread{
    Socket socket;
    String line;
    public ClientThread(Socket socket) {
        this.socket = socket;
}
</span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> run(){
    </span><span style="color: #0000ff;">try</span><span style="color: #000000;"> {
        BufferedReader is </span>= <span style="color: #0000ff;">new</span> BufferedReader(<span style="color: #0000ff;">new</span><span style="color: #000000;"> InputStreamReader(System.in));
        PrintWriter out </span>= <span style="color: #0000ff;">new</span><span style="color: #000000;"> PrintWriter(socket.getOutputStream());
        </span><span style="color: #0000ff;">while</span>(<span style="color: #0000ff;">true</span><span style="color: #000000;">) {
            line </span>=<span style="color: #000000;"> is.readLine();
            out.println(line); </span><span style="color: #008000;">//</span><span style="color: #008000;">向服务器输入;</span>

out.flush(); } }catch(Exception e){ System.out.println("Error:"+e); } } }

客户端线程2:

import java.io.*;
import java.net.*;
public class ClientThread2 extends Thread{
    Socket socket;
</span><span style="color: #0000ff;">public</span><span style="color: #000000;"> ClientThread2(Socket socket) {
    </span><span style="color: #0000ff;">this</span>.socket =<span style="color: #000000;"> socket;
}
</span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> run(){
    </span><span style="color: #0000ff;">try</span><span style="color: #000000;"> {
        BufferedReader in </span>= <span style="color: #0000ff;">new</span> BufferedReader(<span style="color: #0000ff;">new</span><span style="color: #000000;"> InputStreamReader(socket.getInputStream()));
        </span><span style="color: #0000ff;">while</span>(<span style="color: #0000ff;">true</span><span style="color: #000000;">) {
            String line </span>=<span style="color: #000000;"> in.readLine();
            System.out.println(line); 
        }
    }</span><span style="color: #0000ff;">catch</span><span style="color: #000000;">(Exception e){
        System.out.println(</span>"Error:"+<span style="color: #000000;">e);
    }
}

}

展示结果:

 

 

 

 

 

本文转载自博客园,原文链接:https://www.cnblogs.com/rannianyouyu/p/11826606.html

全部评论: 0

    我有话说: