2014-03-13 51 views
1

我開發了將表單客戶端發送到服務器的代碼,然後服務器必須將它從客戶端接收到的內容再次發送到客戶端。但在我的代碼中,服務器只接收消息,但不會再次發送。我不知道是什麼問題從客戶端發送到服務器,從服務器發送到客戶端在java中

這是我的客戶端代碼

public class Client implements Runnable{ 

private static Socket s = null; 
//private static BufferedOutputStream fromUser = null; 
private static DataInputStream fromServer = null; 
private static InputStreamReader input = new InputStreamReader(System.in); 
private static InputStreamReader inputstreamreader = null; 
private static BufferedReader bufferedreader = null; 
private static DataOutputStream fromUser = null; 
private static int chara = 0; 
private static String line = null; 
static int port = 0; 
static String host = null; 

//connect to server 
@Override 
public void run() { 
     try { 
inputstreamreader = new InputStreamReader(s.getInputStream()); 
bufferedreader = new BufferedReader(inputstreamreader); 

    //charr = fromClient.read(); 
    while(true){ 
    if ((line = bufferedreader.readLine()) != null){ 
     System.out.println(line);} 
     if(line.equals(-1)){ 
     break; 
     } 


}//end while 
}catch(NullPointerException e) { 
    // do something other 
} 
catch (IOException e) { 
    System.out.println(e); 
} 

}//end the run 


//constructor with two arguments 
public Client(String host, int port){ 
try{ 
s = new Socket (host,port); 
} 
catch(Exception e){} 
} 
//send message to from Client to Server 

public static void sendToServer(){ 
try{ 
fromUser =new DataOutputStream (new BufferedOutputStream(s.getOutputStream())); 
chara =input.read(); 
while(true){ 
if (chara == '~'){ 
break;} 
fromUser.write(chara); 
fromUser.flush(); 
chara =input.read(); 
}//end while 
} 
catch (IOException e) { 
    System.out.println(e); 
} 
}//end send message 

我試圖用線程接收消息,但也不起作用。我嘗試沒有線程它不工作。

public static void main(String [] args){ 

host = args[0]; 
port = Integer.parseInt(args[1]); 

System.out.println("Start connection ....."); 
Client client = new Client(host,port); 
    Thread thread = new Thread(client); 
    thread.start(); 
//connect(host,port); 


client.sendToServer(); 

client.close(); 



}//end main 

,這是我的服務器代碼

public class Server extends Thread{ 

private static Socket s = null; 
private static ServerSocket ss = null; 
private static DataOutputStream fromUser = null; 
private static DataInputStream fromClient = null; 
private static InputStreamReader input = new InputStreamReader(System.in); 
private static InputStreamReader inputstreamreader = null; 
private static BufferedReader bufferedreader = null; 
static String line=null; 
static String sendC; 
private static int chara = 0; 
static int port = 0; 

//connect to server 
static void connect(int port){ 
try{ 
    ss = new ServerSocket (port); 
    System.out.println("Listening on port "+port+"..."); 
    s = ss.accept(); 
System.out.println("Has been connected ....."); 
    } 
    catch (IOException e) { 
     System.out.println(e); 
    } 

}//end of the connection method 
//send message to from Client to Server 
public static void sendToClient(String text){ 
try{ 
fromUser =new DataOutputStream (new BufferedOutputStream(s.getOutputStream())); 
sendC =text; 
fromUser.write(sendC.getBytes()); 
fromUser.flush(); 

} 
catch (IOException e) { 
    System.out.println(e); 
} 
}//end send message 

public static void receiveFromClient(){ 
try { 
inputstreamreader = new InputStreamReader(s.getInputStream()); 
bufferedreader = new BufferedReader(inputstreamreader); 

    //charr = fromClient.read(); 
    while(true){ 
    if ((line = bufferedreader.readLine()) != null){ 
     System.out.println(line); 
     sendToClient(line);} 
     if(line.equals(-1)){ 
     break; 
     } 
     }//end while 
}catch(NullPointerException e) { 
    // do something other 
} 
catch (IOException e) { 
    System.out.println(e); 
} 
}//end send message 

這是服務器的主要方法

public static void main(String [] args){ 
port = Integer.parseInt(args[0]); 
System.out.println("Start connection ....."); 
connect(port); 
receiveFromClient(); 
//sendToClient(); 
close(); 



}//end main 

我沒有在Java中的知識很多特別是關於插座 感謝您幫助

回答

0

一個簡單的搜索一個java回顯服務器顯示這個

public class EchoServer { 

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

     // create socket 
     int port = 4444; 
     ServerSocket serverSocket = new ServerSocket(port); 
     System.err.println("Started server on port " + port); 

     // repeatedly wait for connections, and process 
     while (true) { 

      // a "blocking" call which waits until a connection is requested 
      Socket clientSocket = serverSocket.accept(); 
      System.err.println("Accepted connection from client"); 

      // open up IO streams 
      In in = new In (clientSocket); 
      Out out = new Out(clientSocket); 

      // waits for data and reads it in until connection dies 
      // readLine() blocks until the server receives a new line from client 
      String s; 
      while ((s = in.readLine()) != null) { 
       out.println(s); 
      } 

      // close IO streams, then socket 
      System.err.println("Closing connection with client"); 
      out.close(); 
      in.close(); 
      clientSocket.close(); 
     } 
    } 
} 

看到http://introcs.cs.princeton.edu/java/84network/EchoServer.java.html

+0

我已經試過了,但仍然無法正常工作。實際上這個代碼中的內容幾乎就像我的代碼中的內容。我想問題在於客戶端如何從服務器接收客戶端代碼中的消息,我們如何讓客戶端接受來自服務器的消息。 – user3397765

+0

一開始,採取自己的建議。 } catch(NullPointerException e){ //做某事 }總是會記錄你捕獲的任何異常 –

+0

另外,'if(line.equals(-1)){' - 這是什麼? –

相關問題