怎么在Java中使用TCP套接字实现一个多人聊天功能
本篇文章为大家展示了怎么在Java中使用TCP套接字实现一个多人聊天功能,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。
创新互联公司主要从事做网站、成都做网站、网页设计、企业做网站、公司建网站等业务。立足成都服务新昌,十余年网站建设经验,价格优惠、服务专业,欢迎来电咨询建站服务:18980820575
Java可以用来干什么
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 ArrayListlist = new ArrayList (); 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套接字实现一个多人聊天功能,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注创新互联行业资讯频道。
网站标题:怎么在Java中使用TCP套接字实现一个多人聊天功能
本文链接:http://azwzsj.com/article/pjdjhi.html