android平板与PC之间的socket通信

今天花了一下午,写了一个关于网络通信方面的小程序,实现的是安卓平板和PC之间的socket通信,其中,

安卓平板作为客户端,通过java socket实现,PC是服务端,通过WinSocket实现。

在实现的过程中,要考虑到两者传递中文消息可能会出现乱码的问题,因此需要作一些编码格式的调整。

一开始,我只考虑到客户端在显示服务端返回的中文信息会出现乱码,所以我设置了输入流的编码格式为

GBK,在安卓客户端我是用BufferedReader类的实例来接收服务端返回的消息的,所以直接用这个类自带

的构造方法设置,具体语句如下:

mBufferedReaderClient = new BufferedReader(new InputStreamReader(mSocketClient.getInputStream(),"GBK"));

我以为所用的VS编译器默认编码格式是Unicode,而java socket的编码格式默认就是Unicode,所以在构建

客户端对服务端发送消息的输出流时采用的是PrintWriter类,用的构造方法如下:

mPrintWriterClient = new PrintWriter(mSocketClient.getOutputStream(), true);

然而服务端在显示客户端发来的消息时出现了中文乱码,我第一时间想到的是在服务端设置编码格式,而且

一直以为用的VS编译器默认编码格式是Unicode,然后我百度了一下关于WinSocket中文乱码的解决,别人

说可能是多字符串和宽字符之间转换的问题,所以我开始的解决方法是用windows api中的MultiByteToWideChar,

该api的介绍可以参照以下网址的博客:

http://www.cnblogs.com/ziwuge/archive/2011/11/05/2236968.html

我用这个api设定的编码格式是CP_UTF8,结果还是出现中文乱码的问题,本来还想设置成GBK看看,发现

这个api并没有这个功能,所以只好作罢。后来我就尝试在客户端的输出流上做改变,我将PrintWriter换成了

PrintStream类,对于这两者的区别读者可自行百度,具体改变如下:

mPrintStreamClient = new PrintStream(mSocketClient.getOutputStream(), true, "GBK");

结果服务端显示中文消息正常了,我一想,不对啊,假如客户端我设置为GBK,但是服务端的VS编译器如果

是Unicode,两者编码格式不统一,还是不会正常啊,不曾想,去看了一下编译器的默认编码格式,居然是GBK,

我的锅咯!还好问题解决了,我心满意足了!希望大家不要犯跟我一样的错。下面贴上全部代码供大家参考。

客户端:

MainActivity.java:

main.xml:

<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <RelativeLayout 
        android:id="@+id/editLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        
        <LinearLayout 
            android:id="@+id/IPEdit"
            android:layout_width="match_parent"
        	android:layout_height="wrap_content"
        	android:orientation="horizontal">
            
            <TextView
                android:id="@+id/IPtv"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="IP地址:"/>
            
            <EditText
                android:id="@+id/IPEditText"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:hint="192.168.191.2"/>
            
        </LinearLayout>
        
        <LinearLayout 
            android:id="@+id/portEdit"
            android:layout_below="@+id/IPEdit"
            android:layout_width="match_parent"
        	android:layout_height="wrap_content"
        	android:orientation="horizontal">
            
            <TextView
                android:id="@+id/portTv"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="端口号:"/>
            
            <EditText
                android:id="@+id/PortEditText"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:hint="8888"/>
            
        </LinearLayout>
        
        <LinearLayout 
            android:id="@+id/messageEdit"
            android:layout_below="@+id/portEdit"
            android:layout_width="match_parent"
        	android:layout_height="wrap_content"
        	android:orientation="horizontal">
            
            <TextView
                android:id="@+id/messageTv"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="发送消息:"/>
            
            <EditText
                android:id="@+id/messageEditText"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:hint="hello!"
                />
            
        </LinearLayout>
        
    </RelativeLayout>
    
    <RelativeLayout 
        android:id="@+id/buttonLayout"
        android:layout_below="@+id/editLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        
        <Button
            android:id="@+id/connectBt"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="连接服务"
            />
        
        <Button
            android:id="@+id/disconnectBt"
            android:layout_toRightOf="@+id/connectBt"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="断开连接"
            />
        
        <Button
            android:id="@+id/sendBt"
            android:layout_toRightOf="@+id/disconnectBt"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="发送消息"
            />
        
    </RelativeLayout>
    
    <RelativeLayout 
        android:id="@+id/messageLayout"
        android:layout_below="@+id/buttonLayout"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        
    	<TextView
        	android:id="@+id/messageView"
        	android:layout_width="fill_parent"
        	android:layout_height="fill_parent"
        	android:text="服务器反馈信息:\n"
        	/>
    
    </RelativeLayout>

</RelativeLayout>
别忘了在AndroidManifest.xml中声明权限:

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

对了,在实现Android端的网络连接等耗时操作应该另开一个线程,我的代码就是这样。

服务端:

本服务端代码是WinSocket多线程版的,其中的ip地址的宏定义应根据你自己需要去设置。

希望这篇博客能帮到大家,谢谢!

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值