2012-09-24 54 views
0

你好,我是java socket編程的新手,我只是想看看有人能給我一些幫助。Java服務器和客戶端套接字需要修復

我將張貼在客戶端和服務器的代碼,然後我會解釋我的問題...

  reader = new BufferedReader(new InputStreamReader(socket.getInputStream)); 

     while(running) 
     { 
      String line = reader.readLine(); 

      if(line != null) 
      { 
       System.out.println(line); 
       stream = new PrintStream(socket.getOutputStream()); 
       stream.println("return: " + line); 
      } 

     } 

    }catch(IOException e) 
    { 
     System.out.println("Socket in use or not available: " + port); 
    } 
} 

public static void main() 
{ 
    run(); 
} 


//Client 

public static String ip; 
public static int port; 

public static Socket socket; 
public static PrintStream stream; 
public static BufferedReader reader; 

public static void main(String args[]) 
{ 

    try 
    { 
     socket = new socket(ip, port); 
     stream = new PrintStream(socket.getOutputStream()); 
     stream.println("test0"); 
     reader = new BufferedReader(new InputStreamReader(socket.getInputStream)); 
     String line = reader.readLine(); 

     if(line != null) 
     { 
      System.out.println(line); 
     } 

     stream.println("test1"); 
     line = reader.readLine(); 

     if(line != null) 
     { 
      System.out.println(line); 
     } 
    }catch(IOException e) 
    { 
      System.out.println("could not connect to server!"); 
    } 
} 

所以我的問題是,即使我擺脫循環,並儘量使其發送字符串兩次它不會發送它。它只會做一次,除非我關閉並在客戶端創建一個新的套接字。所以,如果有人能給我一個解釋我做錯的事情,那會很棒,並且非常感謝你。

+0

找到它,如果的readLine()的'結果'爲空則必須退出循環並關閉插座。否則,你將永遠炒到CPU。 – EJP

回答

0

請保持它的簡單,

嘗試使用InputStream, InputStreamReader, BufferedReader, OutputStream, PrintWriter.

客戶端:

Socket s = new Socket(); 
s.connect(new InetSocketAddress("Server_IP",Port_no),TimeOut); 
// Let Timeout be 5000 

服務器端:

ServerSocket ss = new ServerSocket(Port_no); 
Socket incoming = ss.accept(); 

對於從套接字讀取:

InputStream is = s.getInputStream(); 
InputStreamReader isr = new InputStreamReader(is); 

BufferedReader br = new BufferedReader(isr); 
boolean isDone = false; 

String s = new String(); 

while(!isDone && ((s=br.readLine())!=null)){ 

    System.out.println(s); // Printing on Console 

} 

用於寫入套接字:

OutputStream os = s.getOuptStream(); 
PrintWriter pw = new PrintWriter(os) 

pw.println("Hello"); 
+0

感謝您的回覆,我會仔細研究這段代碼,看看我能如何運作。謝謝! –

+0

@BrandonLengefeld你是受歡迎的.... –

+0

我已經嘗試了迄今爲止所提出的所有改變,但沒有運氣。這可能是我的錯,因爲對此不夠清楚。但是,我的問題是,當我發送類似「test0」的東西時,它會將其發送到服務器並重新發送沒有問題,但是如果我在哪裏發送「test1」沒有運氣去往/來自/來自服務器。它不應該保持穩定的流量還是我做錯了什麼? –

0

確保你從你的服務器刷新輸出:

stream.flush(); 
1

你爲什麼在循環中打開你的外流? stream = new PrintStream(socket.getOutputStream());

將此語句寫入循環外並寫入您的循環中的stream

0

非常感謝所有回答我的人,但我知道它一直都是如此。 我通讀了一些Oracle套接字的東西,發現服務器應該是第一個發送消息的客戶端,而不是發送和接收客戶端......等等等等,所以我會在這裏發佈我的新代碼在希望別人試圖找出同樣的事情可以輕鬆

//Client  
public static String ip; 
public static int port; 

public static Socket socket; 
public static PrintWriter print; 
public static BufferedReader reader; 

public Client(String ip, int port) 
{ 
    this.ip = ip; 
    this.port = port; 

      //initiate all of objects 
    try 
    { 
     socket = new Socket(); 
     socket.connect(new InetSocketAddress(ip, port), 5000); 
     print = new PrintWriter(socket.getOutputStream()); 
     reader = new BufferedReader(new InputStreamReader(socket.getInputStream())); 
      //start connection with server 
     String line = reader.readLine(); 
     System.out.println(line); 
    } catch (IOException e) 
    { 
     e.printStackTrace(); 
    } 
} 

    //quick method to send message 
public void sendMessage(String text) 
{ 
    print.println(text); 
    print.flush(); 
    try 
    { 
     String line = reader.readLine(); 
     System.out.println(line); 
    } catch (IOException e) 
    { 
     e.printStackTrace(); 
    } 
} 
} 

public static void main(String args[]) 
{ 
    client.sendMessage("test"); 
    client.sendMessage("test2"); 
    client.sendMessage("test3") 
} 

    //Server 


public static int port = 9884; 
public static boolean running = true; 

public static ServerSocket serverSocket; 
public static Socket socket; 
public static PrintWriter writer; 
public static BufferedReader reader; 

public static void run() 
{ 
    try 
    { 
     serverSocket = new ServerSocket(port); 
     socket = serverSocket.accept(); 
     writer = new PrintWriter(socket.getOutputStream(), true); 
     writer.println("connection"); 

     while(running) 
     { 
      reader = new BufferedReader(new InputStreamReader(socket.getInputStream())); 
      String line = reader.readLine(); 

      if(line != null) 
      { 
       System.out.println(line); 
       writer.println(line); 
       writer.flush(); 
      } 
     } 
    } catch (IOException e) 
    { 
     e.printStackTrace(); 
    } 
} 

public static void main(String args[]) 
{ 
    run(); 
} 
相關問題