zoukankan      html  css  js  c++  java
  • Android自学笔记(二)基于Socket实现Android与PC通信

    TCP协议简介什么的百度一大把,这里直接粘代码……参考了网上找来的源码

    PC端用Java
    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.InputStreamReader;
    import java.io.OutputStreamWriter;
    import java.io.PrintWriter;
    import java.net.ServerSocket;
    import java.net.Socket;
    
    public class TCPDesktopServer implements Runnable{
    
     
        public static final String SERVERIP = "192.168.155.1";
    
        public static final int SERVERPORT = 1818;
    
     
        public void run() {
    
             try {
    
                 System.out.println("S: Connecting...");
    
                 ServerSocket serverSocket = new ServerSocket(SERVERPORT);
    
                 while (true) {
    
                      Socket client = serverSocket.accept();
    
                      try {
    
                          BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
    
                          String str = in.readLine();
    
                          System.out.println("S: Received: '" + str + "'");
     
                        } catch(Exception e) {
    
                            System.out.println("S: Error");
    
                            e.printStackTrace();
    
                        } finally {
    
                            client.close();
    
                            System.out.println("S: Done.");
    
                        }
    
                 }
    
              } catch (Exception e) {
    
                 System.out.println("S: Error");
    
                 e.printStackTrace();
             }
        }
     
       public static void main (String a[]) {
    
            Thread ServerThread = new Thread(new TCPDesktopServer());
    
            ServerThread.start();
        }
    

    Android
    package com.socket.client;
    
    
    import java.io.*;
    import java.net.Socket;
    import java.net.UnknownHostException;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.text.TextUtils;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.Toast;
    
    public class main extends Activity{
        /** Called when the activity is first created. */
    	
    	private EditText Msgcontainer;
    	private Button BtnSend;
    	private String ip="192.168.155.1";
    	private int port=1818;
    	
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            SetOnClick();
        }
        
        private void SetOnClick()
        {
    
        	Msgcontainer=(EditText)findViewById(R.id.msg_container);
        	BtnSend=(Button)findViewById(R.id.btn_send);
        	
        	BtnSend.setOnClickListener(new OnClickListener() {
    			
    			@Override
    			public void onClick(View v) {
    				// TODO Auto-generated method stub
    				try 
    				{
    					String msg=Msgcontainer.getText().toString();
    					if(!TextUtils.isEmpty(msg))
    						SendMsg(ip,port,msg);
    					else
    					{
    						Toast.makeText(main.this,"msg is EMPTY!!", Toast.LENGTH_LONG).show();
    						Msgcontainer.requestFocus();
    					}
    				}
    			 catch (Exception e) {
    				// TODO Auto-generated catch block
    				e.printStackTrace();
    			 	}
    			}
    		});
        }
        
        public void SendMsg(String ip,int port,String msg) throws UnknownHostException, IOException
        {
        	try
        	{
        	Socket socket=null;
        	socket=new Socket(ip,port);
        	BufferedWriter writer=new BufferedWriter(new OutputStreamWriter
        			(socket.getOutputStream()));
        	writer.write(msg);
        	writer.flush();
        	writer.close();       
        	socket.close();
        	}
        	catch(UnknownHostException e)
        	{
        		e.printStackTrace();
        	} catch (IOException e) 
        	{
        	    e.printStackTrace();
            }
        }
    }



  • 相关阅读:
    python的gui库tkinter
    python图像处理库Pillow基本使用方法
    github配置SSH proxy
    python的pandas库读取csv
    学习app开发思路
    shell脚本中四则运算
    shell脚本实例三
    shell脚本实例二
    shell脚本实例一
    LINUX系统下的shell命令---grep、sed、awk
  • 原文地址:https://www.cnblogs.com/ldgforever/p/5854102.html
Copyright © 2011-2022 走看看