Java以两种方式打开套接字流

时间:2017-05-22 21:27:08

标签: java multithreading sockets

我想制作一个游戏(丛林速度),玩家点击图腾并改变屏幕上的位置(我使用挥杆,这并不重要),并且应该向每个人发送有关本地化更改的信息。

我想创建一个服务器来获取玩家的点击,验证它并向所有人发送更新信息。 在这种情况下,如果有人点击图腾并同时准备发送有关他自己点击的信息,客户端会监听服务器。 服务器监听所有人,同时准备向所有人发送信息。 我尝试像这样实现它: 服务器为每个播放器生成线程,监听内部点击并准备好被中断以发送新的图腾本地化(我在ExecutorService上使用方法shutdownNow,这应该导致线程中的IOException,这将使他们停止循环并发送有关新本地化的信息)然后客户端得到它。 如果客户端点击,则线程被中断,而不是等待新的本地化,它发送他的点击。

问题是我无法创建流。这是输出和代码

客户方:

2017-05-22T23:04:06.417Connected
2017-05-22T23:04:06.417Trying to make output
2017-05-22T23:04:06.417Trying to make input

服务器端:

2017-05-22T23:04:03.278Server Thread :Socket created
2017-05-22T23:04:03.294Server Thread :Waiting for client!
2017-05-22T23:04:06.385Server Thread :Correct, connected!
2017-05-22T23:04:12.239Trying to make input

客户端代码:

package client;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.time.LocalDateTime;
import java.util.logging.Level;
import java.util.logging.Logger;


public class ServerConnection implements Runnable {

    MainWindow frame;
    Socket toServ;
    Socket fromServ;
    ServerSocket myServ;
    ObjectOutputStream out;
    ObjectInputStream reader;
    public int x, y, totemx, totemy;
    int i = 0;

