2011-04-16 132 views
7

我有一個android應用程序,我嘗試使用套接字連接向服務器發送和接收消息。使用Socket發送和接收消息的Android應用程序:

我已經有各種問題發送和接收。我一直都能夠做到這一點,而不是兩個。

我想知道是否有人可以幫我做一個簡單的練習,我可以使用BufferedReaderPrintWriter來編譯。

我很欣賞任何幫助,因爲我處於放棄的地步。

在此先感謝....下面是我試過的幾張照片(雖然它們與這個問題無關,但我希望它表明我已經在此之前嘗試過)。

private OnClickListener sendClickListener = new OnClickListener(){ 
    public void onClick(View arg0) { 

     Thread cThread = new Thread(new ClientThread()); 
     cThread.start(); 
}};  

public class ClientThread implements Runnable { 
    public void run() { 
     try { 
      EditText dstAddress = (EditText) findViewById(R.id.destinationAddress); 
      EditText dstMessage = (EditText) findViewById(R.id.messageToTranslate); 
      EditText dstPort = (EditText) findViewById(R.id.destinationPort); 
      String address = dstAddress.getText().toString(); 
      String message = dstMessage.getText().toString(); 
      int port = Integer.parseInt(dstPort.getText().toString()); 

      InetAddress serverAddr = InetAddress.getByName(address); 
      Log.d(".Coursework", "C: Connecting..."); 

      Socket socket = new Socket(serverAddr, port); 
      connected = true; 
       while (connected) { 
        try { 
         EditText dstTranslation = (EditText) findViewById(R.id.returnedTranslation); 
         dstTranslation.setText("help me"); 
         Log.d(".Coursework", "C: Sending command."); 
         PrintWriter out; 
         out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true); 
         out.println(language); 
         out.println(message); 
         Log.d("ClientActivity", "C: Sent."); 
         //BufferedReader in; 
         //in = new BufferedReader(new InputStreamReader(socket.getInputStream())); 
         //String translation = in.readLine(); 


        } catch (Exception e) { 
         Log.e("ClientActivity", "S: Error", e); 
        } 
       } 
       socket.close(); 
       Log.d("ClientActivity", "C: Closed."); 
     } catch (Exception e) { 
      Log.e("ClientActivity", "C: Error", e); 
      connected = false; 
     } 

    } 

public class ClientConnection { 

String address, language, message; 
int portNumber; 
Socket clientSocket = null; 

public ClientConnection(String lan, String mes, String add, int pn) throws IOException{ 
    address = add; 
    portNumber = pn; 
    language = lan; 
    message = mes; 
} 
public String createAndSend() throws IOException{ 
    // Create and connect the socket 
    Socket clientSocket = null; 
    clientSocket = new Socket(address, portNumber); 
    PrintWriter pw = new PrintWriter(clientSocket.getOutputStream(),true); 
    BufferedReader br = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));  

    // Send first message - Message is being correctly received 
    pw.write(language+"\n"); 
    pw.flush(); 
    // Send off the data 

    // Send the second message - Message is being correctly received 
    pw.write(message+"\n"); 
    pw.flush(); 
    pw.close(); 
    // Send off the data 

    // NOTE: Either I am not receiving the message correctly or I am not sending it from the server properly. 
    String translatedMessage = br.readLine(); 
    System.out.print(translatedMessage); 
    br.close(); 
    //Log.d("application_name",translatedMessage); Trying to check the contents begin returned from the server. 
    return "Say What??"; 
} 

import java.io.BufferedOutputStream; 
import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStreamReader; 
import java.io.PrintWriter; 
import java.net.ServerSocket; 
import java.net.Socket; 

public class ServerConnection { 

    public static void main(String[] args) throws Exception {  
     // Delete - Using while loop to keep connection open permanently. 
     boolean status = false; 
     while(!status){ 
      ServerSocket serverSocket = null; 
      try { 
       serverSocket = new ServerSocket(4444); 
      } catch (IOException e) { 
       System.err.println("Could not listen on port: 4444."); 
       System.exit(1); 
      } 
      Socket clientSocket = null; 
      try { 
       clientSocket = serverSocket.accept(); 
      } catch (IOException e) { 
       System.err.println("Accept failed."); 
       System.exit(1); 
      } 
      // Delete - Working as of here, connection is established and program runs awaiting connection on 4444 
      BufferedReader br = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));  
      String language = br.readLine(); 
      String message = br.readLine(); 
      // Test - Works 
      System.out.println(language);  
      // Test - Works 
      System.out.println(message); 
      // Delete - Working as of here, both messages are passed and applied. Messages are received as sent from client. 
      TranslateMessage tm = new TranslateMessage(); 
      String translatedMessage = tm.translateMessage(language, message); 

      // NOTE: This seems to be where I am going wrong, either I am not sending the message correctly or I am not receiving it correctly.. 
      // PrintWriter writer = new PrintWriter(new BufferedOutputStream(clientSocket.getOutputStream())); 
      PrintWriter pw = new PrintWriter(clientSocket.getOutputStream(),true); 
      // Send translation back 
      System.out.println(translatedMessage); 
      pw.write(translatedMessage+"\n"); 
      pw.write("Return test \n"); // Test message! 
      pw.flush(); 
      // Send off the data 
      pw.close(); 
      br.close(); 
      clientSocket.close(); 
      serverSocket.close(); 
     } 
    } 
} 
+0

@ user671430:爲什麼要使用套接字?這很容易處理HTTP。 – Squonk 2011-04-17 00:03:26

+0

可能重複[客戶端和服務器之間使用套接字通信](http://stackoverflow.com/questions/5689667/communication-between-client-and-server-using-sockets) – MByD 2011-04-17 00:04:53

+0

您問了幾乎相同的問題約2小時前。 – MByD 2011-04-17 00:06:09

回答

1

我只是忘了關閉在客戶端套接字連接。所以雖然我懷疑我做了什麼是對我自己的問題的模型回答,但它的作品包含在內。

相關問題