重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
今天就跟大家聊聊有关Java中怎么利用TCP套接字实现多人聊天功能,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。
让客户满意是我们工作的目标,不断超越客户的期望值来自于我们对这个行业的热爱。我们立志把好的技术通过有效、简单的方式提供给客户,将通过不懈努力成为客户在信息化领域值得信任、有价值的长期合作伙伴,公司提供的服务项目有:主机域名、雅安服务器托管、营销软件、网站建设、白塔网站维护、网站推广。
先创建一个服务端:
package interWebWriterEven2;import java.net.ServerSocket;import java.net.Socket;import java.util.ArrayList;/** * 服务端 * @author * */public class ServiceT { static ArrayList
接着是服务端的线程:
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套接字实现多人聊天功能有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注创新互联行业资讯频道,感谢大家的支持。