需要用一台windows电脑做服务器,在android端与其进行数据交换,实现一些业务。 简单起见,用java写这个服务器,以前没做过,试试水。很简单的代码,纯粹找思路。 服务器端代码:
android端代码:
- package com.teat;
- import java.io.IOException;
- import java.io.InputStream;
- import java.net.Socket;
- import java.net.UnknownHostException;
- import android.app.Activity;
- import android.os.Bundle;
- import android.widget.TextView;
- public class TestSocketActivity extends Activity {
- private TextView myTextView;
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- myTextView = (TextView) findViewById(R.id.textView1);
- try {
- myTextView.setText("0");
- Socket socket = new Socket("192.168.1.100", 8888);
- myTextView.setText("1");
- InputStream in = socket.getInputStream();
- byte[] buffer = new byte[in.available()];
- myTextView.setText("2");
- in.read(buffer);
- myTextView.setText("3");
- String msg = new String(buffer);
- myTextView.setText(msg);
- } catch (UnknownHostException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
方便起见,ip地址是写死的,通过dos窗口下的ipconfig指令查看自己电脑的本地ip,然后把下面这行代码里的ip修改成你电脑的ip就可以正常通信了。
http://www.huihui.cn/share/20288978
http://www.huihui.cn/share/20285986http://www.huihui.cn/share/20284686
http://www.huihui.cn/share/20284296
http://www.huihui.cn/share/20283854
http://www.huihui.cn/share/20283594
http://www.huihui.cn/share/20283210
http://www.huihui.cn/share/20282514
http://www.huihui.cn/share/20281972
http://www.huihui.cn/share/20281288
http://www.huihui.cn/share/20279550
http://www.huihui.cn/share/20279304
http://www.huihui.cn/share/20279040
http://www.huihui.cn/share/20278624
http://www.huihui.cn/share/20277748
http://www.huihui.cn/share/20277384
http://www.huihui.cn/share/20276700
http://www.huihui.cn/share/20275530
http://www.huihui.cn/share/20274876
http://www.huihui.cn/share/20273868
http://www.huihui.cn/share/20273574
http://www.huihui.cn/share/20273054
http://www.huihui.cn/share/20272624
http://www.huihui.cn/share/20272268
http://www.huihui.cn/share/20271078
http://www.huihui.cn/share/20270346
http://www.huihui.cn/share/20269132
http://www.huihui.cn/share/20268370
http://www.huihui.cn/share/20267656
http://www.huihui.cn/share/20266974
http://www.huihui.cn/share/20266554
http://www.huihui.cn/share/20266034
http://www.huihui.cn/share/20265530
http://www.huihui.cn/share/20265302
http://www.huihui.cn/share/20264912
http://www.huihui.cn/share/20264412
http://www.huihui.cn/share/20264190
http://www.huihui.cn/share/20288022
http://www.huihui.cn/share/20286140
http://www.huihui.cn/share/20284862
http://www.huihui.cn/share/20284398
http://www.huihui.cn/share/20283948
http://www.huihui.cn/share/20283690
http://www.huihui.cn/share/20282792
http://www.huihui.cn/share/20282462
http://www.huihui.cn/share/20282158
http://www.huihui.cn/share/20281600
http://www.huihui.cn/share/20279640
http://www.huihui.cn/share/20279374
http://www.huihui.cn/share/20279122
http://www.huihui.cn/share/20278740
http://www.huihui.cn/share/20277906
http://www.huihui.cn/share/20277492
http://www.huihui.cn/share/20276802
http://www.huihui.cn/share/20275654
http://www.huihui.cn/share/20275066
http://www.huihui.cn/share/20274028
http://www.huihui.cn/share/20273666
http://www.huihui.cn/share/20273168
http://www.huihui.cn/share/20272512
http://www.huihui.cn/share/20272222
http://www.huihui.cn/share/20271030
http://www.huihui.cn/share/20268220
http://www.huihui.cn/share/20269302
http://www.huihui.cn/share/20267794
http://www.huihui.cn/share/20267086
http://www.huihui.cn/share/20266668
http://www.huihui.cn/share/20266200
http://www.huihui.cn/share/20265660
http://www.huihui.cn/share/20265344
http://www.huihui.cn/share/20265108
http://www.huihui.cn/share/20264368
http://www.huihui.cn/share/20289090
http://www.huihui.cn/share/20284782
http://www.huihui.cn/share/20284352
http://www.huihui.cn/share/20283896
http://www.huihui.cn/share/20283552
http://www.huihui.cn/share/20282560
http://www.huihui.cn/share/20282040
http://www.huihui.cn/share/20281454
http://www.huihui.cn/share/20279580
http://www.huihui.cn/share/20279342
http://www.huihui.cn/share/20279078
http://www.huihui.cn/share/20278686
http://www.huihui.cn/share/20277800
http://www.huihui.cn/share/20277442
http://www.huihui.cn/share/20276758
http://www.huihui.cn/share/20275574
http://www.huihui.cn/share/20275006
http://www.huihui.cn/share/20273916
http://www.huihui.cn/share/20273622
http://www.huihui.cn/share/20273114
http://www.huihui.cn/share/20272578
http://www.huihui.cn/share/20272312
http://www.huihui.cn/share/20271278
http://www.huihui.cn/share/20270412
http://www.huihui.cn/share/20269184
http://www.huihui.cn/share/20268166
http://www.huihui.cn/share/20267736
http://www.huihui.cn/share/20267018
http://www.huihui.cn/share/20266606
http://www.huihui.cn/share/20266152
http://www.huihui.cn/share/20265612
http://www.huihui.cn/share/20265322
http://www.huihui.cn/share/20264950
http://www.huihui.cn/share/20264464
http://www.huihui.cn/share/20264296
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。