简体   繁体   English

Java客户端/服务器套接字问题

[英]Java client/server socket issues

I am having some issues connecting a client and server together. 我在将客户端和服务器连接在一起时遇到一些问题。 The client just times out trying to reach the server. 客户端只是在尝试访问服务器时超时。

I have already done the following troubleshooting on my network: 我已经在网络上完成了以下疑难解答:

  • I have port forwarded the ports correctly (I have done so many many times before) and it still times out. 我已经正确转发了端口的端口(我已经做过很多次了),但仍然超时。
    • I am using a new modem, a ARRIS TG862 and the first one I got was bad, it would not open the ports at all. 我使用的是新的调制解调器ARRIS TG862,而我得到的第一个调制解调器很糟糕,根本无法打开端口。
    • Now the ports are opening up correctly because I am hosting a website and people can connect, and also I have hosted a few games and there has been no issues. 现在,端口已正确打开,因为我托管了一个网站,并且人们可以连接,并且托管了一些游戏,而且没有任何问题。
  • The modem I am using is using IPv6 which I do not know if that is an issue or not? 我使用的调制解调器正在使用IPv6,我不知道这是不是一个问题?
  • I have tried different ports and it still does the same thing. 我尝试了不同的端口,但它仍然执行相同的操作。 I did even revert to my old modem and router combination and it still will not work. 我什至没有恢复到旧的调制解调器和路由器组合,但仍然无法使用。

I am out of options and have no idea what is going on. 我没有选择,也不知道发生了什么。

  • The server/client works fine on localhost but as soon as I try to connect externally it fails. 服务器/客户端在localhost上工作正常,但是一旦我尝试进行外部连接,它就会失败。
  • When the server is running I can go to canyouseeme.org and it can see the opened port and my program exits because something connected to it. 当服务器运行时,我可以转到canyouseeme.org,它可以看到打开的端口,并且我的程序退出,因为有东西连接到它。
  • I am using the same computer to connect and host, which I have done before and it worked fine a long time ago. 我使用的是同一台计算机进行连接和托管,这是我以前做过的,而且很久以前还可以。
  • I also tried using different computers to connect to each other (on the same network). 我还尝试使用不同的计算机相互连接(在同一网络上)。

If there is any suggestions on where to start to determine the cause of my problems that would be great, I am just kind of lost on where to diagnose from here. 如果对从哪里开始确定问题原因有任何建议,那将是很大的选择,那么,我就迷失了从哪里进行诊断的信息。 The code is below. 代码如下。 I'll even retry some steps if anyone thinks it is something that I already tested. 如果有人认为这是我已经测试过的东西,我什至会重试一些步骤。

Client 客户

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


public class Client{
    private static Socket socket;
    private static PrintWriter printWriter;
    private int portNumber = 1337;
    private String hostName = "234.123.1.234";//Not real IP, made up ip address to demonstrate code to stack overflow users


    public static void main(String[] args){
        new Client().validateSecurity();
    }

    public void validateSecurity(String key){
        try {
            Socket kkSocket = new Socket(hostName, portNumber);
            PrintWriter out = new PrintWriter(kkSocket.getOutputStream(), true);
            BufferedReader in = new BufferedReader(
                    new InputStreamReader(kkSocket.getInputStream()));
        }catch(Exception e){

        }
    }
}

Server 服务器

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

import javax.swing.JFrame;
import javax.swing.JOptionPane;


public class Server{
    private static Socket socket;
    private static PrintWriter printWriter;
    private int portNumber = 1337;


    public static void main(String[] args){
        new Server().validateSecurity("hi");
    }

    public void validateSecurity(String key){
        try
        {
            ServerSocket serverSocket = new ServerSocket(portNumber);
            System.out.println("WAITING ON CONNECTION");
            Socket clientSocket = serverSocket.accept();
            System.out.println("CONNECTION ACCEPTED");
            PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
            BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
        }catch(Exception e){
            e.printStackTrace();
        }
    }
}

连接超时几乎总是表示防火墙有问题。

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

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