Java服务端程序代码:TcpSocketServer.java
package com.example.tcpsocketserver;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
public class TcpSocketServer
{
public static void main(String[] args)throws IOException
{
// TODO Auto-generated method stub
//1.创建一个服务器端Socket,即ServerSocket,指定绑定的端口,并监听此端口
ServerSocket serverSocket = new ServerSocket(12345);
InetAddress address = InetAddress.getLocalHost();
String ip = address.getHostAddress();
Socket socket = null;
//2.调用accept()等待客户端连接
System.out.println("~~~服务端已就绪,等待客户端接入~,服务端ip地址: " + ip);
socket = serverSocket.accept();
//3.连接后获取输入流,读取客户端信息
InputStream is=null;
InputStreamReader isr=null;
BufferedReader br=null;
OutputStream os=null;
PrintWriter pw=null;
is = socket.getInputStream(); //获取输入流
isr = new InputStreamReader(is,"UTF-8");
br = new BufferedReader(isr);
String info = null;
while((info=br.readLine())!=null)
{
//循环读取客户端的信息
System.out.println("客户端发送过来的信息" + info);
}
socket.shutdownInput();//关闭输入流
socket.close();
serverSocket.close();
}
}
Android客户端主程序的程序代码:MainActivity.java
package com.example.tcpsocketclientforandroid;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.Button;
import android.os.Bundle;
public class MainActivity extends ActionBarActivity implements View.OnClickListener
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn_accept = (Button) findViewById(R.id.btn_accept);
btn_accept.setOnClickListener(this);
}
@Override
public void onClick(View v)
{
new Thread()
{
@Override
public void run()
{
try
{
acceptServer();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}.start();
}
private void acceptServer() throws IOException
{
//1.创建客户端Socket,指定服务器地址和端口,其中服务器地址是本机ip,端口自定义.
Socket socket = new Socket("192.168.31.160", 12345);
//2.获取输出流,向服务器端发送信息
OutputStream os = socket.getOutputStream();//字节输出流
PrintWriter pw = new PrintWriter(os);//将输出流包装为打印流
//获取客户端的IP地址
InetAddress address = InetAddress.getLocalHost();
String ip = address.getHostAddress();
pw.write("客户端:~" + ip + "~ 接入服务器!!");
pw.flush();
socket.shutdownOutput();//关闭输出流
socket.close();
}
}
Android客户端界面布局的程序代码:activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.tcpsocketclientforandroid.MainActivity" >
<Button
android:id="@+id/btn_accept"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/accept" />
</LinearLayout>
Android客户端编辑字符串的程序代码:strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">TcpSocketClientForAndroid</string>
<string name="hello_world">Hello world!</string>
<string name="action_settings">Settings</string>
<string name="accept">Accept</string>
</resources>
Android客户端程序配置的程序代码:AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.tcpsocketclientforandroid"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.INTERNET" />
<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>
(1)、鼠标右键点击TcpSocketServerForJava项目——鼠标光标移动到Run As项——点击Java Application项
(2)、点击Java Application项后,Console栏打印输出信息。
(3)、鼠标右键点击TcpSocketClientForAndroid项目——鼠标光标移动到Run As项——点击Android Application项。
(4)、点击Android Application项后,弹出TcpSocketClientForAndroid项目的Android模拟器客户端界面。
(5)、点击Android模拟器客户端界面上的Accept按钮
(6)、点击Android模拟器客户端界面上的Accept按钮后,Console栏打印输出信息。