您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
本篇文章为大家展示了怎么在Java中使用TCP套接字实现一个多人聊天功能,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。
Java主要应用于:1. web开发;2. Android开发;3. 客户端开发;4. 网页开发;5. 企业级应用开发;6. Java大数据开发;7.游戏开发等。
先创建一个服务端:
package interWebWriterEven2; import java.net.ServerSocket; import java.net.Socket; import java.util.ArrayList; /** * 服务端 * @author * */ public class ServiceT { static ArrayList<Socket> list = new ArrayList<Socket>(); public static void main(String[] args) { ServerSocket server; try { server = new ServerSocket(9999); while(true){ Socket socket = server.accept(); System.out.println(socket.getInetAddress().getHostName()+"连接成功。IP:"+socket.getInetAddress().getHostAddress()); list.add(socket); ServiceThreadT t =new ServiceThreadT(socket); t.start(); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
接着是服务端的线程:
package interWebWriterEven2; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileOutputStream; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.net.Socket; /** * 服务端线程 * @author * */ public class ServiceThreadT extends Thread{ private Socket socket; public Socket getSocket() { return socket; } public void setSocket(Socket socket) { this.socket = socket; } public ServiceThreadT(Socket socket) { super(); this.socket = socket; } @Override public void run() { try { @SuppressWarnings("resource") BufferedWriter wFile = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("D://messagesT.txt",true))); BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream())); String strOut = ""; String strIn = ""; while(true){ strIn = socket.getInetAddress().getHostName()+":"+br.readLine(); strOut = strIn; wFile.write(strIn); wFile.newLine(); wFile.flush(); for (Socket soc : ServiceT.list) { if(soc != socket){ BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(soc.getOutputStream())); bw.write(strOut); System.out.println(strOut); bw.newLine(); bw.flush(); } } } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
然后是客户端(应用端):
package interWebWriterEven2; import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.Socket; /** * 客户端 * @author * */ public class ClientT { public static void main(String[] args) { try { Socket socket = new Socket("127.0.0.1",9999); ClientThreadT t = new ClientThreadT(socket); t.start(); BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream())); String strIn = ""; while(true){ strIn = br.readLine(); System.out.println(strIn); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
最后是客户端的线程:
package interWebWriterEven2; import java.io.BufferedWriter; import java.io.IOException; import java.io.OutputStreamWriter; import java.net.Socket; import java.util.Scanner; /** * 客户端的线程 * @author * */ public class ClientThreadT extends Thread{ private Socket socket; public Socket getSocket() { return socket; } public void setSocket(Socket socket) { this.socket = socket; } public ClientThreadT(Socket socket) { super(); this.socket = socket; } @Override public void run() { @SuppressWarnings("resource") Scanner sc = new Scanner(System.in); try { BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())); while(true){ System.out.println(); bw.write(sc.nextLine().trim()); bw.newLine(); bw.flush(); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
上述内容就是怎么在Java中使用TCP套接字实现一个多人聊天功能,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注亿速云行业资讯频道。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。