zoukankan      html  css  js  c++  java
  • socket的简单通信

    ///客户端

    package com.ch.day11_myclient;

    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.PrintStream;
    import java.io.PrintWriter;
    import java.net.Socket;
    import java.net.UnknownHostException;

    import com.lidroid.xutils.BitmapUtils;
    import com.lidroid.xutils.HttpUtils;
    import com.lidroid.xutils.exception.HttpException;
    import com.lidroid.xutils.http.ResponseInfo;
    import com.lidroid.xutils.http.callback.RequestCallBack;
    import com.lidroid.xutils.http.client.HttpRequest;

    import android.os.Bundle;
    import android.os.Handler;
    import android.app.Activity;
    import android.app.ProgressDialog;
    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.ImageView;
    import android.widget.TextView;

    public class MainActivity extends Activity {
    private TextView allmessage;
    private EditText say;
    private Button send;

    private Socket client;
    BufferedReader br;
    PrintWriter pw;
    String sayData;

    Handler h = new Handler(){
    public void handleMessage(android.os.Message msg) {
    String str = (String)msg.obj;
    allmessage.setText(allmessage.getText().toString()+" "+str);
    // allmessage.append(str);
    };
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    init();
    }

    public void init(){
    allmessage = (TextView) findViewById(R.id.allmessage);
    say = (EditText) findViewById(R.id.say);
    send = (Button) findViewById(R.id.send);

    //建立和服务器的连接
    try {
    client = new Socket("169.254.109.73", 8888);//向169.254.109.73服务器的8888端口发送请求
    //获得服务器端的信息,先创建服务器的输入流
    br = new BufferedReader(new InputStreamReader(client.getInputStream()));
    //向服务器发送消息,先创建输出流
    pw = new PrintWriter(client.getOutputStream());
    } catch (UnknownHostException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
    } catch (IOException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
    }
    //启动一个线程,随时接收服务器的信息
    new Thread(){
    public void run() {
    try {
    while(true){
    String str = br.readLine();//读取当前服务器的一句话,读到为止,读不到不会继续向下走
    Log.i("TAG", str);
    //把str显示到allmessage
    h.sendMessage(h.obtainMessage(0x123, str));
    }
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    };
    }.start();
    //点击发送,发送消息
    send.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
    //得到发送的信息
    sayData = say.getText().toString();
    new Thread(){
    public void run() {
    Log.i("TAG", "11111111::::"+sayData);
    pw.println(sayData);
    pw.flush();//及时刷新,推送过去
    };
    }.start();
    }
    });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
    }

    }

    ///服务端

    package com.ch;

    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.PrintStream;
    import java.io.PrintWriter;
    import java.net.ServerSocket;
    import java.net.Socket;
    import java.util.ArrayList;

    public class MyServer {

    /**
    * @param args
    */
    public static void main(String[] args) {
    // TODO Auto-generated method stub
    final ArrayList<Socket> ss = new ArrayList<Socket>();
    try {
    //启动服务器
    ServerSocket server = new ServerSocket(8888);
    while(true){
    System.out.println("等待接收一个新的客户端的请求..............");
    //等待接收一个客户端,接收到为止,代码才会继续向下执行
    final Socket client = server.accept();
    System.out.println("接收到了一个新的客户端的请求,ip:"+client.getInetAddress().toString());
    ss.add(client);//把当前接收到的客户端,收集起来
    //启动一个线程,接收当前客户端的信息,转发给其他客户端
    new Thread(){
    public void run() {
    try {
    //获得客户端的信息,先创建客户端的输入流
    BufferedReader br = new BufferedReader(new InputStreamReader(client.getInputStream()));
    while(true){//有多条信息,需要循环读取
    String data = br.readLine();//读取当前客户的信息
    //把当前信息转发给,其他客户端,自己除外
    //遍历集合获得所有客户端
    for(Socket sk:ss){
    if(sk != client){//自己除外
    //创建一个兄弟客户端的输出流
    PrintWriter pw = new PrintWriter(sk.getOutputStream());
    pw.println(client.getInetAddress().toString()+"--say:"+data);
    pw.flush();

    }
    }

    }
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    };
    }.start();

    }



    // PrintWriter pw = new PrintWriter(client.getOutputStream());
    // while(true){
    // String str = br.readLine();//读取当前客户端的一句话,没有读到,此行代码不行执行完
    // System.out.println(client.getInetAddress().toString()+"say::"+str);
    // //向客户端发送消息,先创建输出流
    // pw.println(str+",是吗?");
    // pw.flush();//及时刷新,推送过去
    //
    // }


    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }

    }

    }

  • 相关阅读:
    postfix遇到的问题
    SElinux以及防火墙的关闭
    centos查看系统信息
    WINDOWS访问SAMBA提示没有权限
    常用命令
    口才
    【李敖的管理经】
    随笔
    查询MX记录
    bash: ifconfig: command not found
  • 原文地址:https://www.cnblogs.com/jsonfan/p/5129358.html
Copyright © 2011-2022 走看看