tCPServer.java
服务器的两个socket都没有关闭:s,ss.两个都是一直开启,然后数据才有显示
class tcpServer
{
public static void main(String[] args) throws IOException{ //建立服务端socket服务,并监听端口
ServerSocket ss =new ServerSocket(30000);
// 采用循环不断接受来自客户端的请求
while (true)
{
//通过accept方法获取链接过来的客户端对象(s中有内容,端口,IP属性)
Socket s = ss.accept();
/*接收手机数据*/
//IP:
String ip =s.getInetAddress().getHostAddress();
System.out.println(ip+"...连接成功" );
//内容:获取客户端发送过来的数据,那么要使用客户端对象s
InputStream in = s.getInputStream(); byte[] buf =new byte[1024];
int len=in.read(buf);
String content = new String(buf,0,len);
System.out.println("内容:"+content );
/*回发给手机数据*/
OutputStream os = s.getOutputStream();
os.write("欢迎回来学安卓,您收到了泡泡的祝福!\n"
.getBytes("utf-8"));
//s.close();//关闭客户端,服务器可以控制客户
//ss.close();//关闭服务端,可选操作
}
}
}
安卓后台代码:
package com.simpleclient; import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket; import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText; public class MainActivity extends Activity { EditText show;
String content;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); show = (EditText) findViewById(R.id.show);
new Thread()
{
@Override
public void run()
{
try
{
// 建立连接到远程服务器的Socket
Socket socket = new Socket("192.168.1.108" , 30000); //①
String lineWrite = "tcp:mobilephone/3.1415926";
//向服务端发送数据
BufferedWriter bufwriter = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())); bufwriter.write(lineWrite);//向服务端发送数据
bufwriter.newLine();
bufwriter.flush(); // 将Socket对应的输入流包装成BufferedReader
BufferedReader br = new BufferedReader(
new InputStreamReader(socket.getInputStream()));
// 进行普通IO操作
String line = br.readLine();
show.setText("读取来自服务器的数据:" + line); // 关闭输入流、socket
br.close();
socket.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}.start(); } }
前台代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
> <EditText
android:id="@+id/show"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:cursorVisible="false"
android:editable="false"
android:ems="10" > <requestFocus />
</EditText> </LinearLayout>
权限代码:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.simpleclient"
android:versionCode="1"
android:versionName="1.0" > <uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="19" /> <application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application> </manifest>
总体而言,实现了服务器和客户端一发一收的效果,但是中文有乱码现象产生,并且不严谨,没有类封装