android socket实现简单的C/S通信

15,552 views

android socket实现简单的C/S通信。

1.服务器端

SocketServer .java

package com.test.clientmov;

import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class SocketServer {
public static void main(String[] args) {
new ServerThread().start();
}
}

class ServerThread extends Thread {
public void run() {
// 声明一个ServerSocket对象
ServerSocket serverSocket = null;
try {
// 创建一个ServerSocket对象,并让这个Socket在8888端口监听
serverSocket = new ServerSocket(8888);
while (true) {
// 调用ServerSocket的accept()方法,接受客户端所发送的请求
Socket socket = serverSocket.accept();
// 从Socket当中得到InputStream对象
InputStream inputStream = socket.getInputStream();
byte buffer[] = new byte[1024 * 4];
int temp = 0;
// 从InputStream当中读取客户端所发送的数据
while ((temp = inputStream.read(buffer)) != -1) {
System.out.println("服务端获取客户端的数据:\n"
+ new String(buffer, 0, temp));
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
serverSocket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}

2.android客户端

SocketClientActivity .java

package com.socketclient.test;

import java.io.OutputStream;
import java.net.Socket;


import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class SocketClientActivity extends Activity {
/** Called when the activity is first created. */
private Button sendBtn ;
private EditText inputEt;
OutputStream outputStream = null;
Socket socket;
int i = 0;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
inputEt=(EditText) findViewById(R.id.input);
sendBtn = (Button) findViewById(R.id.send);
sendBtn.setOnClickListener(new StartSocketListener());
try {
// 创建一个Socket对象,指定服务器端的IP地址和端口号
socket = new Socket("10.0.2.2", 8888);
// 从Socket当中得到OutputStream
outputStream = socket.getOutputStream();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

class StartSocketListener implements OnClickListener {

@Override
public void onClick(View v) {

String str = inputEt.getText().toString()+"------来自客户端文本输入发送次数:" + i++ + "次";
try {
socket.getOutputStream();
byte buffer[] = str.getBytes("UTF-8");
outputStream.write(buffer);
outputStream.flush();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.socketclient.main"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="7" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name="com.socketclient.main.SocketClientActivity"
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>
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
</manifest>

main.xml

<?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/input"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<Button  android:id="@+id/send"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="客户端发送数据" />
</LinearLayout>

1.首先运行SocketServer .java类(可以单独建立项目运行,不放在android目录内),启动服务器端,如果重新运行时出现Address already in use: JVM_Bind异常,在任务管理器中关掉javaw.exe进程;

2.运行android客户端程序,输入内容,可以在服务器端打印输入的数据,如图-2;

3.该程序有待完善,更新,请勿转载。

图-1 android客户端发送数据

图-2 服务器端获取客户端输入数据并打印

This entry was posted in android, socket and tagged by Charlie. Bookmark the permalink.

9 thoughts on “android socket实现简单的C/S通信

    • 不是,下载到手机运行意味着手机访问外网,192.168.1.106局域网IP手机是无法访问的,而且服务器端的程序应该部署在服务器上运行。

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>