Java网络编程输出有问题

客户端代码
import java.io.*;
import java.net.Socket;

/**

  • Created by apple on 17-6-7.
  • 多线程版本
  • 让程序能够支持客户端一边输入一边接收来自服务端的消息 */ public class TCPClient2 { //testClient public static void main(String[] args){ Client sparkClient = new Client("Spark", "127.0.0.1", 8888); } }


class Client{
private static int totalNum = 0; //记录连接的客户端总数量
private Socket clientSock;
private String userName; //客户端用户名
private final int ID = ++totalNum; //标识客户端唯一的号码,类似QQ号,且一经注册变不可改变
private BufferedReader clientReader; //客户端读入流
private BufferedReader userInputReader;
private PrintWriter clientWriter; //客户端向服务端的输出流
private String clientInput; //客户端读入的数据,即从服务端传来的数据
private String userInput;
private Thread inputThread = new Thread(new ClientRead()); //监控输入的线程
private Thread outputThread = new Thread(new ClientWrite()); //监控输出的线程

/*public Client(String userName, Socket sock){
    //this(userName, (String)sock.getInetAddress(), sock.getPort());
    //??????????
    clientSock = sock;
}*/

public Client(String userName, String ipAddress, int port){
    try{
        //id = ++totalNum;
        clientSock = new Socket(ipAddress, port);
        this.userName = userName;
        clientReader = new BufferedReader(new InputStreamReader(clientSock.getInputStream()));
        clientWriter = new PrintWriter(clientSock.getOutputStream(), true);
        userInputReader = new BufferedReader(new InputStreamReader(System.in));
        inputThread.start();
        outputThread.start();
        System.out.println("Connected, just enjoy the communication!");
    } catch (IOException e){
        e.printStackTrace();
        System.exit(-1);
    }
}

public Client(){
    this("Durant", "127.0.0.1", 8888);
    //this((Socket) ("Spark","127.0.0.1", 8888));
}         //尝试随机产生类似QQ号的号码,用户名可以重复

protected void finalize(){close();}

//getter
public Socket getSocket(){
    return clientSock;
}
public int getID (){return ID;}
public String getUserName(){return userName;}
//public String getClientInput(){return clientInput;}
//public BufferedReader getClientReader(){return clientReader;}
//public PrintWriter getClientOutput(){return clientWriter;}
public String getUserInput(){       //客户端接受用户的输入作为写入服务端的信息
    //inputHint();
    try{
        userInput = userInputReader.readLine();
    }catch (IOException e){
        e.printStackTrace();
        System.exit(-1);
    }
    return userInput;
}

//setter
public void setSocket(Socket sock){
    clientSock = sock;
}
public void setUserName(String userName){this.userName = userName;}


//从客户端读入来自服务端或用户写入的数据
public String read(){
    try {
        clientInput = clientReader.readLine();   //将服务端写入的数据读入进来
    }catch (IOException e){
        e.printStackTrace();
        System.exit(-1);
    }
    return clientInput;
}

//从客户端写入数据到服务端
public void write(){
    getUserInput();
    clientWriter.println(userInput);
    clientWriter.flush();
    //  clientOutput.flush();
}

//将服务端传来的消息打印出来
public void printServerMessage(){
    System.out.println("Server:" + clientInput);
}

public void printClientMessage(){
    System.out.println("Client:" + userInput);
}

public void printServerDisconnected() { System.out.println("Sorry, the server is disconnected!"); }
public void printClientDisconnected() { clientWriter.println("User#" + ID + ", " + userName + "is disconnected!"); }

public void close(){
    try{
        clientSock.close();
        clientReader.close();
        userInputReader.close();
        clientWriter.close();
    }catch (IOException e){
        e.printStackTrace();
        System.exit(-1);
    }
}

//判断服务器是否仍在连接中
public boolean serverIsConnected(){
    return !clientInput.equals("bye");
}

public boolean clientIsConnected(){
    return !getUserInput().equals("bye");
}

//实现线程的方法————时刻准备接收来自服务端的消息
public class ClientRead implements Runnable {
    public void run() {
        read();
        while (clientReader != null && serverIsConnected() && clientInput.length()!=0)  {
            printServerMessage();
            read();
        }
        printServerDisconnected();
    }
}

//实现线程的方法————时刻准备向服务端发送消息
class ClientWrite implements Runnable{
    public void run() {
        write();
        while (userInputReader != null && clientIsConnected() && userInput.length()!=0){
            //printClientMessage();
            write();
        }
        printClientDisconnected();

    }
}

}

服务端代码:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

/**

  • Created by apple on 17-6-7.
  • 多线程版本
  • 让程序能够支持客户端一边输入一边接收来自服务端的消息 */

public class TCPServer {
public static void main(String[] args){
Server sparkServer = new Server("Curry", 8888);
}
}

class Server{
private ServerSocket serverSock;
private Socket clientSock;
private String serverName; //服务端名
private BufferedReader serverReader; //服务端读入流
private BufferedReader userInputReader;
private PrintWriter serverWriter; //服务端向客户端的输出流
private String serverInput; //服务端读入的数据,即从客户端传来的数据
private String userInput;
private Thread inputThread = new Thread(new ServerRead()); //监控输入的线程
private Thread outputThread = new Thread(new ServerWrite()); //监控输出的线程

public Server(String serverName, int port){
    try{
        this.serverName = serverName;
        serverSock = new ServerSocket(port);
        clientSock = serverSock.accept();
        serverReader = new BufferedReader(new InputStreamReader(clientSock.getInputStream()));
        userInputReader = new BufferedReader(new InputStreamReader(System.in));
        serverWriter = new PrintWriter(clientSock.getOutputStream(), true);
        inputThread.start();
        outputThread.start();
    }catch (IOException e){
        e.printStackTrace();
        System.exit(-1);
    }
}

public Server(){
    this("SuperQQ", 8888);
}

protected void finalize(){close();}

//getter
public ServerSocket getServerSocket(){
    return serverSock;
}
public String getServerName(){return serverName;}
public String getUserInput(){
    try{
        userInput = userInputReader.readLine();
    }catch (IOException e){
        e.printStackTrace();
        System.exit(-1);
    }
    return userInput;
}

//setter
public void setServerSocket(ServerSocket serverSock){
    this.serverSock = serverSock;
}

public void setServerSocket(int port){
    try{
        serverSock = new ServerSocket(port);
    }catch (IOException e){
        e.printStackTrace();
        System.exit(-1);
    }
}

//接收客户端以及用户在控制台的输入传入服务端
public String read(){
    try {
        serverInput = serverReader.readLine();   //将客户端写入的数据读入进来
    }catch (IOException e){
        e.printStackTrace();
        System.exit(-1);
    }
    return serverInput;
}

//从服务端写入数据到客户端
public void write(){
    getUserInput();
    serverWriter.println(userInput);
    serverWriter.flush();
}

//将客户端传来的消息打印出来
public void printClientMessage(){
    System.out.println("Client:" + serverInput);
}
//将服务端写的消息打印出来
public void printServerMessage() { System.out.println("Server:" + userInput); }

public void close(){
    try {
        serverWriter.close();
        userInputReader.close();
        serverReader.close();
        serverSock.close();
    }catch (IOException e){
        e.printStackTrace();
        System.exit(-1);
    }
}
//判断服务器是否仍在连接中
public boolean serverIsConnected(){
    return !serverInput.equals("bye");
}

public boolean clientIsConnected(){
    return !getUserInput().equals("bye");
}

//实现线程的方法————时刻准备接收来自客户端的消息
public class ServerRead implements Runnable {
    public void run() {
        read();
        while (serverReader != null && serverIsConnected() && serverInput.length()!=0) {
            printClientMessage();
            read();
        }
        //printServerDisconnected();
    }
}

//实现线程的方法————时刻准备向服务端发送消息
class ServerWrite implements Runnable{
    public void run() {
        write();
        while (userInputReader != null && clientIsConnected() && userInput.length()!=0){
            //printServerMessage();
            write();
        }
        //printClientDisconnected();
    }
}

}

1个回答

https://code.csdn.net/lzl_Avery/averywork/tree/master/Avreyworkspace/Demo0602/src/demo/classwork

代码所在地址,server3 client3 服务端多线程,转发每个客户端的消息给每个客户端,每个户端 用一个线程接收 服务器转发的消息,实现了一个
简易聊天室的功能 server1 client1 是没有开线程的 只看3 就可以了 3是多线程实现

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问