http://www.runoob.com/w3cnote/android-tutorial-socket-intro.html
http://blog.csdn.net/x605940745/article/details/17001641
1、客户端
public class MainActivity extends AppCompatActivity {
Socket socket = null;
String buffer = "";
TextView txt1;
Button send;
EditText ed1;
String geted1;
public Handler myHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
if (msg.what == 0x11) {
Bundle bundle = msg.getData();
txt1.append("server:"+bundle.getString("msg")+"\n");
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txt1 = (TextView) findViewById(R.id.txt1);
send = (Button) findViewById(R.id.send);
ed1 = (EditText) findViewById(R.id.ed1);
send.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
geted1 = ed1.getText().toString();
txt1.append("client:"+geted1+"\n");
//启动线程 向服务器发送和接收信息
new MyThread(geted1).start();
}
});
}
class MyThread extends Thread {
public String txt1;
public MyThread(String str) {
txt1 = str;
}
@Override
public void run() {
//定义消息
Message msg = new Message();
msg.what = 0x11;
Bundle bundle = new Bundle();
bundle.clear();
try {
//1.创建客户端Socket,指定服务器地址和端口
//连接服务器 并设置连接超时为5秒
// Socket socket = new Socket("192.168.63.33", 12345);
socket = new Socket();
socket.connect(new InetSocketAddress("192.168.63.33", 12345), 5000);
//2.获取输出流,向服务器端发送信息
OutputStream ou = socket.getOutputStream();
BufferedReader bff = new BufferedReader(new InputStreamReader(socket.getInputStream()));
//读取发来服务器信息
String line = null;
buffer="";
while ((line = bff.readLine()) != null) {
buffer = line + buffer;
}
//向服务器发送信息
ou.write("android 客户端".getBytes("UTF-8"));
ou.flush();
bundle.putString("msg", buffer.toString());
msg.setData(bundle);
//发送消息 修改UI线程中的组件
myHandler.sendMessage(msg);
//关闭各种输入输出流
bff.close();
ou.close();
socket.close();
} catch (SocketTimeoutException aa) {
//连接超时 在UI界面显示消息
bundle.putString("msg", "服务器连接失败!请检查网络是否打开");
msg.setData(bundle);
//发送消息 修改UI线程中的组件
myHandler.sendMessage(msg);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
activity_main.xml 布局
<EditText
android:id="@+id/ed1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="给服务器发送信息"/>
<Button
android:id="@+id/send"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/ed1"
android:text="发送"/>
<TextView
android:id="@+id/txt1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/send"/>
2、服务器端
public class AndroidService {
public static void main(String[] args) throws IOException {
ServerSocket serivce = new ServerSocket(30000);
while (true) {
//等待客户端连接
System.out.println("服务端运行中...\n");
Socket socket = serivce.accept();
new Thread(new AndroidRunable(socket)).start();
}
}
}
public class AndroidRunable implements Runnable {
Socket socket = null;
public AndroidRunable(Socket socket) {
this.socket = socket;
}
@Override
public void run() {
// 向android客户端输出hello worild
String line = null;
InputStream input;
OutputStream output;
String str = "hello world!";
try {
//向客户端发送信息
output = socket.getOutputStream();
input = socket.getInputStream();
BufferedReader bff = new BufferedReader(new InputStreamReader(input));
output.write(str.getBytes("UTF-8"));
output.flush();
//半关闭socket
socket.shutdownOutput();
//获取客户端的信息
while ((line = bff.readLine()) != null) {
System.out.print(line);
}
//关闭输入输出流
output.close();
bff.close();
input.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
网友评论