yearHeaven的博客

专注IT 专注技术

Android网络编程Socket通信

 Java Code 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
Socket
事实上和JavaWeb 里面的Socket一模一样
建立客服端,server端,server开一个port供客服端访问


第一步创建server端:(这里把为了便于解说。把server端,和客服端都放在手机上了)
创建Android project socketserver

package com.example.socketserver;

import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;

import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
/**
 * 创建服务端
 *
 * */


public class MainActivity extends Activity
{

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        AsyncTask.execute(new Runnable()
        {
            @Override
            public void run()
            {
                startService();//访问网络
            }
        });
    }
    /*
     * 服务端从客服端接收数据
     *
     * */

    public void startService()
    {
        Socket socket = null;
        InputStream inputStream = null;
        try
        {
            ServerSocket serverSocket = new ServerSocket(1234);//填入端口号
            socket = serverSocket.accept();//接收客服端的的连接请求

            inputStream = socket.getInputStream();//获取输入流

            /*
             * 从输入流中读取数据
             *
             * */

            byte[] bs = new byte[1024];
            int i = -1;
            while((i = inputStream.read(bs)) != -1)
            {
                System.out.println(new String(bs, 0, i));

            }

        }
        catch (IOException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        finally
        {
            if(inputStream != null)
            {
                try
                {
                    inputStream.close();
                }
                catch (IOException e)
                {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            if(socket != null)
                try
                {
                    socket.close();
                }
                catch (IOException e)
                {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
        }
    }
}

第二步搭建Socket客服端

package com.example.socketclient;

import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;

import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

public class MainActivity extends Activity
{

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        AsyncTask.execute(new Runnable()
        {

            @Override
            public void run()
            {
                startClient();
            }
        });
    }
    public void startClient()
    {
        OutputStream out = null;
        Socket socket = null;
        try
        {
            socket = new Socket("127.0.0.1"1234);
            out = socket.getOutputStream();//获取输出流

            out.write("abc".getBytes());//把相当于客服端数据写到server端

            //虚拟机(手机)相当于一个电脑127.0.0.1是訪问手机自己,1234是server开的端口号

        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            if(out != null)
            {
                try
                {
                    out.close();
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }//关闭
            }
            if(socket != null)
            {
                try
                {
                    socket.close();
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }
            }

        }
    }
}
阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/yearHeaven/article/details/80686677
文章标签: Socket android
个人分类: Android-FL
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