编写Android端为客户端的简易app,用电脑端作服务端,实现客户端与服务端互发字符串。
利用Socket网络编程,实现客户端与服务端的连接并通信。
首先我们需要在AndroidManifest.xml中添加网络权限
<uses-permission android:name="android.permission.INTERNET" />
客户端
先编写客户端app,我们在Android studio中新建project,然后编写布局代码如下:
layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.networktest.MainActivity">
<EditText
android:id="@+id/editText"
android:hint="Type here"
android:maxLines="3"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/send"
android:text="send"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textview"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</ScrollView>
</LinearLayout>
MainActivity
然后是客户端主程序:
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
StringBuffer msg=new StringBuffer();
String getS=" ";
String sendmsg; //文本消息
Socket mysocket; //用Socket定义客户端对象
Button send; //发送按钮
TextView textview; //用于显示收发的字符串
EditText editText; //用于客户端输入字符串
DataInputStream in;
DataOutputStream outt; //输入输出读写
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//控件初始化
send=(Button) findViewById(R.id.send);
textview=(TextView) findViewById(R.id.textview);
editText=(EditText) findViewById(R.id.editText);
send.setOnClickListener(this);
new Thread(new Runnable(){ //开启线程连接服务端
@Override
public void run() {
try{
String ip="192.168.3.10";
mysocket=new Socket(ip,12345); //服务端地址及端口
in=new DataInputStream(mysocket.getInputStream());
outt=new DataOutputStream(mysocket.getOutputStream());
//输入输出流
while(true){
getS="服务端:"+in.readUTF()+"\n"; //读取输入流
msg.append(getS);
showmsg(msg.toString()); //输入显示更新在主界面
}
}catch(Exception e){
System.out.println("wrong");
//错误
}
}
}
).start();
}
private void showmsg(final String ms){ //必须在主线程中进行UI操作
runOnUiThread(new Runnable(){
@Override
public void run() {
textview.setText(ms);
}
});
}
@Override
public void onClick(View v) {
switch(v.getId()){
case R.id.send: //发送按钮
sendmsg=editText.getText().toString(); //获取客户端输入
getS="客户端:"+sendmsg+"\n";
msg.append(getS);
showmsg(msg.toString());
//msg.append("客户端:"+sendmsg+"\n");
try{
outt.writeUTF(sendmsg);
}catch(Exception e){
}
break;
default:
break;
}
}
}
服务端
在AndroidStudio中新建一个Java程序,具体方法http://blog.csdn.net/xiaxiayige/article/details/46706949
接下来是服务端代码:
public class MyClass {
public static void main(String[] args)throws IOException{
final ServerSocket server=new ServerSocket(12345); //定义ServerSocket对象用作服务端,参数为端口
final Socket socket=server.accept(); //accept方法等待客户端连接
DataInputStream in=new DataInputStream(socket.getInputStream());
String s; //接受客户端字符串
new Thread(new Runnable() { //开启线程用于服务端发送数据
@Override
public void run() {
Scanner getin=new Scanner(System.in);
try{
String sendmsg;
DataOutputStream pw =new DataOutputStream(socket.getOutputStream());
while(true){
sendmsg=getin.nextLine(); //控制台输入信息
pw.writeUTF(sendmsg);
}
}catch(Exception r){
}
}
}).start();
while((s=in.readUTF())!=null){
System.out.println("Client "+s); //读取输入流
}
}
}
运行
先运行服务端,再运行客户端,即可实现数据互相收发.