2017-02-05 150 views
0

我正在嘗試使用客戶端和服務器進行簡單項目,客戶端發送字符串,服務器將其反向發送回客戶端。然而,他們都在掛斷收發信息。所以非常想告訴你我在我的程序中停止的地方......這裏是我的客戶端和服務器的控制檯輸出。客戶端無法發送消息到服務器Java套接字

客戶

Connecting to server... 
S: Connected to server at 4446 
You sent Oranges, sending message... 

服務器

Waiting for a connection... 
Connection received from /127.0.0.1 on port 4446 
Waiting for response... 

我讀過一些關於怎麼樣,你需要使用的確切消息輸出和輸入。所以就像客戶端用一個新行發送一個輸入一樣,服務器應該可以讀取它。儘管我在代碼中沒有看到,但是我的預感是這個問題。

我的代碼如下......

Client.java

import java.io.IOException; 
import java.io.PrintWriter; 
import java.net.Socket; 
import java.net.UnknownHostException; 
import java.util.Scanner; 

public class Client 
{ 
    PrintWriter out; 
    Scanner conIn; 

    public static void main(String[] args) throws UnknownHostException, IOException 
    { 
     Client c = new Client(); 
     c.run(); 
    } 

    public void run() throws UnknownHostException, IOException 
    { 
     System.out.println("Connecting to server..."); 
     Socket connection = new Socket("localhost", 4446); //Connects to server 
     out = new PrintWriter(connection.getOutputStream()); 
     conIn = new Scanner(connection.getInputStream()); 
     String msg = conIn.nextLine(); 
     System.out.println(msg); 
     String s = "Oranges"; 
     System.out.println("You sent " + s + ", sending message..."); 
     out.println(s); //STOPS HERE 
     msg = conIn.nextLine(); 
     System.out.println(msg); 
     out.flush(); 
    } 
} 

Server.java

import java.io.IOException; 
import java.io.PrintWriter; 
import java.net.ServerSocket; 
import java.net.Socket; 
import java.util.Scanner; 

public class Server implements Runnable 
{ 
    ServerSocket serverSocket; 
    Scanner in; 
    PrintWriter out; 
    Socket connection = null; 

    public static void main(String[] args) 
    { 
     Server s = new Server(); 
     s.run(); 
    } 

    public void run() 
    { 
     try 
     { 
      serverSocket = new ServerSocket(4446); //Create server 
      System.out.println("Waiting for a connection..."); 
      this.connection = serverSocket.accept(); //Accept connection 
      System.out.println("Connection recieved from " + connection.getInetAddress() + " on port " + connection.getLocalPort()); 
      out = new PrintWriter(connection.getOutputStream()); //Gets output to connection 
      out.flush(); 
      in = new Scanner(connection.getInputStream()); //Gets input to connection 
      out.println("S: Connected to server at " + serverSocket.getLocalPort()); 
      out.flush(); 
      System.out.println("Waiting for response..."); 
      String msg = in.nextLine(); //STOPS HERE 
      System.out.println("Message recieved! Reversing now."); 
      String rMsg = reverse(msg); 
      System.out.println("Returning message..."); 
      out.println("S: Your message was: " + msg + " and it is now " + rMsg); 
      out.flush(); 
     } 
     catch (IOException e) 
     { 
      e.printStackTrace(); 
     } 
    } 

    public String reverse(String s) 
    { 
     String revS = ""; 

     for(int i = s.length() - 1; i >= 0; i--) 
     { 
      revS = revS + s.charAt(i); 
     } 
     return revS; 
    } 

} 
+2

後,如果您做些什麼'出來.flush();'out.println(s);'後立即'' –

+0

啊,似乎已經奏效,謝謝!我以爲他們每次都想堆棧或者什麼東西,並且在發送一堆輸出之後就可以做到。但我想這並沒有多大意義。謝謝 – Carson

+1

當你在等待數據的交叉傳輸時,你不能「堆疊」。使用線程同時允許輸出和輸入會更好。 –

回答

0

out.flush();out.println(s);

相關問題