java 控制台聊天昵称_简单的java控制台聊天室实现

这篇博客介绍了如何使用Java进行简单的socket编程和多线程技术,实现一个控制台聊天室。服务端接收客户端连接,存储客户端信息,并在客户端间建立通信。客户端输入对方IP地址后,可以开始聊天,直到输入'exit'退出。更多内容可访问作者的优快云博客。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

简单的socket编程+多线程;更多东西访问俺的博客啦啦啦,顺便求offer

http://blog.youkuaiyun.com/leejuen/

1.[代码]服务端

import java.io.IOException;

import java.io.PrintStream;

import java.net.ServerSocket;

import java.net.Socket;

import java.util.HashMap;

import java.util.Scanner;

public class ServerIM{

public static HashMap clientMap = new HashMap();

public static void main(String[] args) throws Exception

{

ServerSocket server = new ServerSocket(8888);

Socket client = null;

while(true)

{

System.out.println("等待客户端连接");

client = server.accept();

clientMap.put(client.getInetAddress().getHostAddress(), client);

new Thread(new ServerThread(client)).start();

}

}

}

class ServerThread implements Runnable

{

private Socket client = null;

private Socket client2 = null;

private String address2 = null;

public ServerThread(Socket client)

{

this.client = client;

}

public void run(){

// TODO Auto-generated method stub

try {

Scanner in = new Scanner(client.getInputStream());

PrintStream out = new PrintStream(client.getOutputStream());

System.out.println("客户端"+client.getInetAddress().getHostAddress()+"已经连接");

out.println("欢迎你是第"+ServerIM.clientMap.size()+"用户");

out.flush();

address2 = in.nextLine();

client2 = ServerIM.clientMap.get(address2);

if(client2==null)

{

out.println("你的小伙伴还没有上线请耐心等待");

while(client2 == null)

{

address2 = in.nextLine();

client2 = ServerIM.clientMap.get(address2);

}

}

out.println("您已成功和你的小伙伴连上线了你们可以开始通话了");

PrintStream out2 = new PrintStream(client2.getOutputStream());

while(true)

{

String call = in.nextLine();

System.out.println(client.getInetAddress().getHostAddress()+":"+call);

if("exit".equals(call)) break;

out2.println(client.getInetAddress().getHostAddress()+":"+call);

}

out2.println("再见");

System.out.println("客户端"+client.getInetAddress().getHostAddress()+"已经断开");

client.close();

client2.close();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

2.[代码]客户端

import java.io.PrintStream;

import java.net.Socket;

import java.util.Scanner;

public class ClientIM {

public static void main(String[] args) throws Exception

{

//输入服务端主机ip地址

Socket client = new Socket("10.68.210.4",8888);

//发送数据给服务端流

PrintStream toServer = new PrintStream(client.getOutputStream());

PrintStream sout = new PrintStream(System.out);

//获取服务端数据流

Scanner getServer = new Scanner(client.getInputStream());

//控制台输入流

Scanner sin = new Scanner(System.in);

System.out.println("请输入你要通信的小伙伴的ip地址:");

/* 下面两句话目的是实现输入和刷新实时的作用,使得控制台输入不会阻塞*/

new Thread(new ClientScanner(sin, toServer)).start();

new Thread(new ClientScanner(getServer, sout)).start();

}

}

class ClientScanner implements Runnable

{

Scanner in;

PrintStream toServer;

public ClientScanner(Scanner in,PrintStream toServer)

{

this.in = in;

this.toServer = toServer;

}

public void run() {

// TODO Auto-generated method stub

while(in.hasNext())

{

toServer.println(in.nextLine());

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值