Java使用TCP套接字实现多人聊天功能详解

网友投稿 259 2022-12-25


Java使用TCP套接字实现多人聊天功能详解

本文实例讲述了java使用TCP套接字实现多人聊天功能。分享给大家供大家参考,具体如下:

废话不多说,直接开搞:

先创建一个服务端:

package interWebWriterEven2;

import java.net.ServerSocket;

import java.net.Socket;

import java.util.ArrayList;

/**

* 服务端

* @author

*

*/

public class ServiceT {

static ArrayList list = 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){

MaDEvuGcCc strIn = br.readLine();

System.out.println(strIn);

}

} catch (Exception e) {

// TODO Auto-generaMaDEvuGcCcted 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相关内容感兴趣的读者可查看本站专题:《Java Socket编程技巧总结》、《Java文件与目录操作技巧汇总》、《Java数据结构与算法教程》、《Java操作DOM节点技巧总结》和《Java缓存操作技巧汇总》

希望本文所述对大家java程序设计有所帮助。


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Java中的Set集合简单汇总解析
下一篇:Java中的可变参数常见用法实例总结
相关文章

 发表评论

暂时没有评论,来抢沙发吧~