我试图建立一个服务器 - 客户端模型,它将是一个服务器的java应用程序,一个Android应用程序将与我的服务器计算机建立一个Socket连接,在监听端口(任意选择50000)< / p>
Android app注册,登录,发出一些信息请求,并发送一些回复。服务器程序侦听一个或多个客户端并从MySQL数据库中获取一些信息,当客户端结束时,请求会打印出他们已购买的内容的财务记录。
几乎所有的作品都是:
过了一会儿(几个小时,几分钟),外部访问变得无法访问,原因不明显:
但是当我尝试执行telnet时,事情变得不清楚了:
使用ServerSocket打开服务器的服务器端应用程序的java代码:
@Override
public void run()
{
try {
Start();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
Inicio.Erro("Erro no Server:"+e.getMessage());
e.printStackTrace();
}
System.out.println("SocketServerThread Parou");
Inicio.LOG("Server Parou");
}
public void Start() throws InterruptedException
{
connect();
while(KeepWaiting)
{
WaitClient();
}
}
public void connect()
{
try {
serverSocket =new ServerSocket(50000);
System.out.println("my ServerSocket started");
System.out.println("listening:\n"+InetAddress.getLocalHost()+":"+serverSocket.getLocalPort());
} catch (IOException e) {
e.printStackTrace();
}
}
public void WaitClient() throws InterruptedException{
try {
Inicio.LOG("Waiting Connection...");
Socket socket = serverSocket.accept(); // blocking wait ( 99.9999% of the time in this line )
BufferedReader buff_Reader = new BufferedReader(new InputStreamReader(socket.getInputStream(),Charset.forName("UTF-8")));
BufferedWriter buff_Writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(),Charset.forName("UTF-8")));
System.out.println("<Conected>");
System.out.println("IP Client: " + socket.getInetAddress());
Inicio.LOG("Conected -> IP Client: " + socket.getInetAddress());
ReadThread reader_thread = new ReadThread(buff_Reader);
WriteThread writer_thread = new WriteThread(buff_Writer);
ConectionClient newconn = new ConectionClient(this,reader_thread,writer_thread,socket);
Conections.add(newconn);
System.out.println("Number of Conections:"+Conexoes.size());
newconn.start(); // stats the thread thats holds the conection with that guest
} catch (IOException e ){//SQLException e) {
System.out.println("<Error>");
Inicio.Erro("Server Stoped with Error:"+e.getMessage());
e.printStackTrace();
KeepWaiting=false;
}
finally{
// ends the server
}
}
我的客户端android应用程序的代码,它通过Socket与服务器连接:
@Override
public void run()
{
try {
connect();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public void connect() throws InterruptedException
{
try {
exaustive_connect();
if(connection.isConnected()&&Continue) {
BufferedReader buff_reader = new BufferedReader(new InputStreamReader(new myInputStream(connection.getInputStream()), Charset.forName("UTF-8")));
BufferedWriter buff_writer = new BufferedWriter(new OutputStreamWriter(new myOutputStream(connection.getOutputStream()),Charset.forName("UTF-8")));
Reader = new ReadThread(buff_reader);
Writer = new WriteThread(buff_writer);
Communicate(); // method that listen and write messages.
}
else
{
ManageConection.E("ConexaoServidor","could not connect");
}
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally{
close(); // method that close the socket, reader and writer.
}
}
private void exaustive_connect()
{
try {
if(!exaustive_tryconn(500))
{
if(!exaustive_tryconn(2000))
{
if(!exaustive_tryconn(5000))
{
ManageConection.LOG("could not connect");
}
}
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
private boolean exaustive_tryconn(int timeout)
{
connection = new Socket();
try {
connection.connect(new InetSocketAddress(ManageConection.IP, ip_holder.port), timeout); // tries the first host
} catch (Exception e) {
e.printStackTrace();
}
if(connection.isConnected())return true;
connection = new Socket();
try {
connection.connect(new InetSocketAddress(ManageConection.IP2, ip_holder.port), timeout); // tries the second host
} catch (Exception e) {
e.printStackTrace();
}
return connection.isConnected();
}
所以我在这里试图找到解决问题的方法。任何帮助,以便我可以确定哪些问题是受欢迎的 - ISP,防火墙,NAT,越野车UPnP,阻止代理等?
让我的路由器再次开心的原因是&#39;重新开始&#39;一段时间(几个小时,有时几分钟......)所有工作,我的送货系统工作正常,但这使服务器离线5至10分钟,我希望这可以保持长达六个小时没有中断,或者一些客户会对我的错误服务器/客户端感到失望。
为了测试端口是否打开,我还使用了这个网站http://ping.eu/port-chk/他们说端口是打开的,我的程序检测到有人尝试连接...所以如果这个网站的服务器可以连接到我的服务器...为什么我不能?
Abhi,在测试simple things
...
ARP条目:
NAT规则通过iptables:
这可能是一个unix命令,或者有点因为我只找到信息和支持来在unix系统中查看它,经过长时间的搜索后我找到了命令&#39; Route print&#39;打印有点像iptable
防火墙日志
当服务器正在工作时,允许连接,但是当不工作时,防火墙的日志根本不显示任何有关连接断开的信息...只显示许多允许的地方,但不是#39; t跟踪我的telnet,或者当我尝试从另一个客户端连接时......
当我禁用防火墙时,服务器仍无法访问。
路由器日志
在路由器日志中:
21:24:12 (since last boot) **SYN Flood** 192.168.1.8, 7269->> 179.252.7.196, 50000 (from PPPoE1 Outbound)
21:28:02 (since last boot) **SYN Flood** 192.168.1.5, 60810->> 179.252.7.196, 50000 (from PPPoE1 Outbound)
21:30:17 (since last boot) **SYN Flood** 88.198.46.51, 47508->> 192.168.1.8, 50000 (from PPPoE1 Inbound)
路由器日志说服务器何时不工作......
Microsoft消息分析工具
过滤日志的打印:https://uploaddeimagens.com.br/imagens/messageanalizer-png
要查看的内容:
Application\tTCP: Retransmitted SYN, original message is #1524