当前位置: 代码迷 >> Android >> 关于小弟我的socket编程有关问题
  详细解决方案

关于小弟我的socket编程有关问题

热度:49   发布时间:2016-05-01 17:35:50.0
关于我的socket编程问题
麻烦各位大侠 帮我看一下程序 我写了一个客户端程序和服务器程序
客户端程序如下:
它的大体结构式 我写了九个按钮 每一个按钮都有一个监听器 每一个监听器的处理的事情就是发送一个socket请求给服务器端。
import java.io.BufferedWriter;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.InetSocketAddress;
import java.net.Socket;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class UItestActivity extends Activity {
  /** Called when the activity is first created. */
   
  private Button button1;
  private Button button2;
  private Button button3;
  private Button button4;
  private Button button5;
  private Button button6;
  private Button button7;
  private Button button8;
  private Button button9;
  private EditText edt;
  public void Send(String sRemoteIP,int iRemotePort,String sCmd){
   
  try {
 
  InetSocketAddress remoteAddr = new InetSocketAddress(sRemoteIP,iRemotePort);
 
  Socket socket = new Socket();
   
  socket.connect(remoteAddr);
 
  System.out.println("zheshishenem");
  PrintWriter send = new PrintWriter(new BufferedWriter( new OutputStreamWriter(socket.getOutputStream())));
  send.println(sCmd);
  send.flush();

  send.close();
  socket.close();
  } catch (Exception e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
  }

  }

  @Override
  public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
  button1=(Button)findViewById(R.id.button1);
  button2=(Button)findViewById(R.id.button2);
  button3=(Button)findViewById(R.id.button3);
  button4=(Button)findViewById(R.id.button4);
  button5=(Button)findViewById(R.id.button5);
  button6=(Button)findViewById(R.id.button6);
  button7=(Button)findViewById(R.id.button7);
  button8=(Button)findViewById(R.id.button8);
  button9=(Button)findViewById(R.id.button9);
  edt=(EditText)findViewById(R.id.edt);
  button1.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
edt.setText("1");

Send("172.19.24.81", 1400, "1");

}
});
  button2.setOnClickListener(new OnClickListener() {
 
  @Override
  public void onClick(View v) {
  // TODO Auto-generated method stub
  edt.setText("2");
  Send("172.19.24.81", 1400, "2");
 
  }
  });
  button3.setOnClickListener(new OnClickListener() {
 
  @Override
  public void onClick(View v) {
  // TODO Auto-generated method stub
  edt.setText("3");
  Send("172.19.24.81", 1400, "3");
 
  }
  });
  button4.setOnClickListener(new OnClickListener() {
 
  @Override
  public void onClick(View v) {
  // TODO Auto-generated method stub
  edt.setText("4");
  相关解决方案