android socket

luck6688 发布于 2013/11/08 21:45
阅读 78
收藏 0

急急急 !!! 我要在安卓上 写一个table包含name+id(可以是虚拟的)。。需要当我点击row的时候,打开一个socket connection。。。很凌乱 大神们 求拯救

public class MySocket {

public static void main(String args[]) throws IOException{

Socket socket = null;

BufferedReader in = null;

String b = null;

try {

socket = new Socket("170.2.68.36", 2000);

System.out.println("Connected");

OutputStream os = socket.getOutputStream(); //define a outputstream

 

byte[] bytes = b.getBytes();

os.write(bytes); //向输出流中写入数据

 

os.flush(); //刷行输出流

 

while(socket.isConnected()){

InputStream is = socket.getInputStream();

in = new BufferedReader(new InputStreamReader(is));

byte[] bytes2 = new byte[20];

is.read(bytes2);//读取输入流数据

 

String str = new String(bytes2);//转换成字符串

 

btn.setText(str);

}

String line = null;

while ((line = in.readLine()) != null) {

System.out.println("Line: "+line);

}

}

catch (IOException e) {

System.out.println(e);

}

in.close();

socket.close();

}

}

 

加载中
返回顶部
顶部