android socket编程手机做服务端,Android中socket编程实现简单的客户端和服务器之间的通信...

服务端代码:

package serversocket;

import java.io.BufferedReader;

import java.io.BufferedWriter;

import java.io.IOException;

import java.io.InputStreamReader;

import java.io.OutputStreamWriter;

import java.io.PrintWriter;

import java.net.ServerSocket;

import java.net.Socket;

public class Server implements Runnable {

@Override

public void run() {

// TODO Auto-generated method stub

//创建serversocket

try {

ServerSocket mServerSocket = new ServerSocket(54321);

while(true){

//接收客户端的请求

Socket client = mServerSocket.accept();

System.out.println("-----------accept------");

//接收客户端消息

BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));

String str = in.readLine();

System.out.println("-------read:---" + str);

//向服务器发送消息

PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(client.getOutputStream())), true);

out.println("server message");

//关闭流

in.close();

out.close();

}

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

public static void main(String[] args) {

Thread mThread = new Thread(new Server());

mThread.start();

}

}

Android客户端代码:

package com.socketclient;

import java.io.BufferedReader;

import java.io.BufferedWriter;

import java.io.IOException;

import java.io.InputStreamReader;

import java.io.OutputStreamWriter;

import java.io.PrintWriter;

import java.net.Socket;

import java.net.UnknownHostException;

import android.app.Activity;

import android.os.Bundle;

import android.os.Handler;

import android.os.Message;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.EditText;

import android.widget.TextView;

public class MainActivity extends Activity implements OnClickListener {

private Button mButton;

private EditText mEditText;

private TextView mTextView;

Handler mHandler = new Handler();

/*Handler mHandler = new Handler(){

public void handleMessage(Message msg) {

//从message对象获取要更新的文本

String  str = String.valueOf(msg.obj);

//更新内容

mTextView.setText(str);

};

};*/

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

mButton = (Button) findViewById(R.id.button1);

mEditText = (EditText) findViewById(R.id.editText1);

mTextView = (TextView) findViewById(R.id.textView1);

mButton.setOnClickListener(this);

}

@Override

public void onClick(View v) {

// TODO Auto-generated method stub

switch (v.getId()) {

case R.id.button1:

final String message = mEditText.getText().toString().trim()

+ "\r\n";

new Thread(new Runnable() {

@Override

public void run() {

// TODO Auto-generated method stub

try {

Socket mSocket = null;

mSocket = new Socket("172.16.41.223", 54321);

// 向服务器发送消息

PrintWriter out = new PrintWriter(new BufferedWriter(

new OutputStreamWriter(mSocket

.getOutputStream())), true);

out.println(message);

// 接收来自服务器的消息

BufferedReader br = new BufferedReader(

new InputStreamReader(mSocket.getInputStream()));

final String msg = br.readLine();

if (msg != null) {

/*// mTextView.setText(msg);

Message message = new Message();

//通过message对象向handler传递要更新的内容

message.obj = msg;

//通过handler更新textview控件的内容

mHandler.sendMessage(message);*/

mHandler.post(new Runnable() {

@Override

public void run() {

// TODO Auto-generated method stub

mTextView.setText(msg);

}

});

} else {

//mTextView.setText("data is wrong");

}

// 关闭流

out.close();

br.close();

mSocket.close();

} catch (UnknownHostException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}).start();

break;

default:

break;

}

}

}

Android中layout布局文件;

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

tools:context="com.socketclient.MainActivity" >

android:id="@+id/editText1"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignParentLeft="true"

android:layout_alignParentTop="true"

android:layout_marginLeft="49dp"

android:layout_marginTop="38dp"

android:ems="10" >

android:id="@+id/textView1"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_above="@+id/button1"

android:layout_alignLeft="@+id/editText1"

android:layout_marginBottom="68dp"

android:textSize="20sp"

android:text="TextView" />

android:id="@+id/button1"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_alignParentBottom="true"

android:layout_centerHorizontal="true"

android:layout_marginBottom="136dp"

android:text="send" />

Android中AndroidManifest.xml中加上访问网络的权限

  • 0
    点赞
  • 1
    收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
©️2022 CSDN 皮肤主题:数字20 设计师:CSDN官方博客 返回首页
评论
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值