android Socket

服务器端代码

复制代码
public class Server implements Runnable
{
    public void run()
    {
        try
        {
            //创建ServerSocket
            ServerSocket serverSocket = new ServerSocket(54321);
            while (true)
            {
                //接受客户端请求
                Socket client = serverSocket.accept();
                System.out.println("accept");
                try
                {
                    //接收客户端消息
                    BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
                    String str = in.readLine();
                    System.out.println("read:" + str);    
                    //向服务器发送消息
                    PrintWriter out = new PrintWriter( new BufferedWriter( new OutputStreamWriter(client.getOutputStream())),true);      
                    out.println("server message"); 
                    //关闭流
                    out.close();
                    in.close();
                }
                catch (Exception e)
                {
                    System.out.println(e.getMessage());
                    e.printStackTrace();
                }
                finally
                {
                    //关闭
                    client.close();
                    System.out.println("close");
                }
            }
        }
        catch (Exception e)
        {
            System.out.println(e.getMessage());
        }
    }
    //main函数,开启服务器
    public static void main(String a[])
    {
        Thread desktopServerThread = new Thread(new Server());
        desktopServerThread.start();
    }
}
复制代码

android应用端:

UI

复制代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/messageText"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />

    <EditText
        android:id="@+id/messageEdit"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <requestFocus />
    </EditText>

    <Button
        android:id="@+id/sendButton"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Button" />

</LinearLayout>
复制代码

主页面

复制代码
public class MainActivity extends Activity {
    
    private EditText messageEdit = null;
    private Button sendButton = null;
    private TextView messageText = null;
    
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        messageEdit = (EditText)findViewById(R.id.messageEdit);
        messageText = (TextView)findViewById(R.id.messageText);
        
        sendButton = (Button)findViewById(R.id.sendButton);
        sendButton.setOnClickListener(new Button.OnClickListener(){

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Socket socket = null;
                String str = messageEdit.getText().toString();
                try {
                    //创建Socket
                    socket = new Socket("10.200.194.29", 54321);
                    //向服务器发送消息
                    PrintWriter out = new PrintWriter(new PrintWriter(socket.getOutputStream()), true);
                    out.println(str);
                    
                    //接收来自服务器的消息
                    BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                    String msg = br.readLine();
                    if(msg != null){
                        messageText.setText(msg);
                    }
                    else{
                        messageText.setText("数据错误!");
                    }
                    //关闭流
                    out.close();
                    br.close();
                    //关闭Socket
                    socket.close();
                    
                } catch (Exception e) {
                    // TODO: handle exception
                    System.out.println(e.toString());
                }
                    
            }
            
        });
        
    }
}
复制代码

运行截图

posted @   似水流云  阅读(289)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 因为Apifox不支持离线,我果断选择了Apipost!
· 通过 API 将Deepseek响应流式内容输出到前端
点击右上角即可分享
微信分享提示