标签:
也没什么好说的。看看就行了
package com.example.webclient;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.Socket;
import java.net.UnknownHostException;
import com.example.webclient.R.id;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
private Button link;
private TextView show;
private Handler handle = new Handler() {
public void handleMessage(Message msg) {
String a=(String) msg.obj;
MainActivity.this.show.setText(a) ;
};
};
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
private void init() {
link = (Button) findViewById(id.link);
show = (TextView) findViewById(id.show);
link.setOnClickListener(new linkSocket());
}
public class linkSocket implements OnClickListener {
public void onClick(View arg0) {
Thread thread = new Thread(null, inThread, "linkServer");
thread.start();
}
}
public Runnable inThread = new Runnable() {
public void run() {
hehe();
}
};
public void hehe() {
try {
Socket client = new Socket("192.168.156.1", 8899);
PrintStream out = new PrintStream(client.getOutputStream());
BufferedReader buf = new BufferedReader(new InputStreamReader(
client.getInputStream()));
out.println("怎么突然就对了呢") ; // 向服务器端发送数据
Message msg = Message.obtain();
String a =buf.readLine();
msg.obj=a;
handle.sendMessage(msg);
out.close() ;
buf.close() ;
client.close() ;
} catch (Exception e) {
e.printStackTrace() ;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
serverSocket
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;
public class Myserver {
public static void main(String[] args) throws Exception {
ServerSocket server = new ServerSocket(8899);
Socket client = server.accept();
PrintStream out = new PrintStream(client.getOutputStream());
BufferedReader buf = new BufferedReader(new InputStreamReader(
client.getInputStream()));
StringBuffer info = new StringBuffer() ;
info.append("恩。") ;
info.append(buf.readLine()) ;
out.print(info) ;
out.close() ;
buf.close() ;
client.close() ;
server.close() ;
}
}
readLine刚开始读不出数据,后来又可以了,代码没有改动,不知道是不是eclipse的原因,知道的帮忙解答一下。。。
标签:
原文地址:http://www.cnblogs.com/mydomainlistentome/p/4704187.html