如何在java android中创建网络流?

时间:2012-10-12 15:10:39

标签: java android networking tcp networkstream

有没有办法在java中创建一个网络流数组。 C#支持创建网络流阵列。

AFAIK我们需要创建单独的InputStreams和OutputStreams,以便用Java接收和发送数据。

我想要做的是建立一些TCP连接来发送和接收数据。

在这方面有解决方法吗?

1 个答案:

答案 0 :(得分:0)

您可以通过创建多个Socket实例来实现这一目的:

<强> Server.java

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

public class Server
{
    private static int CLIENT_COUNT = 0;

    public static void main(String[] args) throws IOException
    {
        ServerSocket ss = new ServerSocket(2000);
        while(true)
        {
            Socket s = ss.accept();
            new SocketHandler("Handler#" + ++CLIENT_COUNT, s).start();
        }
    }
}

class SocketHandler extends Thread
{
    private PrintWriter pw;
    private BufferedReader br;

    public SocketHandler(String name, Socket socket) throws IOException
    {
        super(name);
        this.pw = new PrintWriter(socket.getOutputStream(), true);
        this.br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
    }

    @Override
    public void run()
    {
        String message;
        try
        {
            while((message = br.readLine()) != null)
            {
                System.out.println("Server#" + getName() + " - Client sent: " + message);
                sendMessage("Server#" + getName() + ": echo " + message);
            }
        }
        catch(IOException e)
        {
            e.printStackTrace();
            System.out.println("Server#" + getName() + " -> IOException");
        }
    }

    public void sendMessage(String message)
    {
        pw.println(message);
        System.out.println("Server#" + getName() + " is sending ~ " + message);
    }
}

<强> Client.java

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

public class Client
{
    private static int SOCKET_COUNT = 0;

    public static void main(String[] args) throws UnknownHostException, IOException
    {
        Socket s1 = new Socket(InetAddress.getLocalHost(), 2000);
        Socket s2 = new Socket(InetAddress.getLocalHost(), 2000);
        Socket s3 = new Socket(InetAddress.getLocalHost(), 2000);
        Socket s4 = new Socket(InetAddress.getLocalHost(), 2000);

        LocalSocketHandler h1 = new LocalSocketHandler("LocalHandler#" + ++SOCKET_COUNT, s1);
        LocalSocketHandler h2 = new LocalSocketHandler("LocalHandler#" + ++SOCKET_COUNT, s2);
        LocalSocketHandler h3 = new LocalSocketHandler("LocalHandler#" + ++SOCKET_COUNT, s3);
        LocalSocketHandler h4 = new LocalSocketHandler("LocalHandler#" + ++SOCKET_COUNT, s4);

        h1.start();
        h2.start();
        h3.start();
        h4.start();

        h1.sendMessage("I am socket #1!");
        h2.sendMessage("I am socket #2!");
        h3.sendMessage("I am socket #3!");
        h4.sendMessage("I am socket #4!");
    }
}

class LocalSocketHandler extends Thread
{
    private PrintWriter pw;
    private BufferedReader br;

    public LocalSocketHandler(String name, Socket socket) throws IOException
    {
        super(name);
        this.pw = new PrintWriter(socket.getOutputStream(), true);
        this.br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
    }

    @Override
    public void run()
    {
        String message;
        try
        {
            while((message = br.readLine()) != null)
            {
                System.out.println("Client#" + getName() + " - Server sent: " + message);
            }
        }
        catch(IOException e)
        {
            e.printStackTrace();
            System.out.println("Client#" + getName() + " -> IOException");
        }
    }

    public void sendMessage(String message)
    {
        pw.println(message);
        System.out.println("Client#" + getName() + " is sending ~ " + message);
    }
}

客户端控制台:

Client#LocalHandler#1 is sending ~ I am socket #1!
Client#LocalHandler#2 is sending ~ I am socket #2!
Client#LocalHandler#3 is sending ~ I am socket #3!
Client#LocalHandler#4 is sending ~ I am socket #4!
Client#LocalHandler#1 - Server sent: Server#Handler#1: echo I am socket #1!
Client#LocalHandler#3 - Server sent: Server#Handler#3: echo I am socket #3!
Client#LocalHandler#2 - Server sent: Server#Handler#2: echo I am socket #2!
Client#LocalHandler#4 - Server sent: Server#Handler#4: echo I am socket #4!

服务器控制台:

Server#Handler#1 - Client sent: I am socket #1!
Server#Handler#1 is sending ~ Server#Handler#1: echo I am socket #1!
Server#Handler#3 - Client sent: I am socket #3!
Server#Handler#3 is sending ~ Server#Handler#3: echo I am socket #3!
Server#Handler#2 - Client sent: I am socket #2!
Server#Handler#2 is sending ~ Server#Handler#2: echo I am socket #2!
Server#Handler#4 - Client sent: I am socket #4!
Server#Handler#4 is sending ~ Server#Handler#4: echo I am socket #4!