吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 5016|回复: 8
收起左侧

[Java 转载] 类似QQ聊天功能怎么用Java实现的啊???求大神啊

[复制链接]
bounds 发表于 2017-8-11 16:49
聊天实现客户端与客户端之间的通信,怎么通过服务器中转站的???
这些都怎么实现的?求大神指导!!!

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

wozhishixiaxian 发表于 2017-8-11 17:10
http://layim.layui.com/ 貌似不错
苏格拉的底 发表于 2017-8-11 17:10
//服务器端程序
import java.net.*;
import java.io.*;
public class serverSocket
{ static public void main(String args[])  throws IOException   
{ //构造ServerSocket对象,端口为3000
  ServerSocket serversoc=new ServerSocket(3000);
  System.out.println("在等待一个连接...");
  Socket soc=serversoc.accept();     //服务端等待一个连接,返回新套接口soc
   //在新套接口soc上构造输入流in、输出流out
  BufferedReader in=new BufferedReader(new InputStreamReader(soc.getInputStream()));
  PrintWriter out= new PrintWriter (soc.getOutputStream(),true);
  InetAddress  clientIP=soc.getInetAddress();          //得到客户端IP地址
  System.out.println("连接到一个客户,IP地址是:"+clientIP);
  out.println("欢迎!");                            //向客户端发出表示接通的信息
  String clientStr=in.readLine();                       //读取客户端的一行信息   
  while(!clientStr.equals("Bye!"))                      //与客户端通信的循环
  { System.out.println("客户端说:"+clientStr);
   out.println("接收到您的信息——"+ clientStr);        //向客户端回应接到的信息
   clientStr=in.readLine();
  }
  System.out.println("客户端说:"+clientStr);       //客户端的最后一句话
  System.out.println("客户端已经离去。");
  in.close();                                   //关闭输入流
  out.close();                                  //关闭输出流
  soc.close();                                  //关闭soc
  serversoc.close();                             //关闭serversoc
}
}  //服务器端程序结束

//客户器端程序
import java.net.*;
import java.io.*;
public class clientSocket
{static public void main(String args[])  throws IOException   
{ BufferedReader  In = new BufferedReader(new  InputStreamReader(System.in));
   //构造Socket对象, 指向本机, 端口3000
   Socket soc=new Socket("localhost",3000);
   System.out.println("连接到服务器...");
   BufferedReader serin = new BufferedReader(new
                 InputStreamReader(soc.getInputStream()));       //建输入流
   PrintWriter out = new PrintWriter(new   
         OutputStreamWriter(soc.getOutputStream()),true);        //建输出流
   String serverStr=serin.readLine();                 //读取一行服务器端来的信息
  System.out.println("服务器端说:"+serverStr);  
  String outServer=In.readLine();               //从键盘输入一行要发给服务器的文字
  while(!outServer.equals("Bye!"))              //与服务器通信的循环
  { out.println(outServer);                     //发一条信息给服务器端
    serverStr=serin.readLine();                 //读取一行服务器端来的信息
    System.out.println("服务器端说:"+serverStr);
    outServer=In.readLine();              //从键盘输入一行要发给服务器端的文字
  }
  out.println(outServer);                //发最后一条信息(Bye!)给服务器端
  serin.close();
  out.close();
  soc.close();
}
}  //客户器端程序结束

小样丶站住 发表于 2017-8-11 17:29
 楼主| bounds 发表于 2017-8-11 20:57
苏格拉的底 发表于 2017-8-11 17:10
//服务器端程序
import java.net.*;
import java.io.*;

真心感谢
 楼主| bounds 发表于 2017-8-11 20:58
小样丶站住 发表于 2017-8-11 17:29
JSR标准去看看 就懂了

自学者有点不懂
 楼主| bounds 发表于 2017-8-11 21:27
wozhishixiaxian 发表于 2017-8-11 17:10
http://layim.layui.com/ 貌似不错

收费的啊,用不了
小样丶站住 发表于 2017-8-11 21:31
bounds 发表于 2017-8-11 20:58
自学者有点不懂

就是一套标准啊    websocket 编程
GH逐风 发表于 2017-8-21 20:24
一般用DatagramSorkt,DategramPact
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止灌水或回复与主题无关内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-4-19 20:01

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表