    public ServerConnection(MainWindow frame) {
        try {
            this.frame = frame;
            myServ = new ServerSocket(1338);
            toServ = new Socket("localhost", 1337);
            fromServ = myServ.accept();
            System.out.println(LocalDateTime.now() + "Connected");
            try {
                System.out.println(LocalDateTime.now() + "Trying to make output");
                out = new ObjectOutputStream(new BufferedOutputStream(toServ.getOutputStream()));
            } catch (IOException ex) {
                Logger.getLogger(ServerConnection.class.getName()).log(Level.SEVERE, null, ex);
            }
            System.out.println(LocalDateTime.now() + "Trying to make input");
            reader = new ObjectInputStream(new BufferedInputStream(fromServ.getInputStream()));
        } catch (IOException ex) {
            Logger.getLogger(ServerConnection.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public void run() {
        System.out.println(LocalDateTime.now() + "Running");

        while (true) {
            try {
                int xGot, yGot;
                System.out.println(LocalDateTime.now() + "Waiting for params");
                xGot = (int) reader.readInt();
                yGot = (int) reader.readInt();
                System.out.println(LocalDateTime.now() + "I got new params");
                frame.refresh(xGot, yGot);
            } catch (IOException ex) {
                {
                    try {
                        System.out.println(LocalDateTime.now() + "Sending click thread: Sending my click");
                        out.writeInt(x);
                        out.writeInt(y);
                        System.out.println(LocalDateTime.now() + "Sent");
                    } catch (IOException ex1) {
                        Logger.getLogger(ServerConnection.class.getName()).log(Level.SEVERE, null, ex1);
                    }
                }
            }
        }
    }
}

服务器端代码 第一个档案:

package javaapplicationserwer;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.time.LocalDateTime;
import java.util.Random;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Japko
 */
public class Server{

    public int x, y;
    ServerSocket serv = null;
    ExecutorService executor;
    Server()
    {
        x = 10;
        y = 50;
         executor = Executors.newFixedThreadPool(4);
        try {
            serv = new ServerSocket(1337);
            System.out.println(LocalDateTime.now() + "Server Thread :Socket created");
            while (true) {
                System.out.println(LocalDateTime.now() + "Server Thread :Waiting for client!");
                Socket fromSocket = serv.accept();
                Socket toSocket=new Socket(fromSocket.getInetAddress(),1338);
                System.out.println(LocalDateTime.now() + "Server Thread :Correct, connected!");
                ClientConnection temp = new ClientConnection(fromSocket,toSocket, this);
                executor.submit(temp);
            }
        } catch (IOException ex) {

            Logger.getLogger(JavaApplicationSerwer.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void updateIt(int x, int y) {
        System.out.println(LocalDateTime.now() + "Updating");
        if (x == this.x && y == this.y) {
            Random rand = new Random();
            this.x = rand.nextInt(300);
            this.y = rand.nextInt(300);
            System.out.println(LocalDateTime.now() + "Updated");
            executor.shutdownNow();
        }
        System.out.println(LocalDateTime.now() + "I notify");
    }
}

第二个文件(实现由服务器为每个玩家创建的runnable的类):

package javaapplicationserwer;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.time.LocalDateTime;
import java.util.logging.Level;
import java.util.logging.Logger;

public class ClientConnection implements Runnable {

    Socket fromSocket, toSocket;
    InetAddress IP;
    Server serv;
    ObjectOutputStream out;
    ObjectInputStream reader;

    public ClientConnection(Socket fromSocket, Socket toSocket, Server serwer) {
        this.fromSocket = fromSocket;
        this.toSocket = toSocket;
        this.serv = serwer;
        try {
            System.out.println(LocalDateTime.now() + "Trying to make input");
            reader = new ObjectInputStream(new BufferedInputStream(fromSocket.getInputStream()));
        } catch (IOException ex) {
            Logger.getLogger(ClientConnection.class.getName()).log(Level.SEVERE, null, ex);
        }
        System.out.println(LocalDateTime.now() + "Trying to make output");
        try {
            out = new ObjectOutputStream(new BufferedOutputStream(toSocket.getOutputStream()));
        } catch (IOException ex) {
            Logger.getLogger(ClientConnection.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public void run() {
        while (true) {
            System.out.println(LocalDateTime.now() + "Starting");
            try {
                int xGot, yGot;
                while (true) {
                    System.out.println(LocalDateTime.now() + "Waiting for params");
                    try {
                        xGot = reader.readInt();
                        yGot = reader.readInt();
                        System.out.println(LocalDateTime.now() + "Got this");
                        //serv.wait();
                        System.out.println(LocalDateTime.now() + "Waited");
                        serv.updateIt(xGot, yGot);
                        System.out.println(LocalDateTime.now() + "Verified");
                    } catch (IOException ex) {
                        try {
                            out.writeInt(serv.x);
                            out.writeInt(serv.y);
                        } catch (IOException ex1) {
                            Logger.getLogger(ClientConnection.class.getName()).log(Level.SEVERE, null, ex1);
                        }
                        Logger.getLogger(ClientConnection.class.getName()).log(Level.SEVERE, null, ex);
                    }

                }
            } finally {
                System.out.println(LocalDateTime.now() + "I'm not serving for you");
            }
        }
    }
}

听起来客户端有配对输出(这意味着服务器有输入),然后客户端想要创建输入,但服务器从构造函数中逃脱,甚至不尝试输出。

提前感谢您的帮助。

2 个答案:

答案 0 :(得分:0)

套接字是两(2)路连接。您只需要在客户端连接中使用一个套接字。

这里有一些简单的tcp代码:

package p;
import java.io.*;
import java.net.*;
import java.util.function.*;
public class Tcp {
    static class Acceptor extends Thread {
        Acceptor(ServerSocket serverSocket,Consumer<Socket> consumer) {
            super("Acceptor");
            this.serverSocket=serverSocket;
            this.consumer=consumer;
        }
        @Override public void run() {
            p("acceptor running on: "+serverSocket);
            while(true)
                try {
                    Socket socket=serverSocket.accept();
                    if(consumer!=null) consumer.accept(socket);
                } catch(IOException e) {
                    p(getName()+" caught: "+e);
                    break;
                }
        }
        final ServerSocket serverSocket;
        final Consumer<Socket> consumer;
    }
    static class Connection implements Runnable {
        Connection(Socket socket) throws IOException {
            this.socket=socket;
            in=new BufferedReader(new InputStreamReader(socket.getInputStream()));
            out=new OutputStreamWriter(socket.getOutputStream());
        }
        boolean send(String string) {
            try {
                p("sending: "+string+" on: "+socket);
                out.write(string+'\n'/*System.getProperty("line.separator")*/);
                out.flush();
                return true;
            } catch(IOException e) {
                e.printStackTrace();
            }
            return false;
        }
        void process(String string) {
            p("connection on: "+socket+" received: "+string);
        }
        @Override public void run() {
            p("connection on: "+socket+" is runing.");
            String string=null;
            try {
                p("connection on: "+socket+" is trying to read.");
                while((string=in.readLine())!=null) {
                    process(string);
                }
            } catch(IOException e) {
                e.printStackTrace();
            }
            process(null);
            p("connection on: "+socket+" is exiting run()");
        }
        final Socket socket;
        final BufferedReader in;
        final Writer out;
    }
    public static void p(String string) {
        System.out.println(string);
    }
    Tcp(String host,Integer service) throws IOException {
        ServerSocket serverSocket=new ServerSocket();
        SocketAddress socketAddress=new InetSocketAddress(host,service);
        serverSocket.bind(socketAddress);
        Consumer<Socket> socketConsumer=(socket)-> {
            p("accepted from: "+socket);
            try {
                final Connection connection=new Connection(socket) {
                    @Override void process(String string) {
                        super.process(string);
                        send(string.toUpperCase());
                    }
                };
                new Thread(connection,"incoming").start();
            } catch(IOException e2) {
                e2.printStackTrace();
            }
        };
        new Acceptor(serverSocket,socketConsumer).start();
    }
    public static void main(String[] args) throws UnknownHostException,IOException,InterruptedException {
        final String host="localhost";
        final Integer service=1237;
        Tcp tcp=new Tcp(host,service);
        Socket socket=new Socket(host,service);
        Connection c1=new Connection(socket);
        new Thread(c1,"c1").start();
        socket=new Socket(host,service);
        Connection c2=new Connection(socket);
        Thread.sleep(500);
        new Thread(c2,"c2").start();
        c1.send("foo");
        c2.send("bar");
    }
}

答案 1 :(得分:0)

在这种情况下,

out.flush()有所帮助。