2013-08-17 5 views
0

나는 매우 새로운 안드로이드 프로그래밍에 새입니다.안드로이드 TCP 통신 오류

public class AndroidClient extends Activity { 

EditText textOut; 
TextView textIn; 
Socket socket = null; 
DataOutputStream dataOutputStream = null; 
DataInputStream dataInputStream = null; 

/** Called when the activity is first created. */ 
@Override 

public void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
setContentView(R.layout.activity_android_client); 

textOut = (EditText)findViewById(R.id.textout); 
Button buttonSend = (Button)findViewById(R.id.send); 
textIn = (TextView)findViewById(R.id.textin); 
buttonSend.setOnClickListener(buttonSendOnClickListener); 

} 

    Button.OnClickListener buttonSendOnClickListener 
    = new Button.OnClickListener(){ 

@Override 
public void onClick(View arg0) { 
// TODO Auto-generated method stub 

new BackgroundDataTask().execute(""); 
}}; 

    public class BackgroundDataTask extends AsyncTask<String,String,String> { 

    private Exception ex; 
    @Override 
    protected String doInBackground(String... urls) { 

     try { 
      socket = new Socket("10.20.50.68", 8888); 
      dataOutputStream = new DataOutputStream(socket.getOutputStream()); 
      dataInputStream = new DataInputStream(socket.getInputStream()); 
      dataOutputStream.writeUTF(textOut.getText().toString()); 
      textIn.setText(dataInputStream.readUTF()); 
      socket = serverSocket.accept(); 

      textIn.setText(socket.getInetAddress().toString()); 
     } catch (UnknownHostException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

     finally{ 
      if (socket != null){ 
      try { 
      socket.close(); 
      } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
      } 
      } 

      if (dataOutputStream != null){ 
      try { 
      dataOutputStream.close(); 
      } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
      } 
      } 

      if (dataInputStream != null){ 
      try { 
      dataInputStream.close(); 
      } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
      } 
      } 
     } 

     return ""; 
    } 

} 


} 

내가 C# 코드 코드는 포트에 수신에 서버가 다음 처음으로 출력

에게 그러나이 단지 작품을 쓰기로 안드로이드에 내 코드입니다 때 텍스트를 입력하고 을 클릭하십시오.을 보내면 코드가 서버 측에서 수신되고 콘솔에서 ouptput을 볼 수 있습니다. 그러나 그 값은 수신되지 않습니다. 어떻게이 소켓을 계속 실행할 수 있습니까? 내가 뭘 놓치고 있니?

모든

답변

0

가 클래스 client.java

public class client{ 

Socket socket; 
int Port; 
InetAddress serverIp; 
public boolean connection; 
public client(String ip, int port) { 
    // TODO Auto-generated constructor stub 
    try { 
     serverIp = InetAddress.getByName(ip); 
     Port = port; 
        connect(); 
    } catch (UnknownHostException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
     connection = false; 
    } 

} 

public void connect(){ 
    try { 
      System.out.println("wait connection client "+serverIp+" "+Port); 
     socket = new Socket(serverIp, Port); 
     connection = true; 


    } catch (IOException e) { 
     //e.printStackTrace(); 
     connection = false; 
    } 

} 
    public boolean send(String message) throws IOException { 


    byte[] data=message.getBytes(); 
    OutputStream out = socket.getOutputStream(); 
    DataOutputStream dos = new DataOutputStream(out); 
    int len = data.length; 
    dos.writeInt(len); 
    if (len > 0) { 
     dos.write(data, 0, len); 
    } 
    return true; 
} 
public String read() { 
    InputStream in = null; 

    try { 
     in = socket.getInputStream(); 
    } catch (IOException e) { 
     connection=false; 
     e.printStackTrace(); 
    } 

    DataInputStream dis = new DataInputStream(in); 
    try { 
     int len = dis.readInt(); 
     byte[] data = new byte[len]; 
      if (len > 0) { 
       dis.readFully(data); 
      } 

      String result = new String(data, 0, data.length); 
      return result; 
    } catch (Exception e) { 
     // TODO: handle exception 
     e.printStackTrace(); 
     connection=false; 
    } 
    return ""; 
} 

을 만들이

을 시도하고 활동에서 onCreate

client con=new client("10.20.50.68", 8888); 

에 client.java의 객체를 생성 주셔서 감사 전송하려면 con.send("your_data")을 사용하고 데이터를 받으려면 String data=con.read();을 사용하십시오. .. 소켓에서 비동기 적으로 데이터 수신을 원한다면 스레드를 사용하십시오.