简体   繁体   English

无法连接到Java服务器

[英]Not able to connect to java server

I am currently trying to connect my arduino uno client to a java server. 我目前正在尝试将arduino uno客户端连接到Java服务器。 However, i am not able to connect to the server. 但是,我无法连接到服务器。 Java clients are able to connect to the server but not the arduino. Java客户端能够连接到服务器,但不能连接到arduino。 I am using an ethernet shield. 我正在使用以太网屏蔽。

Arduino code: Arduino代码:

     #include <Ethernet.h>
#include <SPI.h>


byte mac[] = { 0xF8, 0xA9, 0x63, 0x25, 0x92, 0x33 };
byte ip[] = { 169,254,103,204 };
byte server[] = { 269,254,130,203 }; // Google
int port = 9876;
EthernetClient client;

    void setup()
    {
      Ethernet.begin(mac, ip);
      Serial.begin(9600);

      delay(1000);

      Serial.println("connecting...");

      if (client.connect(server, port)) {
        Serial.println("connected");
        client.println("GET /search?q=arduino HTTP/1.0");
        client.println();
      } else {
        Serial.println("connection failed");
      }
    }

    void loop()
    {
      if (client.available()) {
        char c = client.read();
        Serial.print(c);
      }

      if (!client.connected()) {
        Serial.println();
        Serial.println("disconnecting.");
        client.stop();
        for(;;)
          ;
      }
    }

Java code: Java代码:

package CardClient;

import java.io.*;
import java.net.*;

public class Server {
    private int cardPort = 9876;
    private int clientPort = 6789;
    private ServerSocket serverCard = null;
    private ServerSocket serverClient = null;
    private Socket cardSocket;
    private Socket clientSocket1;
    private Socket clientSocket2;
    private ObjectOutputStream cardOutput = null;
    private ObjectInputStream cardInput = null;
    private ObjectOutputStream clientOutput1 = null;
    private ObjectInputStream clientInput1 = null;
    private ObjectOutputStream clientOutput2 = null;
    private ObjectInputStream clientInput2 = null;
    private int player1 = 0;
    private int player2 = 0;
    private Thread thread1 = null;
    private Thread thread2 = null;

    // En Konstruktor som tar in vilken port själva komnikationen ska finns på
    public Server() {
        try {
            serverCard = new ServerSocket(cardPort, 1);
            serverClient = new ServerSocket(clientPort, 2);
        } 
        catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("Server running");
        handleCard();
        handleClient();
    }

    public void handleCard() {
        while (true) {
            try {
                cardSocket = serverCard.accept();
                cardOutput = new ObjectOutputStream(cardSocket.getOutputStream());
                cardInput = new ObjectInputStream(cardSocket.getInputStream());
            } 
            catch (IOException e) {
                e.printStackTrace();
            }
            new Thread(new ReceiveCard()).start();
        }
    }

    // En metod som accepterar inkommande klienter
    public void handleClient() {
        while (true) {
            waitingForClient1();
            waitingForClient2();
        }
    }

    public void waitingForClient1() {
        while (true) {
            try {
                clientSocket1 = new Socket();
                clientSocket1 = serverClient.accept();
                setupStreams(1);
                Thread thread1 = new Thread(new ReceiveClient1());
                thread1.start();
            } 
            catch (IOException e) {
                System.out.println("Trying to reconnect to client 1");
                continue;
            }
            return;
        }
    }

    public void waitingForClient2() {
        while (true) {
            try {
                clientSocket2 = new Socket();
                clientSocket2 = serverClient.accept();
                setupStreams(2);
                thread2 = new Thread(new ReceiveClient2());
                thread2.start();
            } 
            catch (IOException e) {
                System.out.println("Trying to reconnect to client 2");
                continue;
            }
            return;
        }
    }

    public void stopThreads(int index) {
        if (index == 1) {
            thread1.stop();
        }
        if (index == 2) {
            thread2.stop();
        }
    }

