Android TCP demo

Android TCP demo,Android 4.4.4测试通过。需要注意的地方,在AndroidManifest.里加一个访问INTERNET的权限。

资源下载地址:http://download.csdn.net/download/shenyuanqing/8237977

效果图


TCPActivity.java

package com.example.androidtcp;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Enumeration;

import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class TCPActivity extends Activity {
	private TextView txtReceiveInfo;
	private EditText edtRemoteIP,edtRemotePort,edtSendInfo,edtLocalPort,edtSeverSendInfo;
	private Button btnConnect,btnSend,btnListen,btnServerSend;
	private boolean isConnected=false,isListened=false;
	private Socket socketClient=null,socket=null;
	private ServerSocket socketServer=null;
	private String receiveInfoClient,receiveInfoServer;
	static BufferedReader bufferedReaderClient	= null,bufferedReaderServer=null;
	static PrintWriter printWriterClient = null,printWriterServer=null;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.tcp);
		btnConnect=(Button)findViewById(R.id.btnConnect);
		btnSend=(Button)findViewById(R.id.btnSend);
		txtReceiveInfo=(TextView)findViewById(R.id.txtReceiveInfo);
		edtRemoteIP=(EditText)findViewById(R.id.edtRemoteIP);
		edtRemotePort=(EditText)findViewById(R.id.edtRemotePort);
		edtSendInfo=(EditText)findViewById(R.id.edtSendInfo);
		btnListen=(Button)findViewById(R.id.btnListen);
		edtLocalPort=(EditText)findViewById(R.id.edtLocalPort);
		btnServerSend=(Button)findViewById(R.id.btnServerSend);
		edtSeverSendInfo=(EditText)findViewById(R.id.edtSeverSendInfo);
	}
	//连接按钮单击事件
	public void ConnectButtonClick(View source)
	{
		if(isConnected)
		{
			isConnected=false;
			if(socketClient!=null)
			{
				try 
				{
					socketClient.close();
					socketClient=null;	
					printWriterClient.close();
					printWriterClient = null;
				} 
				catch (IOException e) 
				{
					// TODO 自动生成的 catch 块
					e.printStackTrace();
				}
			}
			new tcpClientThread().interrupt();
			btnConnect.setText("开始连接");
			edtRemoteIP.setEnabled(true);
			edtRemotePort.setEnabled(true);
			txtReceiveInfo.setText("ReceiveInfo:\n");
		}
		else
		{
			isConnected=true;
			btnConnect.setText("停止连接");
			edtRemoteIP.setEnabled(false);
			edtRemotePort.setEnabled(false);
			new tcpClientThread().start();
		}
	}
	//发送信息按钮单击事件
	public void SendButtonClick(View source)
	{
		if ( isConnected && socketClient!=null) 
		{
			String sendInfo =edtSendInfo.getText().toString();//取得编辑框中我们输入的内容
			try 
			{				    	
		    	printWriterClient.print(sendInfo);//发送给服务器
		    	printWriterClient.flush();
				receiveInfoClient = "发送信息"+"\""+sendInfo+"\""+"\n";//消息换行
				Message msg = new Message();
				msg.what=0x123;
				handler.sendMessage(msg);
			}
			catch (Exception e) 
			{
				receiveInfoClient = e.getMessage() + "\n";
				Message msg = new Message();
				msg.what=0x123;
				handler.sendMessage(msg);
			}
		}
	}
	//TCP客户端线程
	private class tcpClientThread extends Thread 
	{
		public void run()
		{
			try 
			{				
				//连接服务器
				socketClient = new Socket(edtRemoteIP.getText().toString(), Integer.parseInt(edtRemotePort.getText().toString()));	
				//取得输入、输出流
				bufferedReaderClient = new BufferedReader(new InputStreamReader(socketClient.getInputStream()));
				printWriterClient = new PrintWriter(socketClient.getOutputStream(), true);
				receiveInfoClient = "连接服务器成功!\n";
				Message msg = new Message();
				msg.what=0x123;
				handler.sendMessage(msg);	
			}
			catch (Exception e) 
			{
				receiveInfoClient = e.getMessage() + "\n";
				Message msg = new Message();
				msg.what=0x123;
				handler.sendMessage(msg);
				return;
			}			
			char[] buffer = new char[256];
			int count = 0;
			while (isConnected)
			{
				try
				{
					if((count = bufferedReaderClient.read(buffer))>0)
					{						
						receiveInfoClient = "接收信息 "+"\""+getInfoBuff(buffer, count)+"\"" +"\n";//消息换行
						Message msg = new Message();
						msg.what=0x123;
						handler.sendMessage(msg);
					}
				}
				catch (Exception e)
				{
					receiveInfoClient = e.getMessage() + "\n";
					Message msg = new Message();
					msg.what=0x123;
					handler.sendMessage(msg);
					return;
				}
			}
		}
	};
	//TCP服务端线程
	private class tcpServerThread extends Thread
	{
		public void run()
		{
			try 
			{
				socketServer =new ServerSocket(Integer.parseInt(edtLocalPort.getText().toString()));

				receiveInfoServer ="开始监听!"+GetLocalIP()+":"+edtLocalPort.getText().toString()+"(本机地址端口)"+"\n";
				Message msg = new Message();
				msg.what=0x456;
				handler.sendMessage(msg);
			} catch (IOException e) 
			{
				receiveInfoServer = e.getMessage() + "\n";
				Message msg = new Message();
				msg.what=0x456;
				handler.sendMessage(msg);
			}
			while(isListened)
			{
				try 
				{
					socket=socketServer.accept();
					new Thread(new ServerThread(socket)).start();
					String clientIP=socket.getInetAddress().toString();
					clientIP = clientIP.substring(clientIP.lastIndexOf("/") + 1 );
					receiveInfoServer ="连接客户端成功!"+clientIP+"(客户端地址)"+"\n";
					Message msg = new Message();
					msg.what=0x456;
					handler.sendMessage(msg);
				} catch (IOException e) 
				{
					receiveInfoServer = e.getMessage() + "\n";
					Message msg = new Message();
					msg.what=0x456;
					handler.sendMessage(msg);
				}				
			}
		}
	};
	private class ServerThread extends Thread
	{
		private Socket s=null;
		public ServerThread(Socket s)
		{
			this.s=s;
			try 
			{
				bufferedReaderServer = new BufferedReader(new InputStreamReader(s.getInputStream()));
				printWriterServer = new PrintWriter(s.getOutputStream(), true);
				
			} catch (IOException e) 
			{
				// TODO 自动生成的 catch 块
				e.printStackTrace();
			}	
		}
		public void run()
		{
			char[] buffer = new char[256];
			int count = 0;
			while (isListened)
			{
				try
				{
					if((count = bufferedReaderServer.read(buffer))>0)
					{						
						receiveInfoServer = "接收信息"+"\""+getInfoBuff(buffer, count)+"\"" +"\n";//消息换行
						Message msg = new Message();
						msg.what=0x456;
						handler.sendMessage(msg);
					}
				}
				catch (Exception e)
				{
					receiveInfoServer = e.getMessage() + "\n";
					Message msg = new Message();
					msg.what=0x456;
					handler.sendMessage(msg);
					return;
				}
			}		
		}
	}
	Handler handler = new Handler()
	{										
		public void handleMessage(Message msg)										
		{											
			if(msg.what==0x123)
			{
				txtReceiveInfo.append("TCPClient: "+receiveInfoClient);	// 刷新
			}
			if(msg.what==0x456)
			{
				txtReceiveInfo.append("TCPServer: "+receiveInfoServer);		
			}
		}									
	};
	//监听按钮单击事件
	public void ListenButtonClick(View source)
	{
		if(isListened)
		{			
			isListened=false;
			if(socketServer!=null)
			{
				try 
				{
					socketServer.close();
					socketServer=null;
					printWriterServer=null;
				} catch (IOException e) 
				{
					// TODO 自动生成的 catch 块
					e.printStackTrace();
				}
			}
			new tcpServerThread().interrupt();
			btnListen.setText("开始监听");
			edtLocalPort.setEnabled(true);
			txtReceiveInfo.setText("ReceiveInfo:\n");
		}
		else
		{
			isListened=true;
			btnListen.setText("停止监听");
			edtLocalPort.setEnabled(false);
			new tcpServerThread().start();
		}
	}
	//server信息发送按钮单击事件
	public void ServerSendButtonClick(View source)
	{
		if ( isListened && socketServer!=null) 
		{
			String serverSendInfo =edtSeverSendInfo.getText().toString();//取得编辑框中我们输入的内容
			try 
			{				    	
		    	printWriterServer.print(serverSendInfo);//发送给服务器
		    	printWriterServer.flush();
				receiveInfoServer = "发送信息"+"\""+serverSendInfo+"\""+"\n";//消息换行
				Message msg = new Message();
				msg.what=0x456;
				handler.sendMessage(msg);
			}
			catch (Exception e) 
			{
				receiveInfoClient = e.getMessage() + "\n";
				Message msg = new Message();
				msg.what=0x456;
				handler.sendMessage(msg);
			}
		}		
	}
	private String getInfoBuff(char[] buff, int count)
	{
		char[] temp = new char[count];
		for(int i=0; i<count; i++)
		{
			temp[i] = buff[i];
		}
		return new String(temp);
	}
	//得到本地IP地址(WIFI)
	private String GetLocalIP()
	{  
        WifiManager wifiManager = (WifiManager)getSystemService(Context.WIFI_SERVICE);    
        WifiInfo wifiInfo = wifiManager.getConnectionInfo();    
        int ipAddress = wifiInfo.getIpAddress(); 
        if(ipAddress==0)return null;  
        return ((ipAddress & 0xff)+"."+(ipAddress>>8 & 0xff)+"."  
                +(ipAddress>>16 & 0xff)+"."+(ipAddress>>24 & 0xff));  
    } 
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.tc, menu);
		return true;
	}
}
tcp.xml

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:scrollbars="none">

