編寫(xiě)Android端為客戶端的簡(jiǎn)易app哨查,用電腦端作服務(wù)端逗抑,實(shí)現(xiàn)客戶端與服務(wù)端互發(fā)字符串。
利用Socket網(wǎng)絡(luò)編程寒亥,實(shí)現(xiàn)客戶端與服務(wù)端的連接并通信邮府。
首先我們需要在AndroidManifest.xml中添加網(wǎng)絡(luò)權(quán)限
<uses-permission android:name="android.permission.INTERNET" />
客戶端
先編寫(xiě)客戶端app,我們?cè)贏ndroid studio中新建project溉奕,然后編寫(xiě)布局代碼如下:
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定義客戶端對(duì)象
Button send; //發(fā)送按鈕
TextView textview; //用于顯示收發(fā)的字符串
EditText editText; //用于客戶端輸入字符串
DataInputStream in;
DataOutputStream outt; //輸入輸出讀寫(xiě)
@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(){ //開(kāi)啟線程連接服務(wù)端
@Override
public void run() {
try{
String ip="192.168.3.10";
mysocket=new Socket(ip,12345); //服務(wù)端地址及端口
in=new DataInputStream(mysocket.getInputStream());
outt=new DataOutputStream(mysocket.getOutputStream());
//輸入輸出流
while(true){
getS="服務(wù)端:"+in.readUTF()+"\n"; //讀取輸入流
msg.append(getS);
showmsg(msg.toString()); //輸入顯示更新在主界面
}
}catch(Exception e){
System.out.println("wrong");
//錯(cuò)誤
}
}
}
).start();
}
private void showmsg(final String ms){ //必須在主線程中進(jìn)行UI操作
runOnUiThread(new Runnable(){
@Override
public void run() {
textview.setText(ms);
}
});
}
@Override
public void onClick(View v) {
switch(v.getId()){
case R.id.send: //發(fā)送按鈕
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;
}
}
}
服務(wù)端
在AndroidStudio中新建一個(gè)Java程序褂傀,具體方法http://blog.csdn.net/xiaxiayige/article/details/46706949
接下來(lái)是服務(wù)端代碼:
public class MyClass {
public static void main(String[] args)throws IOException{
final ServerSocket server=new ServerSocket(12345); //定義ServerSocket對(duì)象用作服務(wù)端,參數(shù)為端口
final Socket socket=server.accept(); //accept方法等待客戶端連接
DataInputStream in=new DataInputStream(socket.getInputStream());
String s; //接受客戶端字符串
new Thread(new Runnable() { //開(kāi)啟線程用于服務(wù)端發(fā)送數(shù)據(jù)
@Override
public void run() {
Scanner getin=new Scanner(System.in);
try{
String sendmsg;
DataOutputStream pw =new DataOutputStream(socket.getOutputStream());
while(true){
sendmsg=getin.nextLine(); //控制臺(tái)輸入信息
pw.writeUTF(sendmsg);
}
}catch(Exception r){
}
}
}).start();
while((s=in.readUTF())!=null){
System.out.println("Client "+s); //讀取輸入流
}
}
}
運(yùn)行
先運(yùn)行服務(wù)端加勤,再運(yùn)行客戶端仙辟,即可實(shí)現(xiàn)數(shù)據(jù)互相收發(fā).