求一个用java编写的套接字实现类似于QQ对话的程序,急用 10
2个回答
展开全部
有两个类,服务器和客户端、
服务器类代码:
package chat;
import java.io.*;
import java.net.*;
import java.util.*;
/*服务器类*/
public class Server {
public List<Socket> socketList = new ArrayList<Socket>庆旅();//存放所有连接的客户端的集合
public ServerSocket server;//服务器
public int portNum;//端口号
public Server(int portNum){
this.portNum = portNum;
}
public void innit(){
try {
server = new ServerSocket(portNum);
System.out.println("服务器开启成伍薯功!");
int socketNum = 0;
while(true){
Socket socket = server.accept();//被动等待客户端的连接
socketNum++;
System.out.println("第"+socketNum+"个客户端连接成功!!");
socketList.add(socket);//连接的客户端存放到集合里面
new RWThread(socket).start();
}
} catch (IOException e) {
e.printStackTrace();
}
}
class RWThread extends Thread{//接收和发送消息的线程
public Socket socket;
public RWThread(Socket socket){
this.socket = socket;
}
public void run() {
super.run();
try {
BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
while(true){
String message = br.readLine();
System.out.println(message);//接收客户端发来的消息
for(int i=0;i<socketList.size();i++){//发送给所有连接的客户端
PrintWriter pw = new PrintWriter(new OutputStreamWriter(socketList.get(i).getOutputStream()));
pw.println(message);
pw.flush();
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
int portNum = 8888;//创建服务器的端口号
Server server = new Server(portNum);
server.innit();
}
}
客户端类:
package chat;
import java.io.*;
import java.net.*;
import java.util.*;
/*客户端类*/
public class Client {
public Socket socket;
public Client(){
Scanner sca = new Scanner(System.in);
try {
socket = new Socket("127.0.0.1",8888);//创建客户端
new ReadThread(socket).start();//开启读取从服务器端发腔差者来的信息
while(true){
PrintWriter pw = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()));
String message = sca.nextLine();
pw.println(message);//向服务器发送信息
pw.flush();
}
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
System.out.println("错误:服务器未开启!!!");
}
}
class ReadThread extends Thread{//读取服务器发来信息的线程
public Socket socket;
public ReadThread(Socket socket){
this.socket = socket;
}
public void run() {
super.run();
try {
BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
while(true){
String message = br.readLine();
System.out.println(message);//输出信息
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
Client client = new Client();
}
}
现打开服务器类,然后可以打开多个客户端进行聊天
服务器类代码:
package chat;
import java.io.*;
import java.net.*;
import java.util.*;
/*服务器类*/
public class Server {
public List<Socket> socketList = new ArrayList<Socket>庆旅();//存放所有连接的客户端的集合
public ServerSocket server;//服务器
public int portNum;//端口号
public Server(int portNum){
this.portNum = portNum;
}
public void innit(){
try {
server = new ServerSocket(portNum);
System.out.println("服务器开启成伍薯功!");
int socketNum = 0;
while(true){
Socket socket = server.accept();//被动等待客户端的连接
socketNum++;
System.out.println("第"+socketNum+"个客户端连接成功!!");
socketList.add(socket);//连接的客户端存放到集合里面
new RWThread(socket).start();
}
} catch (IOException e) {
e.printStackTrace();
}
}
class RWThread extends Thread{//接收和发送消息的线程
public Socket socket;
public RWThread(Socket socket){
this.socket = socket;
}
public void run() {
super.run();
try {
BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
while(true){
String message = br.readLine();
System.out.println(message);//接收客户端发来的消息
for(int i=0;i<socketList.size();i++){//发送给所有连接的客户端
PrintWriter pw = new PrintWriter(new OutputStreamWriter(socketList.get(i).getOutputStream()));
pw.println(message);
pw.flush();
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
int portNum = 8888;//创建服务器的端口号
Server server = new Server(portNum);
server.innit();
}
}
客户端类:
package chat;
import java.io.*;
import java.net.*;
import java.util.*;
/*客户端类*/
public class Client {
public Socket socket;
public Client(){
Scanner sca = new Scanner(System.in);
try {
socket = new Socket("127.0.0.1",8888);//创建客户端
new ReadThread(socket).start();//开启读取从服务器端发腔差者来的信息
while(true){
PrintWriter pw = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()));
String message = sca.nextLine();
pw.println(message);//向服务器发送信息
pw.flush();
}
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
System.out.println("错误:服务器未开启!!!");
}
}
class ReadThread extends Thread{//读取服务器发来信息的线程
public Socket socket;
public ReadThread(Socket socket){
this.socket = socket;
}
public void run() {
super.run();
try {
BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
while(true){
String message = br.readLine();
System.out.println(message);//输出信息
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
Client client = new Client();
}
}
现打开服务器类,然后可以打开多个客户端进行聊天
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询