2013-05-03 75 views
0

所以,我得到了一個基本的單連接服務器運行,現在我想將其轉換爲一個多線程服務器的小型多層項目,我想做的事,我完全自學成才的,我做絆倒一些事情,和網絡是主要的一個我一直在試圖理解了很久了,可能你們能幫助..需要幫助讓我的基本的socket服務器工作

問題:當我只有一個線程會writeUTFreadUTF它工作得很好,發送的UTF和關閉連接,雖然現在我已移到了多線程我甚至不知道它的客戶端不發送的UTF,服務器不接受UTF,或者服務器不再重新發送UTF,也許你們可以讓我知道,這裏的代碼。

服務器雙面代碼

Server.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(); 
} 
} 

Player.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(); 
     } 

    } 
} 
} 

客戶雙面代碼

Client.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(); 
} 
} 

Input.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(); 
      } 

     } 
    } 
} 

回答

0

您創建和啓動服務器中的Thread對象,但未能通過您的Player可運行它。