0

所以,我设法让一个基本的单连接服务器运行,现在我正试图将它转换为一个多线程服务器,用于我想做的一个小型多层项目,我完全是自学成才,我确实绊倒了有些事情,网络是我很长时间以来一直试图理解的主要事情,也许你们可以提供帮助..

问题:当我只有一个可以正常工作的线程时writeUTFreadUTF发送 UTF 并关闭连接,尽管现在我已将其移至多线程,但我什至不确定是否是客户端未发送 UTF,服务器没有收到 UTF,或者服务器没有重新发送 UTF,也许你们可以告诉我,这是代码。

服务器端代码

服务器.java

package TestServer.net;
/**
 * This is the server class, the server is the main class
 * for instantiating anything related to server-side functionality,
 * the server listens for incoming connections aswell as handles
 * the data transmitted between the two.
 * 
 * 
 * @author Christian
 */
import java.io.*;
import java.net.*;
import java.sql.*;

import TestServer.net.players.Player;


public class Server {
    Player[] player = new Player[Config.MAX_CONNECTIONS];
    ServerSocket serverSocket;
    Socket socket;
    DataOutputStream out;
    DataInputStream in;

public Server() throws IOException {
    // Open the server on the following port
    System.out.println("Attempting to setup server...");
    serverSocket = new ServerSocket(43594);
    System.out.println("Server officially setup on port: 43594");
    // Tell the server to accept connections
    while (true) {
        socket = serverSocket.accept();
        for(int i = 0; i < Config.MAX_CONNECTIONS; i++) {
            System.out.println("Server is waiting for connections...");
            System.out.println("Connection from: " +socket.getInetAddress());
            // Setup the server to send out data.
            out = new DataOutputStream(socket.getOutputStream());
            in = new DataInputStream(socket.getInputStream());
            if(player[i] == null) {
                player[i] = new Player(out, in, player);
                Thread thread = new Thread();
                thread.start();
                break; // End the loop to start listening for more connections
            }
        }
    }
}

public static void main(String args[]) throws IOException {
    new Server();
}
}

播放器.java

package TestServer.net.players;

import TestServer.net.*;
import java.io.*;

public class Player implements Runnable {
Player[] player = new Player[Config.MAX_CONNECTIONS];
DataOutputStream out;
DataInputStream in;


public Player(DataOutputStream out, DataInputStream in, Player[] player) {
    this.in = in;
    this.out = out;
    this.player = player;
}

public void run() {
    while(true) {
        try {
            String message = in.readUTF();
            for(int i = 0; i < Config.MAX_CONNECTIONS; i++) {
                if(player[i] != null) {
                    player[i].out.writeUTF(message);
                }
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
}
}

客户端代码

客户端.java

package TestClient.net;
import java.net.*;
import java.util.Scanner;
import java.io.*;

import TestClient.net.util.Input;

public class Client {

Socket socket;
DataInputStream in;
DataOutputStream out;

public Client() throws UnknownHostException, IOException {
    // Set the client to connect to (IP, Port);
    System.out.println("Attempting to connect to server on port: 43594");
    socket = new Socket("localhost", 43594);
    System.out.println("Successfully connected to server...");
    in = new DataInputStream(socket.getInputStream());
    out = new DataOutputStream(socket.getOutputStream());
    Input input = new Input(in);
    Thread thread = new Thread(input);
    thread.start();
    Scanner scanner = new Scanner(System.in);
    while(true)  {
        String sendMessage = scanner.nextLine();
        out.writeUTF(sendMessage);

    }
}

public static void main(String args[]) throws UnknownHostException, IOException {
    new Client();
}
}

输入.java

    package TestClient.net.util;


import java.io.DataInputStream;
import java.io.IOException;

public class Input implements Runnable {

    DataInputStream in;

    public Input(DataInputStream in) {
        this.in = in;
    }

    public void run() {
        while (true) {
            String message;
            try {
                message = in.readUTF();
                System.out.println(message);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }
    }
}
4

1 回答 1

0

您在服务器中创建并启动了一个Thread对象,但未能将您的Player可运行对象传递给它。

于 2013-05-03T03:55:21.627 回答