前言

最近在处理一些网络相关的东西,学习和温习一下以前的网络编程, 一般的网络编程还是不难的, 以下将组成一个网络编程的系统, 以供自己今后或者有这方面学习的单孩查看..一天一小步

一.服务器端 ImServer.java

 

package com.zhai.server;

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

public class ImServer {

    private int port = 8000;
    private ServerSocket serverSocket;

    public ImServer() throws IOException {
        serverSocket = new ServerSocket(port, 3); // 连接请求队列的长度为3
        System.out.println("服务器启动成功");
    }

    public void service() {

        while (true) {
            Socket socket = null;
            try {
                socket = serverSocket.accept();
                System.out.println("新的连接请求来自:" + socket.getInetAddress() + ":"
                        + socket.getPort());

            } catch (IOException e) {
                e.printStackTrace();
            } finally {

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

    public static void main(String[] args) throws Exception {
        ImServer server = new ImServer();
        // Thread.sleep(60000 * 10); // 睡眠10分钟
        server.service();
    }
}

二,Android客户端的实现Clinet.java

package com.zhai.im.client;

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

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class Client extends Activity {
    protected static final String TAG = "Client";
    /** Called when the activity is first created. */

    Button button;
    final int length = 5;
    String host = "10.2.60.28";
    int port = 8000;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        button = (Button) findViewById(R.id.button1);
        button.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                Socket[] sockets = new Socket[length];

                for (int i = 0; i < length; i++) {

                    try {
                        sockets[i] = new Socket(host, port);
                    } catch (UnknownHostException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }

                    Log.i(TAG, "第" + i + "time connect success");
                }

                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                for (int i = 0; i < length; i++) {
                    try {
                        sockets[i].close();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }
        });

    }
}

三,权限minifest.xml增加一行

  <uses-permission android:name="android.permission.INTERNET"/>

四, 源码打包下载.

本地下载: 本地源码下载