android 通过socket连接服务器与客户端

来源:互联网 发布:中原农险数据脱敏 编辑:程序博客网 时间:2024/05/22 09:59
//android 客户端的Activity
package com.example.administrator.socket;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.view.View;import android.widget.Button;import android.widget.EditText;import android.widget.TextView;import java.io.IOException;import java.io.OutputStreamWriter;import java.io.PrintWriter;import java.net.InetAddress;import java.net.Socket;import java.net.UnknownHostException;public class MainActivity extends AppCompatActivity {    private static final String IAddress="192.168.23.1";//本机的ip    private static final int Port=11120;//在本机找一个端口,尽量大一点,避免与本地的端口冲突    private TextView textView;    private Button button;    private EditText editText;    Socket socket=null;    private PrintWriter writer=null;    InetAddress inetAdd= null;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        initia();        button.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                writer.println(editText.getText().toString());//将数据向服务器发送                writer.flush();                editText.setText("");            }        });    }    void initia() {        button = (Button) findViewById(R.id.btnSend);        textView = (TextView) findViewById(R.id.tv_show);        editText = (EditText) findViewById(R.id.ed_get);        new Thread(){            @Override            public void run() {                try {                    inetAdd = InetAddress.getByName(IAddress);                    socket = new Socket(inetAdd,11120);//以Socket(InetAddress,Port)方法创建一个socket,链接本地的11120(自己可以随便写,在0~65535)端口.                    writer=new PrintWriter(new OutputStreamWriter(socket.getOutputStream()),true);//获取向服务器方向写入的PrintWriter                } catch (UnknownHostException e) {                    e.printStackTrace();                } catch (IOException e) {                    e.printStackTrace();                }            }        }.start();    }}
//xml文件
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:id="@+id/activity_main"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"    tools:context="com.example.administrator.socket.MainActivity">    <TextView        android:id="@+id/tv_show"        android:layout_marginTop="20dp"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="Hello World!" />    <EditText        android:id="@+id/ed_get"        android:paddingLeft="30dp"        android:hint="请输入如要发出的的消息"        android:layout_width="match_parent"        android:layout_height="wrap_content" />    <Button        android:id="@+id/btnSend"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="发送消息"/></LinearLayout>
//服务器端
package socket0;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.net.ServerSocket;import java.net.Socket;public class ssss { protected  static ServerSocket serverSocket; static Socket socket; protected static BufferedReader bufferedReader=null; public static void main(String[] args) {   try {   serverSocket = new ServerSocket(11120);//创建一个ServerSocket  } catch (IOException e1) {   e1.printStackTrace();  }  new Thread(){   public void run() {    try {     socket= serverSocket.accept();//接受一个serverSocket.accept()返回的socket。     System.out.println("服务器成功与客户端连接!");     bufferedReader=new BufferedReader(       new InputStreamReader(         socket.getInputStream(),"UTF-8"));//获取读取数据的bufferedReader     while(true){      if(bufferedReader!=null){       String string;       try {        if((string = bufferedReader.readLine())!=null){         System.out.println("接收到客户端的消息:"+string);//如果有数据就打印        }       } catch (IOException e) {        e.printStackTrace();       }       }     }    } catch (IOException e) {     e.printStackTrace();    }     };  }.start();  }}
0 0
原创粉丝点击