import java.net.*;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.*;
class GetInforClient implements Runnable
{
Socket socket;
public static boolean ing=true;
SimpleDateFormat format=new SimpleDateFormat("[yyyy-MM-dd]HH:mm:ss ");
GetInforClient(Socket s)throws IOException
{
socket=s;
}
public void run()
{
try
{
BufferedReader is=new BufferedReader(new InputStreamReader(socket.getInputStream()));
while(ing==true)System.out.println(format.format(new Date())+"Server:"+is.readLine());
is.close();
}catch(IOException e){System.out.println("run error getinfor");}
}
}
class SendInforClient implements Runnable
{
Socket socket;
SimpleDateFormat format=new SimpleDateFormat("[yyyy-MM-dd]HH:mm:ss ");
SendInforClient(Socket s)throws IOException
{
socket=s;
}
public void run()
{
try
{
BufferedReader sin=new BufferedReader(new InputStreamReader(System.in));
PrintWriter os=new PrintWriter(socket.getOutputStream());
String line;
line=sin.readLine();
while(!line.equals("bye"))
{
os.println(line);
os.flush();
System.out.println(format.format(new Date())+"Client:"+line);
line=sin.readLine();
}
GetInforClient.ing=false;
os.close();
}catch(IOException e){System.out.println("run error sendinfor");}
}
}
public class ChatClient
{
public static void main(String[] args)
{
try
{
String host="127.0.0.1";
Scanner input=new Scanner(System.in);
System.out.println("choose other host? y/n");
char ch=input.next().charAt(0);
while(!(ch=='y'||ch=='n'))
{
ch=input.next().charAt(0);
}
if(ch=='y')
{
System.out.println("use ip:");
host=input.next();
}
Socket socket=new Socket(host,4700);
System.out.println("The server has been successfully connected to start the conversation!");
Runnable getinfor=new GetInforClient(socket);
Runnable sendinfor=new SendInforClient(socket);
Thread t1=new Thread(getinfor);
Thread t2=new Thread(sendinfor);
t1.start();
t2.start();
//socket.close();
}catch(IOException e){System.out.println("error");}
}
}
java实现的一对一聊天系统
需积分: 41 82 浏览量
2018-09-06
16:08:23
上传
评论
收藏 18KB RAR 举报
silentsaber~
- 粉丝: 13
- 资源: 1
最新资源
- 同态加密python.zip
- 基于Python的PCA人脸识别算法的原理及实现代码详解+源码+详细代码解析+开发文档+数据(毕业设计&课程设计&项目开发)
- Decision tree20240105(1).ipynb
- zuoyezuoyezuoye
- zuoyezuoyezuoye
- 机械设计电机转子装配设备sw22非常好的设计图纸100%好用.zip
- 作业作业作业作业作业作业
- xdotool.c
- RLMD鲁棒性局部均值分解信号分量可视化(Matlab完整源码和数据)
- Screenshot_2024-04-26-17-17-26-36_9d26c6446fd7bb8e41d99b6262b17def.jpg
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