zoukankan      html  css  js  c++  java
  • 与socket联通

    也没什么好说的。看看就行了

    package com.example.webclient;
    
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.PrintStream;
    import java.net.Socket;
    import java.net.UnknownHostException;
    
    import com.example.webclient.R.id;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.os.Handler;
    import android.os.Message;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.TextView;
    
    public class MainActivity extends Activity { 
        private Button link;
        private TextView show;
        private Handler handle = new Handler() {
            public void handleMessage(Message msg) {
            String a=(String) msg.obj;
                    MainActivity.this.show.setText(a) ;
            };
        };
    
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            init();
    
        }
    
        private void init() {
            link = (Button) findViewById(id.link);
            show = (TextView) findViewById(id.show);
            
            link.setOnClickListener(new linkSocket());
        }
    
        public class linkSocket implements OnClickListener {
            public void onClick(View arg0) {
                Thread thread = new Thread(null, inThread, "linkServer");
                thread.start();
            }
        }
        public Runnable inThread = new Runnable() {
    
            public void run() {
                hehe();
            }
    
        };
        public void  hehe() {
            try {
                Socket client = new Socket("192.168.156.1", 8899);
                PrintStream out = new PrintStream(client.getOutputStream());
                BufferedReader buf = new BufferedReader(new InputStreamReader(
                        client.getInputStream()));
                out.println("怎么突然就对了呢") ;    // 向服务器端发送数据
                Message msg = Message.obtain();
                String a =buf.readLine();
                msg.obj=a;
                handle.sendMessage(msg);
                out.close() ;
                buf.close() ;
                client.close() ;
            } catch (Exception e) {
                e.printStackTrace() ;
            }
    
    
        }
    
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.main, menu);
            return true;
        }
    
        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            // Handle action bar item clicks here. The action bar will
            // automatically handle clicks on the Home/Up button, so long
            // as you specify a parent activity in AndroidManifest.xml.
            int id = item.getItemId();
            if (id == R.id.action_settings) {
                return true;
            }
            return super.onOptionsItemSelected(item);
        }
    }

    serverSocket

    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    import java.io.PrintStream;
    import java.net.ServerSocket;
    import java.net.Socket;
    
    public class Myserver {
    
        public static void main(String[] args) throws Exception {
            ServerSocket server = new ServerSocket(8899);
            Socket client = server.accept(); 
            PrintStream out = new PrintStream(client.getOutputStream());
            BufferedReader buf = new BufferedReader(new InputStreamReader(
                    client.getInputStream()));
            StringBuffer info = new StringBuffer() ;
            info.append("恩。") ;
            info.append(buf.readLine()) ;
            out.print(info) ;
            out.close() ;
            buf.close() ;
            client.close() ;
            server.close() ;
        }
    
    }

    readLine刚开始读不出数据,后来又可以了,代码没有改动,不知道是不是eclipse的原因,知道的帮忙解答一下。。。

     

     

     

     

     

  • 相关阅读:
    数据结构-树与二叉树-思维导图
    The last packet successfully received from the server was 2,272 milliseconds ago. The last packet sent successfully to the server was 2,258 milliseconds ago.
    idea连接mysql报错Server returns invalid timezone. Go to 'Advanced' tab and set 'serverTimezone' property
    redis学习笔记
    AJAX校验注册用户名是否存在
    AJAX学习笔记
    JSON学习笔记
    JQuery基础知识学习笔记
    Filter、Listener学习笔记
    三层架构学习笔记
  • 原文地址:https://www.cnblogs.com/mydomainlistentome/p/4704187.html
Copyright © 2011-2022 走看看