2014-02-15 103 views
0

這裏是我的服務器端代碼:客戶端 - 服務器通信,沒能獲得客戶端發送信息到服務器

package server; 

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


public class Serverside 
{ 
public static void main (String [] args) throws Exception 
{ 
    System.out.println("The server is running."); 
    int clientnumber = 1; 
    ServerSocket server = new ServerSocket(9090); 
    try 
    { 
     while (true) 
     { 
      new cserver(server.accept(), clientnumber++).start(); 

     } 

    }finally 
    { 
     server.close(); 
    } 

} 

private static class cserver extends Thread 
{ 
    private Socket socket; 
    private int clientnumber; 

    public cserver(Socket socket, int clientnumber) 
    { 
     this.socket = socket; 

     this.clientnumber = clientnumber; 
     log("New connection with Client: " + clientnumber + " at " + socket); 
    } 

    private void log (String message) 
    { 
     System.out.println(message); 
    } 

    public void run() 
    { 
     try 
     { 
      BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream())); 
      PrintWriter out = new PrintWriter(socket.getOutputStream(), true); 

      //Message for Client 
      out.println("Hello, Client# " + clientnumber + "."); 
      out.println("Input Line\n"); 

      while(true) 
      { //System.out.println("Reading from Client\n"); 
       String input = in.readLine(); 
      // out.println(input); 
       if (input == null) 
        break; 

       out.println(input); 
      } 
     }catch (IOException e) 
     { 
       log("Error handling in client#" + clientnumber + ": " + e); 
     }finally 
     { 
      try 
      { 
       socket.close(); 
      }catch (IOException e) 
      { 
       log("Could not close socket"); 
      } 
      log("Connection with Client# " + clientnumber + " closed"); 
     } 


    } 
} 
} 

,這裏是我的客戶端代碼:

package client; 

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



public class clientside 
{ public static void main (String [] args) throws IOException 
{ 
    Socket myclient = new Socket("localhost", 9090); 
    BufferedReader in = 
      new BufferedReader(new 
      InputStreamReader(myclient.getInputStream())); 
    PrintWriter out = new PrintWriter(myclient.getOutputStream(), true); 
    out.print("Hello server, end session\n"); 

    for(int i=0;i<=2;i++) 
    { 
     String input = in.readLine(); 
     System.out.println(input); 
    } 

    System.out.println("It is done"); 

    myclient.close(); 

} 
} 

現在我當運行代碼,客戶端能夠從服務器接收2行並打印它們,但我無法從客戶端發送一行到服務器並進行打印。如果任何人都可以幫助,我會非常感激。

+0

您沒有將數據發送到服務器的套接字,數據如何傳輸? – Smutje

+0

欣賞快速repsonse,但很抱歉,我真的很新,但你介意解釋你的意思。我認爲Printwriter將數據發送到服務器。我應該如何解決這個問題?任何提示? – CurryMaster

+0

我猜你的代碼不完整,行'BufferedReader in = new BufferedReader(新'突然結束。:-) – Smutje

回答

0

這只是一個錯誤。在clientside.java

System.out.println(input);

和16行:當您在Serverside.java更改65行

out.println("Hello server, end session");

它的工作原理。所以「System.out」代替Serverside和「println」中的「out」,而不是客戶端中的「\ n」。

+0

我注意到了,面對着我的愚蠢,並且憤怒地衝出了房子,但是謝謝:) – CurryMaster