<LinearLayout 
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <TextView 
        android:text="TCP_Client"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />
    <TableLayout 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:stretchColumns="1">
        <TableRow>
        	<TextView 
            	android:layout_width="wrap_content"
            	android:layout_height="wrap_content"
            	android:text="RemoteIP:"/>
        	<EditText
            	android:id="@+id/edtRemoteIP"
            	android:layout_width="match_parent"
            	android:layout_height="wrap_content"
            	android:text="192.168.0.188"/>
        </TableRow>
    </TableLayout>
    <TableLayout 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:stretchColumns="1">    
        <TableRow>
        	<TextView 
            	android:layout_width="wrap_content"
            	android:layout_height="wrap_content"
            	android:text="RemotePort:"/>
        	<EditText
            	android:id="@+id/edtRemotePort"
            	android:layout_width="match_parent"
            	android:layout_height="wrap_content"
            	android:text="10000"/>
        	<Button
        	    android:id="@+id/btnConnect"
        	    android:layout_width="wrap_content"
        	    android:layout_height="wrap_content"
        	    android:text="开始连接"
        	    android:onClick="ConnectButtonClick"/>
        </TableRow>        
        <TableRow>
        	<TextView 
            	android:layout_width="wrap_content"
            	android:layout_height="wrap_content"
            	android:text="SendInfo:"/>
        	<EditText
            	android:id="@+id/edtSendInfo"
            	android:layout_width="match_parent"
            	android:layout_height="wrap_content"
            	android:text="HelloServer"/>
        	<Button
        	    android:id="@+id/btnSend"
        	    android:layout_width="wrap_content"
        	    android:layout_height="wrap_content"
        	    android:text="发送信息"
        	    android:onClick="SendButtonClick"/>
        </TableRow>
	    <TextView 
	        android:layout_width="wrap_content"
	        android:layout_height="wrap_content"
	        android:text="TCP_Server"/> 
	     <TableRow>
          	<TextView 
            	android:layout_width="wrap_content"
            	android:layout_height="wrap_content"
            	android:text="LocalPort:"/>
        	<EditText
            	android:id="@+id/edtLocalPort"
            	android:layout_width="match_parent"
            	android:layout_height="wrap_content"
            	android:text="10000"/>
        	<Button
        	    android:id="@+id/btnListen"
        	    android:layout_width="wrap_content"
        	    android:layout_height="wrap_content"
        	    android:text="开始监听"
        	    android:onClick="ListenButtonClick"/>       
     	</TableRow> 
     	<TableRow>
          	<TextView 
            	android:layout_width="wrap_content"
            	android:layout_height="wrap_content"
            	android:text="SendInfo:"/>
        	<EditText
            	android:id="@+id/edtSeverSendInfo"
            	android:layout_width="match_parent"
            	android:layout_height="wrap_content"
            	android:text="HelloClient"/>
        	<Button
        	    android:id="@+id/btnServerSend"
        	    android:layout_width="wrap_content"
        	    android:layout_height="wrap_content"
        	    android:text="发送信息"
        	    android:onClick="ServerSendButtonClick"/>      	    
     	</TableRow>             
    </TableLayout>
    <ScrollView 
        android:layout_width="match_parent"
        android:layout_height="wrap_content">        
    	<TextView 
        	android:id="@+id/txtReceiveInfo"
        	android:layout_width="match_parent"
        	android:layout_height="match_parent"
        	android:text="ReceiveInfo:\n"/>
    </ScrollView>
</LinearLayout>
</ScrollView>

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.androidtcp"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.androidtcp.TCPActivity"
            android:label="AndroidTCP" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>


评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值