java实现简易聊天室

废话不多说直接上代码

server

import java.net.*;
import java.io.*;
import java.util.*;
/*
注意用GBK编码方式
*/
public class main{
    public static void main(String[] args) {
        try(ServerSocket serverall = new ServerSocket()){
            System.out.print("输入启动端口: ");
            Scanner scanforadd = new Scanner(System.in);
            int port = scanforadd.nextInt();
            serverall.bind(new InetSocketAddress(port));
            System.out.println("服务器已启动!");
            while (true) {
                new Thread(new THforrun(serverall.accept())).start();
            }
        } catch (IOException e) {
            System.out.println("启动服务器时遇到一些问题,请换个端口试试");
        }
    }
    private static class THforrun implements Runnable{
        private Socket socket = null;
        public THforrun(Socket socket){
            this.socket = socket;
        }
        @Override
        public void run() {
            try(
                ObjectInputStream i = new ObjectInputStream(socket.getInputStream());
                ObjectOutputStream o = new ObjectOutputStream(socket.getOutputStream())
                ) {
                String name = i.readUTF();
                System.out.println(name + "来了");
                o.writeUTF("你好" + name);
                o.flush();
                Scanner scan = new Scanner(System.in);
                String letter = null;
                while(true) {
                    try {
                        System.out.println(name + ": " + i.readUTF());
                        System.out.print("你: ");
                        letter = scan.nextLine();
                        o.writeUTF(letter);
                        o.flush();    
                    }catch(Exception e) {
                        System.out.println("输入时遇到一些问题");
                    }
                }    
            } catch (IOException e) {
                System.out.println("objectstream has some problem");
            }
        }
    }
}

client

import java.net.*;
import java.io.*;
import java.util.*;
public class main{
    public static void main(String[] args) {
        Socket s = null;
        ObjectInputStream i = null;
        ObjectOutputStream o = null;
        Scanner scanforadd = new Scanner(System.in);
        // System.out.println("Do ypu want to keep the chat history?(1-->yes)(2-->no)");
        // int yn = scanforadd.nextInt();
        System.out.print("输入服务器IP: ");
        String hostname = scanforadd.nextLine();
        System.out.print("输入服务器端口: ");
        int port = scanforadd.nextInt();
        InetSocketAddress a = new InetSocketAddress(hostname, port);
        s = new Socket();
        try {
            s.connect(a);
            o = new ObjectOutputStream(s.getOutputStream());
            i = new ObjectInputStream(s.getInputStream());
            System.out.print("输入你的名字: ");
            Scanner scanforname = new Scanner(System.in); 
            o.writeUTF(scanforname.nextLine());
            o.flush();
            System.out.println("服务器: " + i.readUTF());
            Scanner scan = new Scanner(System.in);
            String letter = null;
            while(true) {
                try {
                    System.out.print("你: ");
                    letter = scan.nextLine();
                    o.writeUTF(letter);
                    o.flush();
                    System.out.println("服务器: " + i.readUTF());
                }catch(Exception e) {
                    System.out.println("输入时遇到一些问题");
                }
            }    
        } catch (IOException e) {
            System.out.println("连接时遇到问题");
        }
    }
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值