重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
可以参考一下这段代码 //1. 构造ServerSocket实例,指定服务端口。ServerSocket servSock = new ServerSocket(servPort); while(true){ // 2.调用accept方法,建立和客户端的连接 Socket clntSock = servSock.accept(); SocketAddress clientAddress = clntSock.getRemoteSocketAddress(); System.out.println("Handling client at " + clientAddress); // 3. 获取连接的InputStream,OutputStream来进行数据读写 InputStream in = clntSock.getInputStream(); OutputStream out = clntSock.getOutputStream(); while((recvMsgSize = in.read(receiveBuf)) != -1) { out.write(receiveBuf, 0, recvMsgSize); } // 4.操作结束,关闭socket. clntSock.close();}
创新互联专业为企业提供黔西网站建设、黔西做网站、黔西网站设计、黔西网站制作等企业网站建设、网页设计与制作、黔西企业网站模板建站服务,十年黔西做网站经验,不只是建网站,更提供有价值的思路和整体网络服务。
有两个类,服务器和客户端、
服务器类代码:
package chat;
import java.io.*;
import java.net.*;
import java.util.*;
/*服务器类*/
public class Server {
public ListSocket socketList = new ArrayListSocket();//存放所有连接的客户端的集合
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;isocketList.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();
}
}
现打开服务器类,然后可以打开多个客户端进行聊天
客户端再起一个线程来负责等待不就行了,多线程处理。还有服务端想高效处理客户端消息,建议用NIO机制来处理,否则客户端很快,服务端不能很快处理,也没意义