package com;
//接收客户端发送消息的线程类
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
class ChatReceive extends Thread{
private Socket socket;
public ChatReceive(Socket socket){
this.socket = socket;
}
@Override
public void run() {
super.run();
}
//实现接收客户端发送的消息
private void receiveMsg(){
BufferedReader nb = null;
try {
nb = new BufferedReader(new InputStreamReader(this.socket.getInputStream()));
while (true){
String msg = nb.readLine();
synchronized ("abc"){
//把读取到的数据写入公共数据区
CharRoomServer.buf="["+this.socket.getInetAddress()+"]"+msg;
//唤醒发送消息的线程对象
"abc".notifyAll();
}
}
}catch (Exception e){
e.printStackTrace();
}finally {
if (nb!=null){
try {
nb.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (this.socket!=null){
try {
this.socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
//xiang客户端发送消息的线程类
class ChatSend extends Thread{
private Socket socket;
public ChatSend(Socket socket){
this.socket = socket;
}
@Override
public void run() {
sendMsg();
}
//将公共数据区的消息发送给客户端
private void sendMsg(){
PrintWriter pw = null;
try {
pw = new PrintWriter(this.socket.getOutputStream());
while (true){
synchronized ("abc"){
//让发送消息的线程处于等待状态
"abc".wait();
//将公共数据区的消息发送给客户端
pw.println(CharRoomServer.buf);
pw.flush();
}
}
}catch (Exception e){
e.printStackTrace();
}finally {
if (pw!=null){
pw.close();
}
if (this.socket!=null){
try {
this.socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
public class CharRoomServer {
//定义公共数据区
public static String buf;
public static void main(String[] args) {
System.out.println("Chat Server Version 1.0");
System.out.println("Listen at 8888");
ServerSocket serverSocket = null;
try {
while (true){
Socket socket = serverSocket.accept();
System.out.println("连接到:"+socket.getInetAddress());
new ChatReceive(socket).start();
new ChatSend(socket).start();
}
}catch (Exception e){
e.printStackTrace();
}finally {
if (serverSocket!=null){
try {
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
"C:\Program Files\Java\jdk-15.0.1\bin\java.exe" -Didea.launcher.port=51224 "-Didea.launcher.bin.path=C:\Program Files\JetBrains\IntelliJ IDEA 2019.3.2\bin" -Dfile.encoding=UTF-8 -classpath "C:\Users\Administrator\IdeaProjects\socketDemo\out\production\socketDemo;C:\Program Files\JetBrains\IntelliJ IDEA 2019.3.2\lib\idea_rt.jar" com.intellij.rt.execution.application.AppMainV2 com.CharRoomServer
Chat Server Version 1.0
Listen at 8888
java.lang.NullPointerException: Cannot invoke "java.net.ServerSocket.accept()" because "serverSocket" is null
at com.CharRoomServer.main(CharRoomServer.java:111)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:64)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:564)
at com.intellij.rt.execution.application.AppMainV2.main(AppMainV2.java:131)
Process finished with exit code 0
找不到解决方法来