全文在IDEA编译器基础上完成。
在pom.xml文件中导入以下依赖
<dependencies> <dependency> <groupId>org.java-websocket</groupId> <artifactId>Java-WebSocket</artifactId> <version>1.5.1</version> </dependency> </dependencies>
创建ChatServer.java文件
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.InetSocketAddress; import java.net.UnknownHostException; import java.nio.ByteBuffer; import java.util.Collections; import org.java_websocket.WebSocket; import org.java_websocket.drafts.Draft; import org.java_websocket.drafts.Draft_6455; import org.java_websocket.handshake.ClientHandshake; import org.java_websocket.server.WebSocketServer; public class ChatServer extends WebSocketServer { public ChatServer(int port) throws UnknownHostException { super(new InetSocketAddress(port)); } public ChatServer(InetSocketAddress address) { super(address); } public ChatServer(int port, Draft_6455 draft) { super(new InetSocketAddress(port), Collections.<Draft>singletonList(draft)); } @Override public void onOpen(WebSocket conn, ClientHandshake handshake) {//客户端连接时调用 conn.send("Welcome to the server!"); broadcast("new connection: " + handshake.getResourceDescriptor()); //This method sends a message to all clients connected System.out.println( conn.getRemoteSocketAddress().getAddress().getHostAddress() + " entered the room!"); } @Override public void onClose(WebSocket conn, int code, String reason, boolean remote) {//客户端断开连接时调用 broadcast(conn + " has left the room!"); System.out.println(conn + " has left the room!"); } @Override public void onMessage(WebSocket conn, String message) {//收到客户端发送的消息时调用 broadcast(message); System.out.println(conn + ": " + message); } @Override public void onMessage(WebSocket conn, ByteBuffer message) { broadcast(message.array()); System.out.println(conn + ": " + message); } public static void main(String[] args) throws InterruptedException, IOException { int port = 8887; // 服务器所使用的端口号 try { port = Integer.parseInt(args[0]); } catch (Exception ex) { } ChatServer s = new ChatServer(port); s.start(); System.out.println("ChatServer started on port: " + s.getPort()); BufferedReader sysin = new BufferedReader(new InputStreamReader(System.in)); while (true) { String in = sysin.readLine(); s.broadcast(in); if (in.equals("exit")) { s.stop(1000); break; } } } @Override public void one rror(WebSocket conn, Exception ex) { ex.printStackTrace(); if (conn != null) { // some errors like port binding failed may not be assignable to a specific websocket } } @Override public void onStart() { System.out.println("Server started!"); setConnectionLostTimeout(0); setConnectionLostTimeout(100); } }
InetAddress addr = InetAddress.getLocalHost(); System.out.println("Local HostAddress: "+addr.getHostAddress());
java的获取IP地址方法可能获取到的是192.168的本地IP,需要在cmd中输入ipconfig来查看本机IP。另外 百度到的IP地址是不可靠的!!我之前获取自己的IP一直是百度输入IP,就能查到自己的IP地址。
这个错误真的很低级,但是就是出现过。我自己写的服务器放别人电脑上,其他人死都连不上服务器,但是本机就能连上,后来发现是他IDEA防火墙设置原因不能连上外网。
这个看学校的设置,我们学校的校园网如果不是同一个接入点就没办法互相连接。解决方法就是客户端和服务器同时连接手机热点。