2012-03-11 71 views
0

我一直在爲此工作了一段時間,並且碰到了一堵磚牆。我嘗試過使用Google搜索,但是該主題上的所有內容都是基於非服務器或使用API​​的。我想堅持java包的基本使用。因執行信使計劃而丟失

我想要的是兩個客戶端能夠來回發送消息,只需在Eclipse內部的控制檯中執行即可。我知道我必須在服務器中實現線程來處理這兩個連接,並且可能還必須爲服務器編寫一個協議。

任何人都可以給我任何指針嗎?幫助非常感謝

這裏是我到目前爲止

客戶

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

public class Client 
{ 
static Socket client; 
static String input; 
static DataInputStream in; 
static DataOutputStream out; 
static BufferedReader keyboard; 


//constructor, instantiation. 
static void Open() 
{ 
    try 
    { 
     client = new Socket("127.0.0.1", 6666); 
     InputStream sin = client.getInputStream(); 
     OutputStream sout = client.getOutputStream(); 

     in = new DataInputStream(sin); 
     out = new DataOutputStream(sout); 
    } 
    catch (UnknownHostException e) { 
     System.err.println("Don't know about host: taranis."); 
     System.exit(1); 
    } 
    catch (IOException e) { 
     System.err.println("Couldn't get I/O for " + "the connection to: Host."); 
     System.exit(1); 
    } 

    keyboard = new BufferedReader(new InputStreamReader(System.in)); 

} 

//close 
static void Close() throws IOException 
{ 
    client.close(); 
    keyboard.close(); 
    in.close(); 
    out.close(); 
} 



public static void main(String[] args) throws IOException 
{ 

    Open(); 

    while(true) 
    { 
     // wait for the user to type in something and press enter. 
     input = keyboard.readLine(); 


     // send the above line to the server. 
     out.writeUTF(input); 
     out.flush(); 

     // wait for the server to send a line of text. 
     input = in.readUTF(); 

     System.out.println("The server was very polite. It sent me this : " + input); 
     System.out.println(); 
    } 


} 

} 

主機

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

public class Host 
{ 

//static variables 
static ServerSocket host; 
static Socket client; 
static HostProtocol hp; 
static String input1, output2; 
static InputStream sin; 
static OutputStream sout; 
static DataInputStream in; 
static DataOutputStream out; 

//constructor 
public Host() throws IOException 
{ 
    hp = new HostProtocol(); 
    Open(); 

    sin = client.getInputStream(); 
    sout = client.getOutputStream(); 
    in = new DataInputStream(sin); 
    out = new DataOutputStream(sout); 
} 

//open sockets, instantiate variables 
public static void Open() 
{ 
    try 
    { 
     host = new ServerSocket(6666); 
    } 
    catch (IOException e) 
    { 
     System.err.println("Could not listen on port: 6666."); 
     System.exit(1); 
    } 

    try 
    { 
     client = host.accept(); 
    } 
    catch (IOException e) 
    { 
     System.err.println("Accept failed."); 
     System.exit(1); 
    } 
} 

//close method 
public static void Close() throws IOException 
{ 
    out.close(); 
    client.close(); 
    host.close(); 
} 


//main method 
public static void main(String[] args) throws IOException 
{ 

    Host serverhost = new Host(); 
    input1=in.readUTF(); 

} 
} 

回答

1

您的客戶端也將至少需要一個額外的線程,以便它可以隨時接收和發送聊天消息。

在服務器端,您需要將呼叫置於accept以便接受多個連接。然後,對於每個新連接的客戶端,您可以生成一個新線程並將其傳遞給從accept獲取的套接字對象:

private class ClientThread implements Runnable { 
    private Socket socket; 

    public ClientThread(Socket socket) { 
     this.socket = socket; 
    } 

    public void run() { 
     // read messages from socket and send them to the other client. 
    } 
} 

... 

while(true) { 
    try { 
     client = host.accept(); 
     Thread t = new Thread(new ClientThread(client)); 
     t.start(); 
    } catch (IOException e) { 
     System.err.println("Accept failed."); 
     System.exit(1); 
    } 
}