    public void setupStreams(int index) throws IOException {
        if (index == 1) {
            clientOutput1 = new ObjectOutputStream(clientSocket1.getOutputStream());
            clientInput1 = new ObjectInputStream(clientSocket1.getInputStream());
        }
        if (index == 2) {
            clientOutput2 = new ObjectOutputStream(clientSocket2.getOutputStream());
            clientInput2 = new ObjectInputStream(clientSocket2.getInputStream());
        }
    }

    public void sendMessage(String outputMessage, int index) {
        if (index == 0) {
            try {
                cardOutput.writeObject(outputMessage);
                cardOutput.flush();
                cardOutput.reset();
            } 
            catch (IOException e) {
                System.out.println("Card error");
            }
        }
        if (index == 1) {
            try {
                clientOutput1.writeObject(outputMessage);
                clientOutput1.flush();
                clientOutput1.reset();
            } 
            catch (IOException e) {
                stopThreads(index);
                waitingForClient1();
            }
        }
        if (index == 2) {
            try {
                clientOutput2.writeObject(outputMessage);
                clientOutput2.flush();
                clientOutput2.reset();
            } 
            catch (IOException e) {
                stopThreads(index);
                waitingForClient2();
            }
        }
    }

    public String getMessage(int index) {
        String inputMessage = null;
        System.out.println(index);
        if (index == 0) {
            if (ConnectionIsUp(index)) {
                try {
                    inputMessage = (String) cardInput.readObject();
                } 
                catch (ClassNotFoundException | IOException e) {
                }
            }
        }
        if (index == 1) {
            if (ConnectionIsUp(index)) {
                try {
                    inputMessage = (String) clientInput1.readObject();
                } 
                catch (ClassNotFoundException | IOException e) {
                    stopThreads(index);
                    waitingForClient1();
                }
            }
        }
        if (index == 2) {
            if (ConnectionIsUp(index)) {
                try {
                    inputMessage = (String) clientInput2.readObject();
                } 
                catch (ClassNotFoundException | IOException e) {
                    stopThreads(index);
                    waitingForClient2();
                }
            }
        }
        return inputMessage;
    }

    public boolean ConnectionIsUp(int index) {
        if (index == 0) {
            return !cardSocket.isClosed();
        }
        if (index == 1) {
            return !clientSocket1.isClosed();
        }
        if (index == 2) {
            return !clientSocket2.isClosed();
        }
        return false;
    }

    // Vanlig main metod som startar servern
    public static void main(String args[]) {
        new Server();
    }

    private class ReceiveCard implements Runnable {
        public void run() {
            System.out.println("Streams are now up with the card!");
            while (true) {
                if (ConnectionIsUp(0)) {
                    System.out.println("The card send the following: " + getMessage(0));
                }
            }
        }
    }

    private class ReceiveClient1 implements Runnable {
        public void run() {
            System.out.println("Streams are now up with the first client!");
            String temp;
            while (true) {
                if (ConnectionIsUp(1)) {
                    temp = getMessage(1);
                    if (temp != null) {
                        sendMessage(temp, 1);
                    }
                    System.out.println("Client 1: " + temp);
                }
                else {
                    stopThreads(1);
                    waitingForClient1();
                }
            }
        }
    }

    private class ReceiveClient2 implements Runnable {
        public void run() {
            System.out.println("Streams are now up with the second client!");
            String temp;
            while (true) {
                if (ConnectionIsUp(2)) {
                    temp = getMessage(2);
                    if (temp != null) {
                        sendMessage(temp, 2);
                    }
                    System.out.println("Client 2: " + temp);
                }
                else {
                    stopThreads(2);
                    waitingForClient2();
                }
            }
        }
    }
}

You need to put those two accept loops into two separate threads. 您需要将这两个accept循环放入两个单独的线程中。 The first one never exits so you can never get to the second one at all. 第一个永远不会退出,因此您根本无法进入第二个。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM